aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/security/CookieManager.php
blob: cde4746e864d4d3991a983fc50a7c76a8609a675 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

declare(strict_types=1);

namespace Shaarli\Security;

class CookieManager
{
    /** @var string Name of the cookie set after logging in **/
    public const STAY_SIGNED_IN = 'shaarli_staySignedIn';

    /** @var mixed $_COOKIE set by reference */
    protected $cookies;

    public function __construct(array &$cookies)
    {
        $this->cookies = $cookies;
    }

    public function setCookieParameter(string $key, string $value, int $expires, string $path): self
    {
        $this->cookies[$key] = $value;

        setcookie($key, $value, $expires, $path);

        return $this;
    }

    public function getCookieParameter(string $key, string $default = null): ?string
    {
        return $this->cookies[$key] ?? $default;
    }
}