]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
Merge pull request #2846 from wallabag/mruminski-patch-1
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / HttpClientFactory.php
CommitLineData
7aab0ecf
BD
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use Graby\Ring\Client\SafeCurlHandler;
6use GuzzleHttp\Client;
7use GuzzleHttp\Cookie\CookieJar;
8use GuzzleHttp\Event\SubscriberInterface;
9
10/**
11 * Builds and configures the Guzzle HTTP client.
12 */
13class HttpClientFactory
14{
15 /** @var \GuzzleHttp\Event\SubscriberInterface */
16 private $authenticatorSubscriber;
17
18 /** @var \GuzzleHttp\Cookie\CookieJar */
19 private $cookieJar;
20
d64bf795
NL
21 private $restrictedAccess;
22
7aab0ecf
BD
23 /**
24 * HttpClientFactory constructor.
25 *
26 * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber
27 * @param \GuzzleHttp\Cookie\CookieJar $cookieJar
d51093a7 28 * @param string $restrictedAccess this param is a kind of boolean. Values: 0 or 1
7aab0ecf 29 */
d64bf795 30 public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar, $restrictedAccess)
7aab0ecf
BD
31 {
32 $this->authenticatorSubscriber = $authenticatorSubscriber;
33 $this->cookieJar = $cookieJar;
d64bf795 34 $this->restrictedAccess = $restrictedAccess;
7aab0ecf
BD
35 }
36
37 /**
d64bf795 38 * @return \GuzzleHttp\Client|null
7aab0ecf
BD
39 */
40 public function buildHttpClient()
41 {
d64bf795
NL
42 if (0 === (int) $this->restrictedAccess) {
43 return null;
44 }
45
40f3ea57
NL
46 // we clear the cookie to avoid websites who use cookies for analytics
47 $this->cookieJar->clear();
7aab0ecf
BD
48 // need to set the (shared) cookie jar
49 $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]);
50 $client->getEmitter()->attach($this->authenticatorSubscriber);
51
52 return $client;
53 }
54}