Completed
Push — master ( 752264...caff68 )
by Mārtiņš
7s
created

SetCookie   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 98.67%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 22
c 4
b 0
f 1
lcom 1
cbo 1
dl 0
loc 160
ccs 74
cts 75
cp 0.9867
rs 10

13 Methods

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