Intraface_Auth_User::isLoggedIn()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 8
cp 0.75
crap 3.1406
1
<?php
2
/**
3
 * Authenticates a user
4
 *
5
 * @package  Intraface
6
 * @author   Lars Olesen <[email protected]>
7
 * @since    0.1.0
8
 * @version  @package-version@
9
 */
10
class Intraface_Auth_User
11
{
12
    private $db;
13
    private $email;
14
    private $password;
15
16
    /**
17
     * Constructor
18
     *
19
     * @param object $db       Databaseobject
20
     * @param string $email    Username
21
     * @param string $password Password
22
     *
23
     * @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...
24
     */
25 3
    function __construct(MDB2_Driver_Common $db, $session_id, $email = null, $password = null)
26
    {
27 3
        $this->db         = $db;
28 3
        $this->email      = $email;
29 3
        $this->password   = $password;
30 3
        $this->session_id = $session_id;
0 ignored issues
show
Bug introduced by
The property session_id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31 3
    }
32
33
    /**
34
     * Auth
35
     *
36
     * @return object
37
     */
38 1
    public function auth()
0 ignored issues
show
Coding Style introduced by
auth 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...
39
    {
40 1
        $result = $this->db->query("SELECT id FROM user WHERE email = ".$this->db->quote($this->email, 'text')." AND password = ".$this->db->quote(md5($this->password), 'text'));
41
42 1
        if (PEAR::isError($result)) {
43
            throw new Exception('result is an error' . $result->getMessage() . $result->getUserInfo());
44
        }
45
46 1
        if ($result->numRows() != 1) {
47
            return false;
48
        }
49 1
        $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
50
51 1
        $result = $this->db->exec("UPDATE user SET lastlogin = NOW(), session_id = ".$this->db->quote($this->session_id, 'text')." WHERE id = ". $this->db->quote($row['id'], 'integer'));
52 1
        if (PEAR::isError($result)) {
53
            throw new Exception('could not update user ' . $result->getMessage() . $result->getUserInfo());
54
        }
55
56 1
        $user = new Intraface_User($row['id']);
57 1
        if (!is_object($user) || $user->get('id') != $row['id']) {
58
            throw new Exception('Unable to return a valid user object on login');
59
        }
60
61 1
        $_SESSION['intraface_logged_in_user_id'] = $user->getId();
62
63 1
        return $user;
64
    }
65
66 1
    function isLoggedIn()
67
    {
68 1
        $result = $this->db->query("SELECT id FROM user WHERE session_id = ".$this->db->quote($this->session_id, 'text'));
69 1
        if (PEAR::isError($result)) {
70
            throw new Exception('could not check if user is logged in ' . $result->getUserInfo());
71
        }
72
73 1
        if ($result->numRows() == 0) {
74
            return false;
75
        }
76
77 1
        $row = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
78 1
        return new Intraface_User($row['id']);
79
    }
80
81
    /**
82
     * logout()
83
     *
84
     * @return boolean
85
     */
86 2 View Code Duplication
    public function logout()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87 2
    {
88
        $result = $this->db->exec("UPDATE user SET session_id = " . $this->db->quote('', 'text') . " WHERE session_id = " . $this->db->quote($this->session_id, 'text'));
89
90
        if (PEAR::isError($result)) {
91
             throw new Exception('could not log user out ' . $result->getUserInfo());
92
        }
93
        return true;
94
    }
95
96
    /**
97
     * Returns an identification string on the user
98
     *
99
     * @return string identification (email)
100
     */
101
    public function getIdentification()
102
    {
103
        return $this->email;
104
    }
105
}
106