Passed
Push — master ( 19517c...d5d6b0 )
by Sergey
01:00
created

Cookies   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 8 2
A getToken() 0 4 1
A all() 0 4 1
A fill() 0 10 3
A isHttp() 0 4 1
A getCookieData() 0 17 1
A isValidLine() 0 4 3
A parseCookieLine() 0 17 3
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers;
4
5
class Cookies
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $cookies = [];
11
12
    /**
13
     * @param string $name
14
     * @return mixed
15
     */
16
    public function get($name)
17
    {
18
        if(array_key_exists($name, $this->cookies)) {
19
            return $this->cookies[$name]['value'];
20
       }
21
22
        return null;
23
    }
24
25
    /**
26
     * @return string|null
27
     */
28
    public function getToken()
29
    {
30
        return $this->get('csrftoken');
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    public function all()
37
    {
38
        return $this->cookies;
39
    }
40
41
    /**
42
     * @param string $file
43
     */
44
    public function fill($file)
45
    {
46
        $this->cookies = [];
47
48
        foreach (file($file) as $line) {
49
            if($cookie = $this->parseCookieLine($line)) {
50
                $this->cookies[$cookie['name']] = $cookie;
51
            }
52
        }
53
    }
54
55
    /**
56
     * @param string $line
57
     * @return bool
58
     */
59
    protected function isHttp($line)
60
    {
61
        return substr($line, 0, 10) == '#HttpOnly_';
62
    }
63
64
    /**
65
     * @param string $line
66
     * @return bool
67
     */
68
    protected function isValidLine($line)
69
    {
70
        return strlen($line) > 0 && $line[0] != '#' && substr_count($line, "\t") == 6;
71
    }
72
73
    /**
74
     * @param string $line
75
     * @return array|bool
76
     */
77
    protected function parseCookieLine($line)
78
    {
79
        // detect http only cookies and remove #HttpOnly prefix
80
        $httpOnly = $this->isHttp($line);
81
82
        if ($httpOnly) {
83
            $line = substr($line, 10);
84
        }
85
86
        if (!$this->isValidLine($line)) return false;
87
88
        $data = $this->getCookieData($line);
89
90
        $data['httponly'] = $httpOnly;
91
92
        return $data;
93
    }
94
95
    /**
96
     * @param string $line
97
     * @return array
98
     */
99
    protected function getCookieData($line)
100
    {
101
        // execGet tokens in an array
102
        $data = explode("\t", $line);
103
        // trim the tokens
104
        $data =  array_map('trim', $data);
105
106
        return [
107
            'domain'     => $data[0],
108
            'flag'       => $data[1],
109
            'path'       => $data[2],
110
            'secure'     => $data[3],
111
            'name'       => urldecode($data[5]),
112
            'value'      => urldecode($data[6]),
113
            'expiration' => date('Y-m-d h:i:s', $data[4]),
114
        ];
115
    }
116
}
117