SetCookie   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 22
c 5
b 0
f 1
lcom 1
cbo 0
dl 0
loc 162
ccs 69
cts 69
cp 1
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A hasInvalidOptions() 0 6 1
A convertTime() 0 13 2
A isDateTime() 0 4 2
A getName() 0 4 1
A getValue() 0 9 1
A collectFormatedOptions() 0 8 1
A isFinal() 0 4 1
A prepare() 0 8 2
A cleanOptions() 0 18 3
A collectExpireTime() 0 12 2
A collectDomainPathValue() 0 10 2
A collectBooleanOptions() 0 14 3
1
<?php
2
3
namespace Fracture\Http\Headers;
4
5
use Fracture\Http\Cookie;
6
7
class SetCookie implements Abstracted
8
{
9
10
    protected $headerValue = null;
11
    protected $headerName = 'Set-Cookie';
12
13
    private $cookieName = null;
14
    private $cookieValue = null;
15
    private $options = [];
16
17
    private $defaults = [
18
        'expires' => null,
19
        'path' => '/',
20
        'domain' => '',
21
        'secure' => false,
22
        'httpOnly' => true,
23
    ];
24
25
26 15
    public function __construct($name, $value, $options = [])
27
    {
28 15
        $this->cookieName = $name;
29 15
        $this->cookieValue = $value;
30 15
        $this->options = $options;
31 15
    }
32
33
34 17
    public function prepare()
35
    {
36 17
        if ($this->hasInvalidOptions($this->options)) {
37 1
            $message = 'Valid array keys for cookie options are: \'expires\', \'path\', \'domain\', \'secure\' and \'httpOnly\'';
38 1
            trigger_error($message, E_NOTICE);
39
        }
40 16
        $this->options = $this->cleanOptions($this->options);
41 16
    }
42
43
44 11
    private function hasInvalidOptions($options)
45
    {
46 11
        $keys = ['expires', 'path', 'domain', 'secure', 'httpOnly'];
47 11
        $wrongKeys = array_diff(array_keys($options), $keys);
48 11
        return count($wrongKeys) > 0;
49
    }
50
51
52 10
    private function cleanOptions($options)
53
    {
54 10
        $options = $options + $this->defaults;
55
56 10
        if ($options['expires'] !== null) {
57 3
            $options['expires'] = $this->convertTime($options['expires']);
58
        }
59
60 10
        if ($options['path'] === null) {
61 1
            $options['path'] = '/';
62
        }
63
64 10
        $options['domain'] = strtolower($options['domain']);
65 10
        $options['secure'] = (bool) $options['secure'];
66 10
        $options['httpOnly'] = (bool) $options['httpOnly'];
67
68 10
        return $options;
69
    }
70
71
72 6
    private function convertTime($time) {
73 6
        if ($this->isDateTime($time)) {
74 3
            $time->setTimeZone(new \DateTimeZone('GMT'));
75 3
            return $time;
76
        }
77
78 3
        $time = (int) $time;
79
80 3
        $dateTime = new \DateTime;
81 3
        $dateTime->setTimestamp($time);
82 3
        $dateTime->setTimeZone(new \DateTimeZone('GMT'));
83 3
        return $dateTime;
84
    }
85
86
87 6
    private function isDateTime($time)
88
    {
89 6
        return is_object($time) && $time instanceof \DateTime;
90
    }
91
92
93
94
95 1
    public function getName()
96
    {
97 1
        return $this->headerName;
98
    }
99
100
101 13
    public function getValue()
102
    {
103 13
        $name = urlencode($this->cookieName);
104 13
        $value = urlencode($this->cookieValue);
105
106 13
        $result = "{$name}={$value}" . $this->collectFormatedOptions();
107
108 13
        return $result;
109
    }
110
111
112 10
    private function collectFormatedOptions()
113
    {
114 10
        $options  = $this->collectExpireTime($this->options);
115 10
        $options .= $this->collectDomainPathValue($this->options);
116 10
        $options .= $this->collectBooleanOptions($this->options);
117
118 10
        return $options;
119
    }
120
121
122 13
    private function collectExpireTime($options)
123
    {
124 13
        $string = '';
125
126 13
        if ($options['expires'] !== null) {
127 6
            $string = $options['expires']->format(\DateTime::RFC1123);
128 6
            $string =  str_replace('+0000', 'GMT', $string);
129 6
            $string = '; Expires=' . $string;
130
        }
131
132 13
        return $string;
133
    }
134
135
136 10
    private function collectDomainPathValue($options)
137
    {
138 10
        $output = '';
139
140 10
        if ($options['domain'] !== '') {
141 2
            $output .= '; Domain=' . $options['domain'];
142
        }
143
144 10
        return $output . '; Path=' . $options['path'];
145
    }
146
147
148 10
    private function collectBooleanOptions($options)
149
    {
150 10
        $result = '';
151
152 10
        if ($options['secure']) {
153 1
            $result .= '; Secure';
154
        }
155
156 10
        if ($options['httpOnly']) {
157 9
            $result .= '; HttpOnly';
158
        }
159
160 10
        return $result;
161
    }
162
163
164 1
    public function isFinal()
165
    {
166 1
        return true;
167
    }
168
}
169