Passed
Push — master ( 62f043...679ba5 )
by devosc
03:11
created

HttpCookies::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Cookie\Config;
7
8
use Mvc5\Arg;
9
use Mvc5\Cookie\Cookies;
10
11
trait HttpCookies
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $config = [];
17
18
    /**
19
     * @var array
20
     */
21
    protected $defaults = [
22
        Arg::EXPIRE    => 0,
23
        Arg::PATH      => '/',
24
        Arg::DOMAIN    => '',
25
        Arg::SECURE    => false,
26
        Arg::HTTP_ONLY => true
27
    ];
28
29
    /**
30
     * @param array $cookies
31
     * @param array $defaults
32
     */
33 27
    function __construct(array $cookies = [], array $defaults = [])
34
    {
35 27
        $this->config = $cookies;
36 27
        $this->defaults = $defaults + $this->defaults;
37 27
    }
38
39
    /**
40
     * @return array
41
     */
42
    function all() : array
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
43
    {
44
        return $this->config;
45
    }
46
47
    /**
48
     * @param string $name
49 10
     * @param string $value
50
     * @param int|string|null $expire
51
     * @param string|null $path
52
     * @param string|null $domain
53 10
     * @param bool|null $secure
54 10
     * @param bool|null $httponly
55 10
     * @return array
56 10
     */
57 10
    protected function cookie($name, $value, $expire = null,
58 10
                              string $path = null, string $domain = null, bool $secure = null, bool $httponly = null) : array
59 10
    {
60
        return [
61
            Arg::NAME => (string) $name,
62
            Arg::VALUE => (string) $value,
63
            Arg::EXPIRE => is_string($expire ?? $expire = $this->defaults[Arg::EXPIRE]) ? $expire : (int) $expire,
64
            Arg::PATH => $path ?? $this->defaults[Arg::PATH],
65
            Arg::DOMAIN => $domain ?? $this->defaults[Arg::DOMAIN],
66
            Arg::SECURE => $secure ?? $this->defaults[Arg::SECURE],
67
            Arg::HTTP_ONLY => $httponly ?? $this->defaults[Arg::HTTP_ONLY]
68
        ];
69
    }
70 3
71
    /**
72 3
     * @param string $name
73 3
     * @param string|null $path
74
     * @param string|null $domain
75
     * @param bool|null $secure
76
     * @param bool|null $httponly
77
     */
78
    function remove($name, string $path = null, string $domain = null, bool $secure = null, bool $httponly = null) : void
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
79
    {
80
        $this->set($name, '', 946706400, $path, $domain, $secure, $httponly);
81
    }
82
83
    /**
84
     * @param string $name
85 3
     * @param string $value
86
     * @param int|string|null $expire
87
     * @param string|null $path
88 3
     * @param string|null $domain
89 3
     * @param bool|null $secure
90
     * @param bool|null $httponly
91
     * @return mixed
92
     */
93
    function set($name, $value = null, $expire = null,
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
94
                 string $path = null, string $domain = null, bool $secure = null, bool $httponly = null)
95
    {
96
        $this->config[$name] = $this->cookie($name, $value, $expire, $path, $domain, $secure, $httponly);
97
        return $value;
98
    }
99
100
    /**
101
     * @param string $name
102 2
     * @param string|null $value
103
     * @param int|string|null $expire
104
     * @param string|null $path
105 2
     * @param string|null $domain
106 2
     * @param bool|null $secure
107 2
     * @param bool|null $httponly
108
     * @return self|mixed
109
     */
110
    function with($name, $value = null, $expire = null,
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
111
                  string $path = null, string $domain = null, bool $secure = null, bool $httponly = null) : Cookies
112
    {
113
        $new = clone $this;
114
        $new->set($name, $value, $expire, $path, $domain, $secure, $httponly);
115
        return $new;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $new returns the type Mvc5\Cookie\Config\HttpCookies which is incompatible with the type-hinted return Mvc5\Cookie\Cookies.
Loading history...
116
    }
117
118 1
    /**
119
     * @param string $name
120 1
     * @param string|null $path
121 1
     * @param string|null $domain
122 1
     * @param bool|null $secure
123
     * @param bool|null $httponly
124
     * @return self|mixed
125
     */
126
    function without($name, string $path = null, string $domain = null, bool $secure = null, bool $httponly = null) : Cookies
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
127
    {
128
        $new = clone $this;
129
        $new->remove($name, $path, $domain, $secure, $httponly);
130
        return $new;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $new returns the type Mvc5\Cookie\Config\HttpCookies which is incompatible with the type-hinted return Mvc5\Cookie\Cookies.
Loading history...
131
    }
132
}
133