diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/HttpClientFactory.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/HttpClientFactory.php | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php new file mode 100644 index 00000000..1ac8feb1 --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php | |||
@@ -0,0 +1,60 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Graby\Ring\Client\SafeCurlHandler; | ||
6 | use GuzzleHttp\Client; | ||
7 | use GuzzleHttp\Cookie\CookieJar; | ||
8 | use GuzzleHttp\Event\SubscriberInterface; | ||
9 | use Psr\Log\LoggerInterface; | ||
10 | |||
11 | /** | ||
12 | * Builds and configures the Guzzle HTTP client. | ||
13 | */ | ||
14 | class HttpClientFactory | ||
15 | { | ||
16 | /** @var \GuzzleHttp\Event\SubscriberInterface */ | ||
17 | private $authenticatorSubscriber; | ||
18 | |||
19 | /** @var \GuzzleHttp\Cookie\CookieJar */ | ||
20 | private $cookieJar; | ||
21 | |||
22 | private $restrictedAccess; | ||
23 | private $logger; | ||
24 | |||
25 | /** | ||
26 | * HttpClientFactory constructor. | ||
27 | * | ||
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 | ||
32 | */ | ||
33 | public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger) | ||
34 | { | ||
35 | $this->authenticatorSubscriber = $authenticatorSubscriber; | ||
36 | $this->cookieJar = $cookieJar; | ||
37 | $this->restrictedAccess = $restrictedAccess; | ||
38 | $this->logger = $logger; | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * @return \GuzzleHttp\Client|null | ||
43 | */ | ||
44 | public function buildHttpClient() | ||
45 | { | ||
46 | $this->logger->log('debug', 'Restricted access config enabled?', array('enabled' => (int) $this->restrictedAccess)); | ||
47 | |||
48 | if (0 === (int) $this->restrictedAccess) { | ||
49 | return; | ||
50 | } | ||
51 | |||
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); | ||
57 | |||
58 | return $client; | ||
59 | } | ||
60 | } | ||