]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Helper/HttpClientFactory.php
Merge pull request #1 from wallabag/master
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / HttpClientFactory.php
index 1ac8feb17b5c0aadb9c7744acca171d0eb47c9af..ea864acbbf6a7471947e4a2e9b7689be6fdcb700 100644 (file)
@@ -2,19 +2,21 @@
 
 namespace Wallabag\CoreBundle\Helper;
 
-use Graby\Ring\Client\SafeCurlHandler;
-use GuzzleHttp\Client;
+use GuzzleHttp\Client as GuzzleClient;
 use GuzzleHttp\Cookie\CookieJar;
 use GuzzleHttp\Event\SubscriberInterface;
+use Http\Adapter\Guzzle5\Client as GuzzleAdapter;
+use Http\Client\HttpClient;
+use Http\HttplugBundle\ClientFactory\ClientFactory;
 use Psr\Log\LoggerInterface;
 
 /**
- * Builds and configures the Guzzle HTTP client.
+ * Builds and configures the HTTP client.
  */
-class HttpClientFactory
+class HttpClientFactory implements ClientFactory
 {
-    /** @var \GuzzleHttp\Event\SubscriberInterface */
-    private $authenticatorSubscriber;
+    /** @var [\GuzzleHttp\Event\SubscriberInterface] */
+    private $subscribers = [];
 
     /** @var \GuzzleHttp\Cookie\CookieJar */
     private $cookieJar;
@@ -25,36 +27,48 @@ class HttpClientFactory
     /**
      * HttpClientFactory constructor.
      *
-     * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber
-     * @param \GuzzleHttp\Cookie\CookieJar          $cookieJar
-     * @param string                                $restrictedAccess        this param is a kind of boolean. Values: 0 or 1
-     * @param LoggerInterface                       $logger
+     * @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1
      */
-    public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger)
+    public function __construct(CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger)
     {
-        $this->authenticatorSubscriber = $authenticatorSubscriber;
         $this->cookieJar = $cookieJar;
         $this->restrictedAccess = $restrictedAccess;
         $this->logger = $logger;
     }
 
     /**
-     * @return \GuzzleHttp\Client|null
+     * Adds a subscriber to the HTTP client.
      */
-    public function buildHttpClient()
+    public function addSubscriber(SubscriberInterface $subscriber)
     {
-        $this->logger->log('debug', 'Restricted access config enabled?', array('enabled' => (int) $this->restrictedAccess));
+        $this->subscribers[] = $subscriber;
+    }
+
+    /**
+     * Input an array of configuration to be able to create a HttpClient.
+     *
+     * @return HttpClient
+     */
+    public function createClient(array $config = [])
+    {
+        $this->logger->log('debug', 'Restricted access config enabled?', ['enabled' => (int) $this->restrictedAccess]);
 
         if (0 === (int) $this->restrictedAccess) {
-            return;
+            return new GuzzleAdapter(new GuzzleClient($config));
         }
 
         // we clear the cookie to avoid websites who use cookies for analytics
         $this->cookieJar->clear();
-        // need to set the (shared) cookie jar
-        $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]);
-        $client->getEmitter()->attach($this->authenticatorSubscriber);
+        if (!isset($config['defaults']['cookies'])) {
+            // need to set the (shared) cookie jar
+            $config['defaults']['cookies'] = $this->cookieJar;
+        }
+
+        $guzzle = new GuzzleClient($config);
+        foreach ($this->subscribers as $subscriber) {
+            $guzzle->getEmitter()->attach($subscriber);
+        }
 
-        return $client;
+        return new GuzzleAdapter($guzzle);
     }
 }