public function authenticate(string $username, string $password) : Result
 {
     $identity = $this->identityHandler->createIdentity($username, $password);
     try {
         $resultSet = $this->resultSetClient->execute((new Command($this->identityLayout, [$this->usernameField => '==' . $this->resultSetClient->quoteString($username), '-find' => null]))->withIdentity($identity));
     } catch (InvalidResponseException $e) {
         $errorCode = $e->getCode();
         if (401 === $errorCode) {
             return Result::fromInvalidCredentials();
         }
         throw $e;
     }
     if ($resultSet->isEmpty()) {
         throw InvalidResultException::fromEmptyResultSet();
     }
     return Result::fromIdentity($identity);
 }
 private function buildRequest(Command $command, UriInterface $uri) : RequestInterface
 {
     $parameters = sprintf('-db=%s&%s', urlencode($this->database), $command);
     $body = new Stream('php://temp', 'wb+');
     $body->write($parameters);
     $body->rewind();
     $request = (new Request($uri->withUserInfo(''), 'POST'))->withAddedHeader('User-agent', 'SimpleFM')->withAddedHeader('Content-type', 'application/x-www-form-urlencoded')->withAddedHeader('Content-length', (string) strlen($parameters))->withBody($body);
     $credentials = urldecode($uri->getUserInfo());
     if ($command->hasIdentity()) {
         Assertion::notNull($this->identityHandler, 'An identity handler must be set to use identities on commands');
         $identity = $command->getIdentity();
         $credentials = sprintf('%s:%s', $identity->getUsername(), $this->identityHandler->decryptPassword($identity));
     }
     $this->logger->info(sprintf('%s?%s', (string) $uri->withUserInfo(''), $parameters));
     if ('' === $credentials) {
         return $request;
     }
     return $request->withAddedHeader('Authorization', sprintf('Basic %s', base64_encode($credentials)));
 }