Intraface_Auth::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
/**
3
 * Keeps track of logged in users
4
 *
5
 * @package  Intraface
6
 * @author   Lars Olesen <[email protected]>
7
 * @since    0.1.0
8
 * @version  @package-version@
9
 */
10
class Intraface_Auth
11
{
12
    private $identity;
0 ignored issues
show
Unused Code introduced by
The property $identity is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
    private $observers = array();
14
    private $session_id;
15
16
    /**
17
     * Constructor
18
     *
19
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
20
     */
21 3
    public function __construct($session_id)
22
    {
23 3
        $this->session_id = $session_id;
24 3
    }
25
26 2
    public function authenticate($adapter)
27
    {
28 2
        if (is_callable(array($adapter, 'getIdentification'))) {
29 2
            $identification = $adapter->getIdentification();
30 2
        } else {
31
            $identification = '[unidentifiable]';
32
        }
33
34 2
        if ($object = $adapter->auth()) {
35 2
            $this->notifyObservers('login', $identification . ' logged in');
36 2
        } else {
37
            $this->notifyObservers('login', $identification . ' could not login');
38
        }
39
40 2
        return $object;
41
    }
42
43
    /**
44
     * hasIdentity()
45
     *
46
     * @return mixed user id or false
47
     */
48 1
    public function hasIdentity()
0 ignored issues
show
Coding Style introduced by
hasIdentity 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...
49
    {
50 1
        if (!empty($_SESSION['intraface_logged_in_user_id'])) {
51 1
            return true;
52
        } else {
53
            return false;
54
        }
55
    }
56
57
    /**
58
     * logout()
59
     *
60
     * @return boolean
61
     */
62
    public function clearIdentity()
0 ignored issues
show
Coding Style introduced by
clearIdentity 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...
63
    {
64
        $_SESSION['user'] = '';
65
        unset($_SESSION['user']);
66
        //session_destroy();
67
        return true;
68
    }
69
70 1
    public function getIdentity($db)
71
    {
72 1
        if ($this->hasIdentity()) {
73 1
            $adapter = new Intraface_Auth_User($db, $this->session_id);
74 1
            if (!$user = $adapter->isLoggedIn()) {
75
                throw new Exception('No valid user was found');
76
            }
77
            // $user->clearCachedPermission();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78 1
            return $user;
79
        }
80
        return false;
81
    }
82
83
    /**
84
     * Implements the observer pattern
85
     *
86
     * @param object $observer
87
     *
88
     * @return boolean
89
     */
90
    public function attachObserver($observer)
91
    {
92
        $this->observers[] = $observer;
93
        return true;
94
    }
95
96
    /**
97
     * Notifies observers
98
     *
99
     * @param string $code Which code
100
     * @param string $msg  Which message to pass to observers
101
     */
102 2
    private function notifyObservers($code, $msg)
103
    {
104 2
        foreach ($this->getObservers() as $observer) {
105
            $observer->update($code, $msg);
106 2
        }
107 2
        return true;
108
    }
109
110
    /**
111
     * Implements the observer pattern
112
     *
113
     * @return array with observers
114
     */
115 2
    public function getObservers()
116
    {
117 2
        return $this->observers;
118
    }
119
}
120