Passed
Push — master ( c65e58...1194b0 )
by Oss
02:24
created

CookieList::initList()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 15
loc 15
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 10
nc 4
nop 0
crap 4
1
<?php
2
/**
3
 * @category    Brownie/HttpClient
4
 * @author      Brownie <[email protected]>
5
 * @license     http://www.gnu.org/copyleft/lesser.html
6
 */
7
8
namespace Brownie\HttpClient\Cookie;
9
10
use Brownie\HttpClient\HeadList;
11
12
class CookieList extends HeadList
13
{
14
15
    /**
16
     * Initializing the cookies container.
17
     */
18 3 View Code Duplication
    protected function initList()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20 3
        $cookieList = array();
21 3
        foreach (explode("\r\n", trim($this->getInitData())) as $headerLine) {
22 3
            if (empty($headerLine)) {
23 2
                $cookieList = array();
24 2
                continue;
25
            }
26 2
            if (!$this->isHeaderSetCookie($headerLine)) {
27 2
                continue;
28
            }
29 2
            $cookieParams = $this->getCookieParams($headerLine);
30 2
            $cookieList[$cookieParams['name']] = new Cookie($cookieParams);
31
        }
32 3
        $this->setList($cookieList);
33 3
    }
34
35
    /**
36
     * Splits HTTP header on the parameters.
37
     *
38
     * @param string    $headerLine     HTTP header line Set-Cookie.
39
     *
40
     * @return array
41
     */
42 2
    private function getCookieParams($headerLine)
43
    {
44 2
        $cookieParamsStrings = explode(':', trim($headerLine), 2);
45 2
        $params = array();
46 2
        foreach (explode(';', trim($cookieParamsStrings[1])) as $index => $paramString) {
47 2
            $pairParam = explode('=', trim($paramString), 2);
48 2
            $name = trim($pairParam[0]);
49 2
            $value = (!empty($pairParam[1]) ? trim($pairParam[1]) : true);
50 2
            if (0 == $index) {
51 2
                $params['name'] = $name;
52 2
                $params['value'] = $value;
53 2
                continue;
54
            }
55 1
            $params[$name] = $value;
56
        }
57 2
        return $params;
58
    }
59
}
60