CookieJar::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 12
cts 12
cp 1
rs 9.584
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\Cookies;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Application\Exceptions\InvalidArgumentException;
22
use Limoncello\Contracts\Cookies\CookieInterface;
23
use Limoncello\Contracts\Cookies\CookieJarInterface;
24
use function array_key_exists;
25
26
/**
27
 * @package Limoncello\Application
28
 */
29
class CookieJar implements CookieJarInterface
30
{
31
    /**
32
     * @var CookieInterface[]
33
     */
34
    private $cookies;
35
36
    /**
37
     * @var string
38
     */
39
    private $defaultPath;
40
41
    /**
42
     * @var string
43
     */
44
    private $defaultDomain;
45
46
    /**
47
     * @var bool
48
     */
49
    private $defaultIsSecure;
50
51
    /**
52
     * @var bool
53
     */
54
    private $defaultIsHttpOnly;
55
56
    /**
57
     * @var bool
58
     */
59
    private $defaultIsRaw;
60
61
    /**
62
     * @param string $defaultPath
63
     * @param string $defaultDomain
64
     * @param bool   $defaultIsSecure
65 3
     * @param bool   $defaultIsHttpOnly
66
     * @param bool   $defaultIsRaw
67
     */
68
    public function __construct(
69
        string $defaultPath,
70
        string $defaultDomain,
71
        bool $defaultIsSecure,
72 3
        bool $defaultIsHttpOnly,
73 3
        bool $defaultIsRaw
74 3
    ) {
75 3
        $this->cookies           = [];
76 3
        $this->defaultPath       = $defaultPath;
77 3
        $this->defaultDomain     = $defaultDomain;
78
        $this->defaultIsSecure   = $defaultIsSecure;
79
        $this->defaultIsHttpOnly = $defaultIsHttpOnly;
80
        $this->defaultIsRaw      = $defaultIsRaw;
81
    }
82
83
84 2
    /**
85
     * @inheritdoc
86 2
     */
87 1
    public function create(string $cookieName): CookieInterface
88
    {
89
        if ($this->has($cookieName) === true) {
90 2
            throw new InvalidArgumentException($cookieName);
91 2
        }
92 2
93 2
        $cookie = new Cookie(
94 2
            $cookieName,
95 2
            '',
96 2
            0,
97 2
            $this->getDefaultPath(),
98 2
            $this->getDefaultDomain(),
99
            $this->getDefaultIsSecure(),
100
            $this->getDefaultIsHttpOnly(),
101 2
            $this->getDefaultIsRaw()
102
        );
103 2
104
        $this->cookies[$cookieName] = $cookie;
105
106
        return $cookie;
107
    }
108
109 2
    /**
110
     * @inheritdoc
111 2
     */
112
    public function has(string $cookieName): bool
113
    {
114
        return array_key_exists($cookieName, $this->cookies);
115
    }
116
117 1
    /**
118
     * @inheritdoc
119 1
     */
120
    public function get(string $cookieName): CookieInterface
121
    {
122
        return $this->cookies[$cookieName];
123
    }
124
125 1
    /**
126
     * @inheritdoc
127 1
     */
128
    public function delete(string $cookieName): CookieJarInterface
129 1
    {
130
        unset($this->cookies[$cookieName]);
131
132
        return $this;
133
    }
134
135 2
    /**
136
     * @inheritdoc
137
     */
138 2
    public function getAll(): iterable
139
    {
140 2
        /** @var iterable $result */
141
        $result = $this->cookies;
142
143
        return $result;
144
    }
145
146 2
    /**
147
     * @return string
148 2
     */
149
    protected function getDefaultPath(): string
150
    {
151
        return $this->defaultPath;
152
    }
153
154 2
    /**
155
     * @return string
156 2
     */
157
    protected function getDefaultDomain(): string
158
    {
159
        return $this->defaultDomain;
160
    }
161
162
    /**
163
     * @return bool
164 2
     *
165
     * @SuppressWarnings(PHPMD.BooleanGetMethodName)
166 2
     */
167
    protected function getDefaultIsSecure(): bool
168
    {
169
        return $this->defaultIsSecure;
170
    }
171
172
    /**
173
     * @return bool
174 2
     *
175
     * @SuppressWarnings(PHPMD.BooleanGetMethodName)
176 2
     */
177
    protected function getDefaultIsHttpOnly(): bool
178
    {
179
        return $this->defaultIsHttpOnly;
180
    }
181
182
    /**
183
     * @return bool
184 2
     *
185
     * @SuppressWarnings(PHPMD.BooleanGetMethodName)
186 2
     */
187
    protected function getDefaultIsRaw(): bool
188
    {
189
        return $this->defaultIsRaw;
190
    }
191
}
192