AdminUser   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 3

Importance

Changes 0
Metric Value
wmc 28
lcom 4
cbo 3
dl 0
loc 171
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getSource() 0 4 1
A initialize() 0 4 1
A setCheckboxes() 0 4 2
A getId() 0 4 1
A getLogin() 0 4 1
A getEmail() 0 4 1
A getRole() 0 4 1
A getRoleTitle() 0 6 2
A setRole() 0 4 1
A getName() 0 4 1
A setName() 0 4 1
A getPassword() 0 4 1
A checkPassword() 0 6 2
A getActive() 0 4 1
A isActive() 0 6 2
A setLogin() 0 4 1
A setEmail() 0 4 1
A setPassword() 0 6 2
A setActive() 0 4 1
A getAuthData() 0 10 1
A getRoleById() 0 18 2
A validation() 0 18 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.com.ua)
4
 * @author Aleksandr Torosh <[email protected]>
5
 */
6
7
namespace Admin\Model;
8
use Phalcon\Validation;
9
use Phalcon\Validation\Validator\Email as EmailValidator;
10
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;
11
use stdClass;
12
13
class AdminUser extends \Phalcon\Mvc\Model
14
{
15
16
    public function getSource()
17
    {
18
        return "admin_user";
19
    }
20
21
    public $id;
22
    public $role;
23
    public $login;
24
    public $email;
25
    public $name;
26
    public $password;
27
    public $active = 0;
28
29
    public static $roles = [
30
        'journalist' => 'Journalist',
31
        'editor'     => 'Editor',
32
        'admin'      => 'Admin',
33
    ];
34
35
    public function initialize()
36
    {
37
        
38
    }
39
40
    public function setCheckboxes($post)
41
    {
42
        $this->setActive(isset($post['active']) ? 1 : 0);
43
    }
44
45
    public function validation()
46
    {
47
48
       $validator = new Validation();
49
       $validator->add('login', new UniquenessValidator(
50
           [
51
               "model"   => $this,
52
               "message" => $this->getDi()->get('helper')->translate("The Login must be unique")
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDi() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
53
           ]
54
       ));
55
       $validator->add('email', new UniquenessValidator(
56
           [
57
               "model"   => $this,
58
               "message" => $this->getDi()->get('helper')->translate("The Email must be unique")
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDi() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
59
           ]
60
       ));
61
       return $this->validate($validator);
62
    }
63
64
    public function getId()
65
    {
66
        return $this->id;
67
    }
68
69
    public function getLogin()
70
    {
71
        return $this->login;
72
    }
73
74
    public function getEmail()
75
    {
76
        return $this->email;
77
    }
78
79
    public function getRole()
80
    {
81
        return $this->role;
82
    }
83
84
    public function getRoleTitle()
85
    {
86
        if (array_key_exists($this->role, self::$roles)) {
87
            return self::$roles[$this->role];
88
        }
89
    }
90
91
    public function setRole($role)
92
    {
93
        $this->role = $role;
94
    }
95
96
    public function getName()
97
    {
98
        return $this->name;
99
    }
100
101
102
    public function setName($name)
103
    {
104
        $this->name = $name;
105
    }
106
107
    public function getPassword()
108
    {
109
        return ''; // We don't need hash of password. Just return empty string.
110
    }
111
112
    public function checkPassword($password)
113
    {
114
        if (password_verify($password, $this->password)) {
115
            return true;
116
        }
117
    }
118
119
    public function getActive()
120
    {
121
        return $this->active;
122
    }
123
124
    public function isActive()
125
    {
126
        if ($this->active) {
127
            return true;
128
        }
129
    }
130
131
    public function setLogin($login)
132
    {
133
        $this->login = $login;
134
    }
135
136
    public function setEmail($email)
137
    {
138
        $this->email = $email;
139
    }
140
141
    public function setPassword($password)
142
    {
143
        if ($password) {
144
            $this->password = password_hash($password, PASSWORD_DEFAULT);
145
        }
146
    }
147
148
    public function setActive($active)
149
    {
150
        $this->active = $active;
151
    }
152
153
    public function getAuthData()
154
    {
155
        $authData = new stdClass();
156
        $authData->id = $this->getId();
157
        $authData->admin_session = true;
158
        $authData->login = $this->getLogin();
159
        $authData->email = $this->getEmail();
160
        $authData->name = $this->getName();
161
        return $authData;
162
    }
163
164
    public static function getRoleById($id)
165
    {
166
        $role = self::findFirst([
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $role is correct as self::findFirst(array('c...d), 'lifetime' => 60))) (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
167
            'conditions' => 'id = :id:',
168
            'bind'       => ['id' => $id],
169
            'columns'    => ['role'],
170
            'cache'      => [
171
                'key'      => HOST_HASH . md5('Admin\Model\AdminUser::getRoleById::' . $id),
172
                'lifetime' => 60,
173
            ]
174
        ]);
175
        if ($role) {
176
            return $role->role;
177
        } else {
178
            return 'guest';
179
        }
180
181
    }
182
183
}
184