]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
fixup! fixup! Fix documentation wording for matches/notmatches tagging rules
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / HttpClientFactory.php
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 $subscribers = [];
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\Cookie\CookieJar $cookieJar
29 * @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1
30 * @param LoggerInterface $logger
31 */
32 public function __construct(CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger)
33 {
34 $this->cookieJar = $cookieJar;
35 $this->restrictedAccess = $restrictedAccess;
36 $this->logger = $logger;
37 }
38
39 /**
40 * @return \GuzzleHttp\Client|null
41 */
42 public function buildHttpClient()
43 {
44 $this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]);
45
46 if (0 === (int) $this->restrictedAccess) {
47 return;
48 }
49
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]]);
54
55 foreach ($this->subscribers as $subscriber) {
56 $client->getEmitter()->attach($subscriber);
57 }
58
59 return $client;
60 }
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 }
71 }