Completed
Push — master ( ac011d...56c397 )
by Mikołaj
03:19
created

Auth::isLogged()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 8
nc 4
nop 0
1
<?php
2
3
namespace Egzaminer;
4
5
use Exception;
6
7
class Auth
8
{
9
    private $users;
10
11
    public function __construct()
12
    {
13
        $configPath = dirname(__DIR__).'/config/users.php';
14
        if (!file_exists($configPath)) {
15
            throw new Exception('Config file users.php does not exist');
16
        }
17
        $this->users = include $configPath;
18
    }
19
20
    /**
21
     * Login.
22
     *
23
     * @param string $login
24
     * @param string $password
25
     *
26
     * @return bool
27
     */
28
    public function login($login, $password)
0 ignored issues
show
Coding Style introduced by
login uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
29
    {
30
        foreach ($this->users as $user) {
31
            if (password_verify($password, $user['pass_hash'])
32
                && $login === $user['login']) {
33
                $_SESSION['egzaminer_auth_un'] = $user['login'];
34
                $_SESSION['ga_cookie'] = password_hash($user['login'], PASSWORD_DEFAULT);
35
36
                return true;
37
            }
38
        }
39
40
        return false;
41
    }
42
43
    /**
44
     * Verify is usser logged.
45
     */
46
    public function isLogged()
0 ignored issues
show
Coding Style introduced by
isLogged uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
47
    {
48
        if (!isset($_SESSION['ga_cookie'])) {
49
            return false;
50
        }
51
52
        foreach ($this->users as $user) {
53
            if (password_verify($user['login'], $_SESSION['ga_cookie'])
54
                && $_SESSION['egzaminer_auth_un'] === $user['login']) {
55
                return true;
56
            }
57
        }
58
59
        return false;
60
    }
61
62
    /**
63
     * Logout user.
64
     *
65
     * @return bool
66
     */
67
    public function logout()
0 ignored issues
show
Coding Style introduced by
logout uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
68
    {
69
        $_SESSION['egzaminer_auth_un'] = false;
70
        $_SESSION['ga_cookie'] = false;
71
72
        return session_destroy();
73
    }
74
}
75