]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Helper/FileCookieJar.php
Cleanup cookie jar
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / FileCookieJar.php
index 52c7f5deb6aa080ddf56806e1cf006e91205897c..4b4dca10fc92aa6de4e8773d4342097bd341285f 100644 (file)
@@ -5,35 +5,25 @@ namespace Wallabag\CoreBundle\Helper;
 use GuzzleHttp\Cookie\FileCookieJar as BaseFileCookieJar;
 use GuzzleHttp\Cookie\SetCookie;
 use GuzzleHttp\Utils;
+use Psr\Log\LoggerInterface;
 
 /**
  * Overidden Cookie behavior to:
- *     - fix multiple concurrent writes (see https://github.com/guzzle/guzzle/pull/1884)
  *     - ignore error when the cookie file is malformatted (resulting in clearing it).
  */
 class FileCookieJar extends BaseFileCookieJar
 {
+    private $logger;
+
     /**
-     * Saves the cookies to a file.
-     *
-     * @param string $filename File to save
-     *
-     * @throws \RuntimeException if the file cannot be found or created
+     * @param LoggerInterface $logger     Only used to log info when something goes wrong
+     * @param string          $cookieFile File to store the cookie data
      */
-    public function save($filename)
+    public function __construct(LoggerInterface $logger, $cookieFile)
     {
-        $json = [];
-        foreach ($this as $cookie) {
-            if ($cookie->getExpires() && !$cookie->getDiscard()) {
-                $json[] = $cookie->toArray();
-            }
-        }
+        parent::__construct($cookieFile);
 
-        if (false === file_put_contents($filename, json_encode($json), LOCK_EX)) {
-            // @codeCoverageIgnoreStart
-            throw new \RuntimeException("Unable to save file {$filename}");
-            // @codeCoverageIgnoreEnd
-        }
+        $this->logger = $logger;
     }
 
     /**
@@ -57,6 +47,11 @@ class FileCookieJar extends BaseFileCookieJar
         try {
             $data = Utils::jsonDecode($json, true);
         } catch (\InvalidArgumentException $e) {
+            $this->logger->error('JSON inside the cookie is broken', [
+                'json' => $json,
+                'error_msg' => $e->getMessage(),
+            ]);
+
             // cookie file is invalid, just ignore the exception and it'll reset the whole cookie file
             $data = '';
         }