Passed
Push — develop ( eacc0b...67462a )
by Andrew
02:45
created

Auth   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isAuthenticated() 0 16 4
A __construct() 0 4 1
1
<?php
2
3
4
namespace ddlzz\AmoAPI;
5
6
7
/**
8
 * Class Auth
9
 * @package ddlzz\AmoAPI
10
 * @author ddlzz
11
 */
12
class Auth
13
{
14
    /** @var string */
15
    private $login;
16
17
    /** @var string */
18
    private $cookiePath;
19
20
    /**
21
     * Auth constructor.
22
     * @param string $login
23
     * @param string $cookiePath
24
     */
25 9
    public function __construct($login, $cookiePath)
26
    {
27 9
        $this->login = $login;
28 9
        $this->cookiePath = $cookiePath;
29 9
    }
30
31
    /**
32
     * Checks if the cookie file exists and if it's valid.
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 === (strpos(file_get_contents($cookie), (str_replace('@', '%40', $this->login))))) {
0 ignored issues
show
Bug introduced by
It seems like file_get_contents($cookie) can also be of type false; however, parameter $haystack of strpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        if (false === (strpos(/** @scrutinizer ignore-type */ file_get_contents($cookie), (str_replace('@', '%40', $this->login))))) {
Loading history...
46 1
            unlink($cookie);
47 1
            return false;
48
        }
49
50 6
        return true;
51
    }
52
}