Auth   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 42
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isAuthenticated() 0 17 4
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