<?php

namespace Google\Site_Kit_Dependencies\GuzzleHttp\Cookie;

use Google\Site_Kit_Dependencies\GuzzleHttp\Utils;
/**
 * Persists cookies in the client session
 */
class SessionCookieJar extends \Google\Site_Kit_Dependencies\GuzzleHttp\Cookie\CookieJar
{
    /** @var string session key */
    private $sessionKey;
    /**
     * Create a new SessionCookieJar object
     *
     * @param string $sessionKey Session key name to store the cookie data in session
     */
    public function __construct($sessionKey)
    {
        $this->sessionKey = $sessionKey;
        $this->load();
    }
    /**
     * Saves cookies to session when shutting down
     */
    public function __destruct()
    {
        $this->save();
    }
    /**
     * Save cookies to the client session
     */
    public function save()
    {
        $json = [];
        foreach ($this as $cookie) {
            if ($cookie->getExpires() && !$cookie->getDiscard()) {
                $json[] = $cookie->toArray();
            }
        }
        $_SESSION[$this->sessionKey] = \json_encode($json);
    }
    /**
     * Load the contents of the client session into the data array
     */
    protected function load()
    {
        $cookieJar = isset($_SESSION[$this->sessionKey]) ? $_SESSION[$this->sessionKey] : null;
        $data = \Google\Site_Kit_Dependencies\GuzzleHttp\Utils::jsonDecode($cookieJar, \true);
        if (\is_array($data)) {
            foreach ($data as $cookie) {
                $this->setCookie(new \Google\Site_Kit_Dependencies\GuzzleHttp\Cookie\SetCookie($cookie));
            }
        } elseif (\strlen($data)) {
            throw new \RuntimeException("Invalid cookie data");
        }
    }
}
