Completed
Push — master ( dbb7e7...2e3c0b )
by Sergey
56s
created

CsrfHelper::getDefaultCookie()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace seregazhuk\PinterestBot\Helpers;
4
5
class CsrfHelper
6
{
7
    const TOKEN_NAME = 'csrftoken';
8
    const DEFAULT_TOKEN = '1234';
9
10
    /**
11
     * Get a CSRF token from the given cookie file.
12
     *
13
     * @param string $file
14
     *
15
     * @return string|null
16
     */
17
    public static function getTokenFromFile($file)
18
    {
19
        if (!file_exists($file)) {
20
            return null;
21
        }
22
23
        foreach (file($file) as $line) {
24
            if ($token = self::parseLineForToken($line)) {
25
                return $token;
26
            }
27
        }
28
29
        return null;
30
    }
31
32
    /**
33
     * @param string $line
34
     *
35
     * @return false|string
36
     */
37
    protected static function parseLineForToken($line)
38
    {
39
        if (empty(strstr($line, self::TOKEN_NAME))) {
40
            return false;
41
        }
42
43
        preg_match('/' . self::TOKEN_NAME . '\s(\w*)/', $line, $matches);
44
        if (!empty($matches)) {
45
            return $matches[1];
46
        }
47
48
        return false;
49
    }
50
}
51