]>
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; | |
9 | ||
10 | /** | |
11 | * Builds and configures the Guzzle HTTP client. | |
12 | */ | |
13 | class HttpClientFactory | |
14 | { | |
15 | /** @var \GuzzleHttp\Event\SubscriberInterface */ | |
16 | private $authenticatorSubscriber; | |
17 | ||
18 | /** @var \GuzzleHttp\Cookie\CookieJar */ | |
19 | private $cookieJar; | |
20 | ||
21 | /** | |
22 | * HttpClientFactory constructor. | |
23 | * | |
24 | * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber | |
25 | * @param \GuzzleHttp\Cookie\CookieJar $cookieJar | |
26 | */ | |
27 | public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar) | |
28 | { | |
29 | $this->authenticatorSubscriber = $authenticatorSubscriber; | |
30 | $this->cookieJar = $cookieJar; | |
31 | } | |
32 | ||
33 | /** | |
34 | * @return \GuzzleHttp\Client | |
35 | */ | |
36 | public function buildHttpClient() | |
37 | { | |
40f3ea57 NL |
38 | // we clear the cookie to avoid websites who use cookies for analytics |
39 | $this->cookieJar->clear(); | |
7aab0ecf BD |
40 | // need to set the (shared) cookie jar |
41 | $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); | |
42 | $client->getEmitter()->attach($this->authenticatorSubscriber); | |
43 | ||
44 | return $client; | |
45 | } | |
46 | } |