Completed
Push — master ( ecdd68...e69772 )
by Arnold
02:25
created

Sessions::updateSessionData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
nc 2
cc 2
eloc 5
nop 2
1
<?php
2
3
namespace Jasny\Auth;
4
5
use Jasny\Auth\User;
6
7
/**
8
 * Use PHP sessions to persist the authenticated user
9
 */
10
trait Sessions
11
{
12
    /**
13
     * Get the authenticated user
14
     * 
15
     * @return User
16
     */
17
    abstract public function user();
18
    
19
    
20
    /**
21
     * Get the session data
22
     * 
23
     * @return array
24
     */
25
    protected function getSessionData($key)
1 ignored issue
show
Coding Style introduced by
getSessionData 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...
26
    {
27
        return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
28
    }
29
    
30
    /**
31
     * Update the session
32
     * 
33
     * @param string $key
34
     * @param mixed  $value
35
     */
36
    protected function updateSessionData($key, $value)
1 ignored issue
show
Coding Style introduced by
updateSessionData 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...
37
    {
38
        if (is_null($value)) {
39
            unset($_SESSION[$key]);
40
        } else {
41
            $_SESSION[$key] = $value;
42
        }
43
    }
44
    
45
    
46
    /**
47
     * Get current authenticated user id for the session
48
     * 
49
     * @return mixed
50
     */
51
    protected function getCurrentUserId()
52
    {
53
        return $this->getSessionData('auth_uid');
54
    }
55
    
56
    /**
57
     * Store the current user id in the session
58
     */
59
    protected function persistCurrentUser()
60
    {
61
        $user = $this->user();
62
        $this->updateSessionData('auth_uid', $user ? $user->getId() : null);
63
    }
64
}
65