]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
Add a real configuration for CS-Fixer
[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;
7bf6b555 9use Psr\Log\LoggerInterface;
7aab0ecf
BD
10
11/**
12 * Builds and configures the Guzzle HTTP client.
13 */
14class HttpClientFactory
15{
5b914b04
BD
16 /** @var [\GuzzleHttp\Event\SubscriberInterface] */
17 private $subscribers = [];
7aab0ecf
BD
18
19 /** @var \GuzzleHttp\Cookie\CookieJar */
20 private $cookieJar;
21
d64bf795 22 private $restrictedAccess;
7bf6b555 23 private $logger;
d64bf795 24
7aab0ecf
BD
25 /**
26 * HttpClientFactory constructor.
27 *
5b914b04
BD
28 * @param \GuzzleHttp\Cookie\CookieJar $cookieJar
29 * @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1
d047530d 30 * @param LoggerInterface $logger
7aab0ecf 31 */
5b914b04 32 public function __construct(CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger)
7aab0ecf 33 {
7aab0ecf 34 $this->cookieJar = $cookieJar;
d64bf795 35 $this->restrictedAccess = $restrictedAccess;
7bf6b555 36 $this->logger = $logger;
7aab0ecf
BD
37 }
38
39 /**
d64bf795 40 * @return \GuzzleHttp\Client|null
7aab0ecf
BD
41 */
42 public function buildHttpClient()
43 {
f808b016 44 $this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]);
7bf6b555 45
d64bf795 46 if (0 === (int) $this->restrictedAccess) {
7bf6b555 47 return;
d64bf795
NL
48 }
49
40f3ea57
NL
50 // we clear the cookie to avoid websites who use cookies for analytics
51 $this->cookieJar->clear();
7aab0ecf
BD
52 // need to set the (shared) cookie jar
53 $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]);
94b232bb 54
5b914b04
BD
55 foreach ($this->subscribers as $subscriber) {
56 $client->getEmitter()->attach($subscriber);
57 }
7aab0ecf
BD
58
59 return $client;
60 }
5b914b04
BD
61
62 /**
63 * Adds a subscriber to the HTTP client.
64 *
65 * @param SubscriberInterface $subscriber
66 */
67 public function addSubscriber(SubscriberInterface $subscriber)
68 {
69 $this->subscribers[] = $subscriber;
70 }
7aab0ecf 71}