3 namespace Wallabag\CoreBundle\Helper
;
5 use GuzzleHttp\Cookie\FileCookieJar
as BaseFileCookieJar
;
6 use GuzzleHttp\Cookie\SetCookie
;
8 use Psr\Log\LoggerInterface
;
11 * Overidden Cookie behavior to:
12 * - ignore error when the cookie file is malformatted (resulting in clearing it).
14 class FileCookieJar
extends BaseFileCookieJar
19 * @param LoggerInterface $logger Only used to log info when something goes wrong
20 * @param string $cookieFile File to store the cookie data
22 public function __construct(LoggerInterface
$logger, $cookieFile)
24 parent
::__construct($cookieFile);
26 $this->logger
= $logger;
30 * Load cookies from a JSON formatted file.
32 * Old cookies are kept unless overwritten by newly loaded ones.
34 * @param string $filename cookie file to load
36 * @throws \RuntimeException if the file cannot be loaded
38 public function load($filename)
40 $json = file_get_contents($filename);
41 if (false === $json) {
42 // @codeCoverageIgnoreStart
43 throw new \
RuntimeException("Unable to load file {$filename}");
44 // @codeCoverageIgnoreEnd
48 $data = Utils
::jsonDecode($json, true);
49 } catch (\InvalidArgumentException
$e) {
50 $this->logger
->error('JSON inside the cookie is broken', [
52 'error_msg' => $e->getMessage(),
55 // cookie file is invalid, just ignore the exception and it'll reset the whole cookie file
59 if (\
is_array($data)) {
60 foreach (Utils
::jsonDecode($json, true) as $cookie) {
61 $this->setCookie(new SetCookie($cookie));
63 } elseif (\
strlen($data)) {
64 throw new \
RuntimeException("Invalid cookie file: {$filename}");