3 namespace Wallabag\CoreBundle\Helper
;
5 use GuzzleHttp\Client
as GuzzleClient
;
6 use GuzzleHttp\Cookie\CookieJar
;
7 use GuzzleHttp\Event\SubscriberInterface
;
8 use Http\Adapter\Guzzle5\Client
as GuzzleAdapter
;
9 use Http\Client\HttpClient
;
10 use Http\HttplugBundle\ClientFactory\ClientFactory
;
11 use Psr\Log\LoggerInterface
;
14 * Builds and configures the HTTP client.
16 class HttpClientFactory
implements ClientFactory
18 /** @var [\GuzzleHttp\Event\SubscriberInterface] */
19 private $subscribers = [];
21 /** @var \GuzzleHttp\Cookie\CookieJar */
24 private $restrictedAccess;
28 * HttpClientFactory constructor.
30 * @param \GuzzleHttp\Cookie\CookieJar $cookieJar
31 * @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1
32 * @param LoggerInterface $logger
34 public function __construct(CookieJar
$cookieJar, $restrictedAccess, LoggerInterface
$logger)
36 $this->cookieJar
= $cookieJar;
37 $this->restrictedAccess
= $restrictedAccess;
38 $this->logger
= $logger;
42 * Adds a subscriber to the HTTP client.
44 * @param SubscriberInterface $subscriber
46 public function addSubscriber(SubscriberInterface
$subscriber)
48 $this->subscribers
[] = $subscriber;
52 * Input an array of configuration to be able to create a HttpClient.
54 * @param array $config
58 public function createClient(array $config = [])
60 $this->logger
->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess
]);
62 if (0 === (int) $this->restrictedAccess
) {
63 return new GuzzleAdapter(new GuzzleClient($config));
66 // we clear the cookie to avoid websites who use cookies for analytics
67 $this->cookieJar
->clear();
68 if (!isset($config['defaults']['cookies'])) {
69 // need to set the (shared) cookie jar
70 $config['defaults']['cookies'] = $this->cookieJar
;
73 $guzzle = new GuzzleClient($config);
74 foreach ($this->subscribers
as $subscriber) {
75 $guzzle->getEmitter()->attach($subscriber);
78 return new GuzzleAdapter($guzzle);