Auth::isAuthenticated()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 3
nop 0
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ddlzz\AmoAPI;
4
5
/**
6
 * Class Auth.
7
 *
8
 * @author ddlzz
9
 */
10
class Auth
11
{
12
    /** @var string */
13
    private $login;
14
15
    /** @var string */
16
    private $cookiePath;
17
18
    /**
19
     * Auth constructor.
20
     *
21
     * @param string $login
22
     * @param string $cookiePath
23
     */
24 9
    public function __construct($login, $cookiePath)
25
    {
26 9
        $this->login = $login;
27 9
        $this->cookiePath = $cookiePath;
28 9
    }
29
30
    /**
31
     * Checks if the cookie file exists and if it's valid.
32
     *
33
     * @return bool
34
     */
35 9
    public function isAuthenticated()
36
    {
37 9
        $cookie = $this->cookiePath;
38 9
        $cookieLifetime = time() - 60 * 14; // 14 minutes
39
40 9
        if ((!file_exists($cookie)) || (filemtime($cookie) <= $cookieLifetime)) {
41 2
            return false;
42
        }
43
44
        // If login has been changed, we need to delete the cookie file for the changes to take effect
45 7
        if (false === mb_strpos(file_get_contents($cookie), str_replace('@', '%40', $this->login))) {
46 1
            unlink($cookie);
47
48 1
            return false;
49
        }
50
51 6
        return true;
52
    }
53
}
54