X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FHelper%2FHttpClientFactory.php;h=3e19a7be1f4a3943b45fd2fe1ec873bb94a0c145;hb=1048c9c4a811821b00cc04bfec905bebcc22bac4;hp=8891887b6e97d1a68ad3f783f130ac49879261d6;hpb=78295b99dd1721c613f1ce52e2debbe6f6db7753;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php index 8891887b..3e19a7be 100644 --- a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php @@ -2,53 +2,79 @@ namespace Wallabag\CoreBundle\Helper; -use Graby\Ring\Client\SafeCurlHandler; -use GuzzleHttp\Client; +use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Cookie\CookieJar; use GuzzleHttp\Event\SubscriberInterface; +use Http\Adapter\Guzzle5\Client as GuzzleAdapter; +use Psr\Log\LoggerInterface; +use Http\Client\HttpClient; +use Http\HttplugBundle\ClientFactory\ClientFactory; /** - * Builds and configures the Guzzle HTTP client. + * Builds and configures the HTTP client. */ -class HttpClientFactory +class HttpClientFactory implements ClientFactory { - /** @var \GuzzleHttp\Event\SubscriberInterface */ - private $authenticatorSubscriber; + /** @var [\GuzzleHttp\Event\SubscriberInterface] */ + private $subscribers = []; /** @var \GuzzleHttp\Cookie\CookieJar */ private $cookieJar; private $restrictedAccess; + private $logger; /** * HttpClientFactory constructor. * - * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber - * @param \GuzzleHttp\Cookie\CookieJar $cookieJar - * @param string $restrictedAccess this param is a kind of boolean. Values: 0 or 1 + * @param \GuzzleHttp\Cookie\CookieJar $cookieJar + * @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1 + * @param LoggerInterface $logger */ - public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar, $restrictedAccess) + public function __construct(CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger) { - $this->authenticatorSubscriber = $authenticatorSubscriber; $this->cookieJar = $cookieJar; $this->restrictedAccess = $restrictedAccess; + $this->logger = $logger; } /** - * @return \GuzzleHttp\Client|null + * Adds a subscriber to the HTTP client. + * + * @param SubscriberInterface $subscriber + */ + public function addSubscriber(SubscriberInterface $subscriber) + { + $this->subscribers[] = $subscriber; + } + + /** + * Input an array of configuration to be able to create a HttpClient. + * + * @param array $config + * + * @return HttpClient */ - public function buildHttpClient() + public function createClient(array $config = []) { + $this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]); + if (0 === (int) $this->restrictedAccess) { - return null; + return new GuzzleAdapter(new GuzzleClient($config)); } // we clear the cookie to avoid websites who use cookies for analytics $this->cookieJar->clear(); - // need to set the (shared) cookie jar - $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); - $client->getEmitter()->attach($this->authenticatorSubscriber); + if (!isset($config['defaults']['cookies'])) { + // need to set the (shared) cookie jar + $config['defaults']['cookies'] = $this->cookieJar; + } + + $guzzle = new GuzzleClient($config); + foreach ($this->subscribers as $subscriber) { + $guzzle->getEmitter()->attach($subscriber); + } - return $client; + return new GuzzleAdapter($guzzle); } }