]>
Commit | Line | Data |
---|---|---|
7aab0ecf BD |
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; | |
7bf6b555 | 9 | use Psr\Log\LoggerInterface; |
7aab0ecf BD |
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 | ||
d64bf795 | 22 | private $restrictedAccess; |
7bf6b555 | 23 | private $logger; |
d64bf795 | 24 | |
7aab0ecf BD |
25 | /** |
26 | * HttpClientFactory constructor. | |
27 | * | |
28 | * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber | |
29 | * @param \GuzzleHttp\Cookie\CookieJar $cookieJar | |
d51093a7 | 30 | * @param string $restrictedAccess this param is a kind of boolean. Values: 0 or 1 |
7bf6b555 | 31 | * @param LoggerInterface $logger |
7aab0ecf | 32 | */ |
7bf6b555 | 33 | public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger) |
7aab0ecf BD |
34 | { |
35 | $this->authenticatorSubscriber = $authenticatorSubscriber; | |
36 | $this->cookieJar = $cookieJar; | |
d64bf795 | 37 | $this->restrictedAccess = $restrictedAccess; |
7bf6b555 | 38 | $this->logger = $logger; |
7aab0ecf BD |
39 | } |
40 | ||
41 | /** | |
d64bf795 | 42 | * @return \GuzzleHttp\Client|null |
7aab0ecf BD |
43 | */ |
44 | public function buildHttpClient() | |
45 | { | |
7bf6b555 JB |
46 | $this->logger->log('debug', 'Restricted access config enabled?', array('enabled' => (int) $this->restrictedAccess)); |
47 | ||
d64bf795 | 48 | if (0 === (int) $this->restrictedAccess) { |
7bf6b555 | 49 | return; |
d64bf795 NL |
50 | } |
51 | ||
40f3ea57 NL |
52 | // we clear the cookie to avoid websites who use cookies for analytics |
53 | $this->cookieJar->clear(); | |
7aab0ecf BD |
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 | } |