← All changes
|
includes/sdk/s3/GuzzleHttp/Cookie/FileCookieJar.php
+22
-14
1.2.2
→
1.4.2
View file →
| @@ -1,27 +1,32 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Dudlewebs\WPMCS\s3\GuzzleHttp\Cookie; |
| 4 | 4 | |
| 5 | +use Dudlewebs\WPMCS\s3\GuzzleHttp\Utils; | |
| 5 | 6 | /** |
| 6 | 7 | * Persists non-session cookies using a JSON formatted file |
| 7 | 8 | */ |
| 8 | 9 | class FileCookieJar extends CookieJar |
| 9 | 10 | { |
| 10 | - /** @var string filename */ | |
| 11 | + /** | |
| 12 | + * @var string filename | |
| 13 | + */ | |
| 11 | 14 | private $filename; |
| 12 | - /** @var bool Control whether to persist session cookies or not. */ | |
| 15 | + /** | |
| 16 | + * @var bool Control whether to persist session cookies or not. | |
| 17 | + */ | |
| 13 | 18 | private $storeSessionCookies; |
| 14 | 19 | /** |
| 15 | 20 | * Create a new FileCookieJar object |
| 16 | 21 | * |
| 17 | - * @param string $cookieFile File to store the cookie data | |
| 18 | - * @param bool $storeSessionCookies Set to true to store session cookies | |
| 19 | - * in the cookie jar. | |
| 22 | + * @param string $cookieFile File to store the cookie data | |
| 23 | + * @param bool $storeSessionCookies Set to true to store session cookies | |
| 24 | + * in the cookie jar. | |
| 20 | 25 | * |
| 21 | 26 | * @throws \RuntimeException if the file cannot be found or created |
| 22 | 27 | */ |
| 23 | - public function __construct($cookieFile, $storeSessionCookies = \false) | |
| 28 | + public function __construct(string $cookieFile, bool $storeSessionCookies = \false) | |
| 24 | 29 | { |
| 25 | 30 | parent::__construct(); |
| 26 | 31 | $this->filename = $cookieFile; |
| 27 | 32 | $this->storeSessionCookies = $storeSessionCookies; |
| @@ -39,20 +44,21 @@ | ||
| 39 | 44 | /** |
| 40 | 45 | * Saves the cookies to a file. |
| 41 | 46 | * |
| 42 | 47 | * @param string $filename File to save |
| 48 | + * | |
| 43 | 49 | * @throws \RuntimeException if the file cannot be found or created |
| 44 | 50 | */ |
| 45 | - public function save($filename) | |
| 51 | + public function save(string $filename) : void | |
| 46 | 52 | { |
| 47 | 53 | $json = []; |
| 54 | + /** @var SetCookie $cookie */ | |
| 48 | 55 | foreach ($this as $cookie) { |
| 49 | - /** @var SetCookie $cookie */ | |
| 50 | 56 | if (CookieJar::shouldPersist($cookie, $this->storeSessionCookies)) { |
| 51 | 57 | $json[] = $cookie->toArray(); |
| 52 | 58 | } |
| 53 | 59 | } |
| 54 | - $jsonStr = \Dudlewebs\WPMCS\s3\GuzzleHttp\json_encode($json); | |
| 60 | + $jsonStr = Utils::jsonEncode($json); | |
| 55 | 61 | if (\false === \file_put_contents($filename, $jsonStr, \LOCK_EX)) { |
| 56 | 62 | throw new \RuntimeException("Unable to save file {$filename}"); |
| 57 | 63 | } |
| 58 | 64 | } |
| @@ -61,24 +67,26 @@ | ||
| 61 | 67 | * |
| 62 | 68 | * Old cookies are kept unless overwritten by newly loaded ones. |
| 63 | 69 | * |
| 64 | 70 | * @param string $filename Cookie file to load. |
| 71 | + * | |
| 65 | 72 | * @throws \RuntimeException if the file cannot be loaded. |
| 66 | 73 | */ |
| 67 | - public function load($filename) | |
| 74 | + public function load(string $filename) : void | |
| 68 | 75 | { |
| 69 | 76 | $json = \file_get_contents($filename); |
| 70 | 77 | if (\false === $json) { |
| 71 | 78 | throw new \RuntimeException("Unable to load file {$filename}"); |
| 72 | - } elseif ($json === '') { | |
| 79 | + } | |
| 80 | + if ($json === '') { | |
| 73 | 81 | return; |
| 74 | 82 | } |
| 75 | - $data = \Dudlewebs\WPMCS\s3\GuzzleHttp\json_decode($json, \true); | |
| 83 | + $data = Utils::jsonDecode($json, \true); | |
| 76 | 84 | if (\is_array($data)) { |
| 77 | - foreach (\json_decode($json, \true) as $cookie) { | |
| 85 | + foreach ($data as $cookie) { | |
| 78 | 86 | $this->setCookie(new SetCookie($cookie)); |
| 79 | 87 | } |
| 80 | - } elseif (\strlen($data)) { | |
| 88 | + } elseif (\is_scalar($data) && !empty($data)) { | |
| 81 | 89 | throw new \RuntimeException("Invalid cookie file: {$filename}"); |
| 82 | 90 | } |
| 83 | 91 | } |
| 84 | 92 | } |