Completed
Push — master ( deda3e...c2c9c1 )
by Sergey
03:19 queued 47s
created

Cookies   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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