Passed
Push — master ( 0b5a84...abcc5c )
by Vsevolods
03:17
created

CookieJar::expirationToDateTime()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 10.4218

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 17
cp 0.5881
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 11
nc 7
nop 1
crap 10.4218
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Http;
4
5
use DateInterval;
6
use DateTime;
7
use DateTimeImmutable;
8
use DateTimeInterface;
9
use InvalidArgumentException;
10
use Venta\Contracts\Http\Cookie as CookieContract;
11
use Venta\Contracts\Http\CookieJar as CookieJarContract;
12
13
/**
14
 * Class CookieJar
15
 *
16
 * @package Venta\Http
17
 */
18
final class CookieJar implements CookieJarContract
19
{
20
21
    /**
22
     * @var CookieContract[]
23
     */
24
    private $cookies = [];
25
26
    /**
27
     * @inheritDoc
28
     */
29 4
    public function add(
30
        string $name,
31
        string $value,
32
        $expiration,
33
        string $path = '',
34
        string $domain = '',
35
        bool $secure = false,
36
        bool $httpOnly = false
37
    ) {
38 4
        $expiration = $this->expirationToDateTime($expiration);
39 3
        $this->put(new Cookie($name, $value, $expiration, $path, $domain, $secure, $httpOnly));
40 3
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45 1
    public function all(): array
46
    {
47 1
        return $this->cookies;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53 5
    public function findByName(string $name)
54
    {
55 5
        return $this->cookies[$name] ?? null;
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61 1
    public function forever(
62
        string $name,
63
        string $value = null,
64
        string $path = '',
65
        string $domain = '',
66
        bool $secure = false,
67
        bool $httpOnly = false
68
    ) {
69 1
        $this->add($name, $value, (new DateTime())->add(new DateInterval('P10Y')), $path, $domain, $secure, $httpOnly);
70 1
    }
71
72
    /**
73
     * @inheritDoc
74
     */
75 1
    public function forget(string $name)
76
    {
77 1
        $this->add($name, '', (new DateTime())->setTimestamp(1));
78 1
    }
79
80
    /**
81
     * @inheritDoc
82
     */
83 6
    public function put(CookieContract $cookie)
84
    {
85 6
        $this->cookies[$cookie->name()] = $cookie;
86 6
    }
87
88
    /**
89
     * @inheritDoc
90
     */
91 1
    public function session(
92
        string $name,
93
        string $value,
94
        string $path = '',
95
        string $domain = '',
96
        bool $secure = false,
97
        bool $httpOnly = false
98
    ) {
99 1
        $this->put(new Cookie($name, $value, null, $path, $domain, $secure, $httpOnly));
100 1
    }
101
102
    /**
103
     * Parses expiration time and returns valid DateTimeInterface implementation.
104
     *
105
     * @param DateTimeInterface|DateInterval|string $expires
106
     * @return DateTimeInterface
107
     * @throws InvalidArgumentException
108
     */
109 4
    private function expirationToDateTime($expires): DateTimeInterface
110
    {
111 4
        if ($expires instanceof DateTimeImmutable) {
112
            return $expires;
113
        }
114
115 4
        if ($expires instanceof DateInterval) {
116 1
            $expires = (new DateTime)->add($expires);
117 4
        } elseif (is_string($expires) || is_int($expires)) {
118 2
            $expires = new DateTime(is_numeric($expires) ? "@$expires" : $expires);
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $expires instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
119
        }
120
121 4
        if (!$expires instanceof DateTimeInterface) {
122 1
            throw new InvalidArgumentException(
123 1
                "Invalid cookie expiration time. Cannot be converted to DateTimeInterface."
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Invalid cookie expiratio...d to DateTimeInterface. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
124
            );
125
        }
126
127 3
        return new DateTimeImmutable($expires->format(DateTime::ISO8601), $expires->getTimezone());
128
    }
129
130
}