1 | <?php |
||
15 | class CookiePlugin implements Plugin |
||
16 | { |
||
17 | /** |
||
18 | * Cookie storage. |
||
19 | * |
||
20 | * @var CookieJar |
||
21 | */ |
||
22 | private $cookieJar; |
||
23 | |||
24 | /** |
||
25 | * @param CookieJar $cookieJar |
||
26 | */ |
||
27 | 9 | public function __construct(CookieJar $cookieJar) |
|
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | 7 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
81 | |||
82 | /** |
||
83 | * Creates a cookie from a string. |
||
84 | * |
||
85 | * @param RequestInterface $request |
||
86 | * @param $setCookie |
||
87 | * |
||
88 | * @return Cookie|null |
||
89 | */ |
||
90 | 1 | private function createCookie(RequestInterface $request, $setCookie) |
|
91 | { |
||
92 | 1 | $parts = array_map('trim', explode(';', $setCookie)); |
|
93 | |||
94 | 1 | if (empty($parts) || !strpos($parts[0], '=')) { |
|
95 | return; |
||
96 | } |
||
97 | |||
98 | 1 | list($name, $cookieValue) = $this->createValueKey(array_shift($parts)); |
|
99 | |||
100 | 1 | $maxAge = null; |
|
101 | 1 | $expires = null; |
|
102 | 1 | $domain = $request->getUri()->getHost(); |
|
103 | 1 | $path = $request->getUri()->getPath(); |
|
104 | 1 | $secure = false; |
|
105 | 1 | $httpOnly = false; |
|
106 | |||
107 | // Add the cookie pieces into the parsed data array |
||
108 | 1 | foreach ($parts as $part) { |
|
109 | 1 | list($key, $value) = $this->createValueKey($part); |
|
110 | |||
111 | 1 | switch (strtolower($key)) { |
|
112 | 1 | case 'expires': |
|
113 | 1 | $expires = \DateTime::createFromFormat(\DateTime::COOKIE, $value); |
|
114 | 1 | break; |
|
115 | |||
116 | 1 | case 'max-age': |
|
117 | 1 | $maxAge = (int) $value; |
|
118 | 1 | break; |
|
119 | |||
120 | 1 | case 'domain': |
|
121 | 1 | $domain = $value; |
|
122 | 1 | break; |
|
123 | |||
124 | 1 | case 'path': |
|
125 | 1 | $path = $value; |
|
126 | 1 | break; |
|
127 | |||
128 | 1 | case 'secure': |
|
129 | 1 | $secure = true; |
|
130 | 1 | break; |
|
131 | |||
132 | 1 | case 'httponly': |
|
133 | 1 | $httpOnly = true; |
|
134 | 1 | break; |
|
135 | 1 | } |
|
136 | 1 | } |
|
137 | |||
138 | 1 | return new Cookie($name, $cookieValue, $maxAge, $domain, $path, $secure, $httpOnly, $expires); |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Separates key/value pair from cookie. |
||
143 | * |
||
144 | * @param $part |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | 1 | private function createValueKey($part) |
|
156 | } |
||
157 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.