Completed
Push — develop ( ea9a36...1bc5bd )
by Neomerx
05:01
created

CookieJar::getDefaultIsRaw()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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