aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
diff options
context:
space:
mode:
authoradev <adev2000@gmail.com>2017-10-24 22:55:40 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2019-05-28 11:40:41 +0200
commitbf9ace0643f654e7ccd9c020b8b501ad56cd19de (patch)
tree4ff47ca915eb326307127b1b4caf69beda4036e3 /src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
parent92a66835624acf6fd14f5adc5f8aab399658592e (diff)
downloadwallabag-bf9ace0643f654e7ccd9c020b8b501ad56cd19de.tar.gz
wallabag-bf9ace0643f654e7ccd9c020b8b501ad56cd19de.tar.zst
wallabag-bf9ace0643f654e7ccd9c020b8b501ad56cd19de.zip
Use httplug
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/HttpClientFactory.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/HttpClientFactory.php47
1 files changed, 26 insertions, 21 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
index 4602a684..4899d3d4 100644
--- a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
+++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
@@ -2,16 +2,18 @@
2 2
3namespace Wallabag\CoreBundle\Helper; 3namespace Wallabag\CoreBundle\Helper;
4 4
5use Graby\Ring\Client\SafeCurlHandler; 5use GuzzleHttp\Client as GuzzleClient;
6use GuzzleHttp\Client;
7use GuzzleHttp\Cookie\CookieJar; 6use GuzzleHttp\Cookie\CookieJar;
8use GuzzleHttp\Event\SubscriberInterface; 7use GuzzleHttp\Event\SubscriberInterface;
8use Http\Adapter\Guzzle5\Client as GuzzleAdapter;
9use Psr\Log\LoggerInterface; 9use Psr\Log\LoggerInterface;
10use Http\Client\HttpClient;
11use Http\HttplugBundle\ClientFactory\ClientFactory;
10 12
11/** 13/**
12 * Builds and configures the Guzzle HTTP client. 14 * Builds and configures the HTTP client.
13 */ 15 */
14class HttpClientFactory 16class HttpClientFactory implements ClientFactory
15{ 17{
16 /** @var [\GuzzleHttp\Event\SubscriberInterface] */ 18 /** @var [\GuzzleHttp\Event\SubscriberInterface] */
17 private $subscribers = []; 19 private $subscribers = [];
@@ -37,35 +39,38 @@ class HttpClientFactory
37 } 39 }
38 40
39 /** 41 /**
40 * @return \GuzzleHttp\Client|null 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
41 */ 57 */
42 public function buildHttpClient() 58 public function createClient(array $config = [])
43 { 59 {
44 $this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]); 60 $this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]);
45 61
46 if (0 === (int) $this->restrictedAccess) { 62 if (0 === (int) $this->restrictedAccess) {
47 return; 63 return new GuzzleAdapter(new GuzzleClient());
48 } 64 }
49 65
50 // we clear the cookie to avoid websites who use cookies for analytics 66 // we clear the cookie to avoid websites who use cookies for analytics
51 $this->cookieJar->clear(); 67 $this->cookieJar->clear();
52 // need to set the (shared) cookie jar 68 // need to set the (shared) cookie jar
53 $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); 69 $guzzle = new GuzzleClient(['defaults' => ['cookies' => $this->cookieJar]]);
54
55 foreach ($this->subscribers as $subscriber) { 70 foreach ($this->subscribers as $subscriber) {
56 $client->getEmitter()->attach($subscriber); 71 $guzzle->getEmitter()->attach($subscriber);
57 } 72 }
58 73
59 return $client; 74 return new GuzzleAdapter($guzzle);
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 } 75 }
71} 76}