]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
Use httplug
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / HttpClientFactory.php
CommitLineData
7aab0ecf
BD
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
bf9ace06 5use GuzzleHttp\Client as GuzzleClient;
7aab0ecf
BD
6use GuzzleHttp\Cookie\CookieJar;
7use GuzzleHttp\Event\SubscriberInterface;
bf9ace06 8use Http\Adapter\Guzzle5\Client as GuzzleAdapter;
7bf6b555 9use Psr\Log\LoggerInterface;
bf9ace06 10use Http\Client\HttpClient;
11use Http\HttplugBundle\ClientFactory\ClientFactory;
7aab0ecf
BD
12
13/**
bf9ace06 14 * Builds and configures the HTTP client.
7aab0ecf 15 */
bf9ace06 16class HttpClientFactory implements ClientFactory
7aab0ecf 17{
5b914b04
BD
18 /** @var [\GuzzleHttp\Event\SubscriberInterface] */
19 private $subscribers = [];
7aab0ecf
BD
20
21 /** @var \GuzzleHttp\Cookie\CookieJar */
22 private $cookieJar;
23
d64bf795 24 private $restrictedAccess;
7bf6b555 25 private $logger;
d64bf795 26
7aab0ecf
BD
27 /**
28 * HttpClientFactory constructor.
29 *
5b914b04
BD
30 * @param \GuzzleHttp\Cookie\CookieJar $cookieJar
31 * @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1
d047530d 32 * @param LoggerInterface $logger
7aab0ecf 33 */
5b914b04 34 public function __construct(CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger)
7aab0ecf 35 {
7aab0ecf 36 $this->cookieJar = $cookieJar;
d64bf795 37 $this->restrictedAccess = $restrictedAccess;
7bf6b555 38 $this->logger = $logger;
7aab0ecf
BD
39 }
40
41 /**
bf9ace06 42 * Adds a subscriber to the HTTP client.
43 *
44 * @param SubscriberInterface $subscriber
45 */
46 public function addSubscriber(SubscriberInterface $subscriber)
47 {
48 $this->subscribers[] = $subscriber;
49 }
50
51 /**
52 * Input an array of configuration to be able to create a HttpClient.
53 *
54 * @param array $config
55 *
56 * @return HttpClient
7aab0ecf 57 */
bf9ace06 58 public function createClient(array $config = [])
7aab0ecf 59 {
f808b016 60 $this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]);
7bf6b555 61
d64bf795 62 if (0 === (int) $this->restrictedAccess) {
bf9ace06 63 return new GuzzleAdapter(new GuzzleClient());
d64bf795
NL
64 }
65
40f3ea57
NL
66 // we clear the cookie to avoid websites who use cookies for analytics
67 $this->cookieJar->clear();
7aab0ecf 68 // need to set the (shared) cookie jar
bf9ace06 69 $guzzle = new GuzzleClient(['defaults' => ['cookies' => $this->cookieJar]]);
5b914b04 70 foreach ($this->subscribers as $subscriber) {
bf9ace06 71 $guzzle->getEmitter()->attach($subscriber);
5b914b04 72 }
7aab0ecf 73
bf9ace06 74 return new GuzzleAdapter($guzzle);
5b914b04 75 }
7aab0ecf 76}