Skip to content

Commit

Permalink
factory
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklog committed Nov 13, 2019
1 parent 8048a3e commit 4df924c
Showing 1 changed file with 65 additions and 51 deletions.
116 changes: 65 additions & 51 deletions src/Http/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Shapecode\FUT\Client\Api\CoreInterface;
Expand All @@ -38,6 +40,12 @@ class ClientFactory implements ClientFactoryInterface
/** @var RequestFactoryInterface */
protected $requestFactory;

/** @var StreamFactoryInterface */
protected $streamFactory;

/** @var UriFactoryInterface */
protected $urlFactory;

/** @var ConfigInterface */
protected $config;

Expand All @@ -51,14 +59,65 @@ class ClientFactory implements ClientFactoryInterface

public function __construct(
ConfigInterface $config,
?RequestFactoryInterface $requestFactory = null,
?CookieJarBuilderInterface $cookieJarBuilder = null,
?LoggerInterface $logger = null
?LoggerInterface $logger = null,
?RequestFactoryInterface $requestFactory = null,
?StreamFactoryInterface $streamFactory = null,
?UriFactoryInterface $urlFactory = null
) {
$this->config = $config;
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
$this->cookieJarBuilder = $cookieJarBuilder ?: new CookieJarBuilder();
$this->logger = $logger ?: new NullLogger();

$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
$this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory();
$this->urlFactory = $urlFactory ?: Psr17FactoryDiscovery::findUrlFactory();
}

/**
* @inheritdoc
*/
public function request(
AccountInterface $account,
string $method,
string $url,
array $options = [],
array $plugins = []
) : ClientCall {
$headers = [];

if (isset($options['headers'])) {
/** @var mixed[] $headers */
$headers = $options['headers'];
unset($options['headers']);
}

$call = new ClientCall();

$plugins[] = new HeaderSetPlugin(CoreInterface::REQUEST_HEADERS);
$plugins[] = new HeaderSetPlugin([
'User-Agent' => $this->getConfig()->getUserAgent(),
]);

if (count($headers) > 0) {
$plugins[] = new HeaderSetPlugin($headers);
}

$plugins[] = new ContentLengthPlugin();
$plugins[] = new LoggerPlugin($this->logger);
$stopwatch = new Stopwatch();
$plugins[] = new StopwatchPlugin($stopwatch);
$plugins[] = new ClientCallPlugin($call);
$plugins[] = new RedirectPlugin();

$guzzle = $this->createAccountClient($account, $options);
$client = $this->createPluginClient($guzzle, $plugins);

$request = $this->createRequest($method, $url);

$client->sendRequest($request);

return $call;
}

/**
Expand Down Expand Up @@ -105,10 +164,11 @@ protected function createRequest(
?string $body = null,
array $headers = []
) : RequestInterface {
$request = $this->requestFactory->createRequest($method, $uri);
$url = $this->urlFactory->createUri($uri);
$request = $this->requestFactory->createRequest($method, $url);

if ($body !== null) {
$stream = Psr17FactoryDiscovery::findStreamFactory()->createStream($body);
$stream = $this->streamFactory->createStream($body);
$request = $request->withBody($stream);
}

Expand All @@ -121,52 +181,6 @@ protected function createRequest(
return $request;
}

/**
* @inheritdoc
*/
public function request(
AccountInterface $account,
string $method,
string $url,
array $options = [],
array $plugins = []
) : ClientCall {
$headers = [];

if (isset($options['headers'])) {
/** @var mixed[] $headers */
$headers = $options['headers'];
unset($options['headers']);
}

$call = new ClientCall();

$plugins[] = new HeaderSetPlugin(CoreInterface::REQUEST_HEADERS);
$plugins[] = new HeaderSetPlugin([
'User-Agent' => $this->getConfig()->getUserAgent(),
]);

if (count($headers) > 0) {
$plugins[] = new HeaderSetPlugin($headers);
}

$plugins[] = new ContentLengthPlugin();
$plugins[] = new LoggerPlugin($this->logger);
$stopwatch = new Stopwatch();
$plugins[] = new StopwatchPlugin($stopwatch);
$plugins[] = new ClientCallPlugin($call);
$plugins[] = new RedirectPlugin();

$guzzle = $this->createAccountClient($account, $options);
$client = $this->createPluginClient($guzzle, $plugins);

$request = $this->createRequest($method, $url);

$client->sendRequest($request);

return $call;
}

protected function getConfig() : ConfigInterface
{
return $this->config;
Expand Down

0 comments on commit 4df924c

Please sign in to comment.