CookieLogin::addCookie()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 3
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\User\Login\Cookie;
6
7
use DateInterval;
8
use DateTimeImmutable;
9
use JsonException;
10
use Psr\Http\Message\ResponseInterface;
11
use Yiisoft\Cookies\Cookie;
12
13
use function json_encode;
14
15
/**
16
 * The service is used to send or remove auto-login cookie.
17
 *
18
 * @see CookieLoginIdentityInterface
19
 * @see CookieLoginMiddleware
20
 */
21
final class CookieLogin
22
{
23
    private string $cookieName = 'autoLogin';
24
25
    /**
26
     * @param DateInterval|null $duration Interval until the auto-login cookie expires. If it isn't set it means
27
     * the auto-login cookie is session cookie that expires when browser is closed.
28
     */
29 27
    public function __construct(private ?DateInterval $duration = null)
30
    {
31 27
    }
32
33
    /**
34
     * Returns a new instance with the specified auto-login cookie name.
35
     *
36
     * @param string $name The auto-login cookie name.
37
     */
38 5
    public function withCookieName(string $name): self
39
    {
40 5
        $new = clone $this;
41 5
        $new->cookieName = $name;
42 5
        return $new;
43
    }
44
45
    /**
46
     * Adds auto-login cookie to response so the user is logged in automatically based on cookie even if session
47
     * is expired.
48
     *
49
     * @param CookieLoginIdentityInterface $identity The cookie login identity instance.
50
     * @param ResponseInterface $response Response for adding auto-login cookie.
51
     * @param DateInterval|false|null $duration Interval until the auto-login cookie expires. If it is null it means
52
     * the auto-login cookie is session cookie that expires when browser is closed. If it is false (by default) will be
53
     * used default value of duration.
54
     *
55
     * @throws JsonException If an error occurs during JSON encoding of the cookie value.
56
     *
57
     * @return ResponseInterface Response with added auto-login cookie.
58
     */
59 9
    public function addCookie(
60
        CookieLoginIdentityInterface $identity,
61
        ResponseInterface $response,
62
        DateInterval|null|false $duration = false,
63
    ): ResponseInterface {
64 9
        $duration = $duration === false ? $this->duration : $duration;
65
66 9
        $data = [$identity->getId(), $identity->getCookieLoginKey()];
67
68 9
        if ($duration !== null) {
69 7
            $expires = (new DateTimeImmutable())->add($duration);
70 7
            $data[] = $expires->getTimestamp();
71
        } else {
72 2
            $expires = null;
73 2
            $data[] = 0;
74
        }
75
76 9
        $cookieValue = json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
77
78 9
        return (new Cookie(name: $this->cookieName, value: $cookieValue, expires: $expires))->addToResponse($response);
79
    }
80
81
    /**
82
     * Expires auto-login cookie so user is not logged in automatically anymore.
83
     *
84
     * @param ResponseInterface $response Response for adding auto-login cookie.
85
     *
86
     * @return ResponseInterface Response with added auto-login cookie.
87
     */
88 3
    public function expireCookie(ResponseInterface $response): ResponseInterface
89
    {
90 3
        return (new Cookie($this->cookieName))
91 3
            ->expire()
92 3
            ->addToResponse($response);
93
    }
94
95
    /**
96
     * Returns the auto-login cookie name.
97
     *
98
     * @return string The auto-login cookie name.
99
     */
100 17
    public function getCookieName(): string
101
    {
102 17
        return $this->cookieName;
103
    }
104
}
105