3 namespace Wallabag\CoreBundle\Helper
;
5 use Graby\Ring\Client\SafeCurlHandler
;
7 use GuzzleHttp\Cookie\CookieJar
;
8 use GuzzleHttp\Event\SubscriberInterface
;
9 use Psr\Log\LoggerInterface
;
12 * Builds and configures the Guzzle HTTP client.
14 class HttpClientFactory
16 /** @var [\GuzzleHttp\Event\SubscriberInterface] */
17 private $subscribers = [];
19 /** @var \GuzzleHttp\Cookie\CookieJar */
22 private $restrictedAccess;
26 * HttpClientFactory constructor.
28 * @param \GuzzleHttp\Cookie\CookieJar $cookieJar
29 * @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1
30 * @param LoggerInterface $logger
32 public function __construct(CookieJar
$cookieJar, $restrictedAccess, LoggerInterface
$logger)
34 $this->cookieJar
= $cookieJar;
35 $this->restrictedAccess
= $restrictedAccess;
36 $this->logger
= $logger;
40 * @return \GuzzleHttp\Client|null
42 public function buildHttpClient()
44 $this->logger
->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess
]);
46 if (0 === (int) $this->restrictedAccess
) {
50 // we clear the cookie to avoid websites who use cookies for analytics
51 $this->cookieJar
->clear();
52 // need to set the (shared) cookie jar
53 $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar
]]);
55 foreach ($this->subscribers
as $subscriber) {
56 $client->getEmitter()->attach($subscriber);
63 * Adds a subscriber to the HTTP client.
65 * @param SubscriberInterface $subscriber
67 public function addSubscriber(SubscriberInterface
$subscriber)
69 $this->subscribers
[] = $subscriber;