SessionLoginManager::isLogged()   A
last analyzed

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
nop 0
1
<?php
2
3
namespace App\Auth\LoginManager;
4
5
use App\Auth\AuthManager;
6
use App\Auth\LoginManagerInterface;
7
use App\Auth\AccountManagerInterface;
8
use App\Auth\Exception\AccountNotFound;
9
10
class SessionLoginManager implements LoginManagerInterface
11
{
12
    /**
13
     * @var AccountManagerInterface
14
     */
15
    private $accountManager;
16
17
    public function __construct(array $settings = [])
18
    {
19
        if (session_status() === PHP_SESSION_NONE) {
20
            session_start();
21
        }
22
23
        $this->accountManager = AuthManager::getAccountManager();
24
    }
25
26
    public function login($accountId, $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...
27
    {
28
        if (
29
            $this->accountManager->isExistAccountId($accountId) === false
30
            || $this->accountManager->isEqualPassword($accountId, $password) === false
31
        ) {
32
            throw new AccountNotFound('Invalid account id or password');
33
        }
34
35
        $_SESSION['account'] = [
36
            'id' => $accountId
37
        ];
38
    }
39
40
    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...
41
    {
42
        unset($_SESSION['account']);
43
    }
44
45
    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...
46
    {
47
        return isset($_SESSION['account']) && isset($_SESSION['account']['id']);
48
    }
49
50
    public function getLoggedAccountId()
0 ignored issues
show
Coding Style introduced by
getLoggedAccountId 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...
51
    {
52
        if ($this->isLogged() === false) {
53
            return;
54
        }
55
56
        return $_SESSION['account']['id'];
57
    }
58
}
59