Cookies::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Context;
4
5
class Cookies extends Container
6
{
7
8
    /**
9
     * @var Configure
10
     */
11
    protected $configure;
12
13
    /**
14
     * Cookie constructor.
15
     *
16
     * @param null|string $password
17
     * @param Configure   $configure
18
     */
19 8
    public function __construct(?string $password = null, Configure $configure = null)
20
    {
21 8
        parent::__construct($password);
22 8
        $this->store = filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW) ?: [];
23
24 8
        $this->configure = $this->load($configure);
25 8
    }
26
27
    /**
28
     * @return null|string
29
     */
30 1
    public function sessionId(): ?string
31
    {
32 1
        return $this->store['PHPSESSID'] ?? null;
33
    }
34
35
    /**
36
     * @return Configure
37
     */
38 3
    public function configure(): Configure
39
    {
40 3
        return $this->configure;
41
    }
42
43
    /**
44
     * @param Configure $configure
45
     *
46
     * @return Configure
47
     */
48 8
    protected function load(?Configure $configure) : Configure
49
    {
50 8
        if (!$configure)
51
        {
52 8
            $configure = new Configure();
53
        }
54
55 8
        return $configure;
56
    }
57
58
    /**
59
     * @param string         $name
60
     * @param mixed          $data
61
     * @param Configure|null $configure
62
     */
63 8
    public function set(string $name, $data, Configure $configure = null): void
64
    {
65 8
        parent::set($name, $data);
66 8
        $configure = $configure ?: $this->configure;
67 8
        \setcookie($name, $this->store[$name], ...$configure->asArray());
0 ignored issues
show
Bug introduced by
It seems like $configure->asArray() can also be of type boolean and string; however, parameter $expire of setcookie() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        \setcookie($name, $this->store[$name], /** @scrutinizer ignore-type */ ...$configure->asArray());
Loading history...
68 8
    }
69
70
    /**
71
     * @param string $name
72
     */
73 3
    public function remove(string $name): void
74
    {
75 3
        $this->set($name, '', $this->configure()->modify([
76 3
            'expire' => 0
77
        ]));
78
79 3
        parent::remove($name);
80 3
    }
81
82
}
83