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 $authenticatorSubscriber;
19 /** @var \GuzzleHttp\Cookie\CookieJar */
22 private $restrictedAccess;
26 * HttpClientFactory constructor.
28 * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber
29 * @param \GuzzleHttp\Cookie\CookieJar $cookieJar
30 * @param string $restrictedAccess this param is a kind of boolean. Values: 0 or 1
31 * @param LoggerInterface $logger
33 public function __construct(SubscriberInterface
$authenticatorSubscriber, CookieJar
$cookieJar, $restrictedAccess, LoggerInterface
$logger)
35 $this->authenticatorSubscriber
= $authenticatorSubscriber;
36 $this->cookieJar
= $cookieJar;
37 $this->restrictedAccess
= $restrictedAccess;
38 $this->logger
= $logger;
42 * @return \GuzzleHttp\Client|null
44 public function buildHttpClient()
46 $this->logger
->log('debug', 'Restricted access config enabled?', array('enabled' => (int) $this->restrictedAccess
));
48 if (0 === (int) $this->restrictedAccess
) {
52 // we clear the cookie to avoid websites who use cookies for analytics
53 $this->cookieJar
->clear();
54 // need to set the (shared) cookie jar
55 $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar
]]);
56 $client->getEmitter()->attach($this->authenticatorSubscriber
);