Completed
Push — master ( 3b85e8...220fac )
by Anton
16s
created

Row   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 7

Test Coverage

Coverage 14.89%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 107
ccs 7
cts 47
cp 0.1489
rs 10
wmc 9
lcom 3
cbo 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
B beforeSave() 0 43 3
A beforeInsert() 0 4 1
A beforeUpdate() 0 4 1
A getRoles() 0 4 1
A getPrivileges() 0 7 2
A hasRole() 0 6 1
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link      https://github.com/bluzphp/skeleton
5
 */
6
7
declare(strict_types=1);
8
9
namespace Application\Users;
10
11
use Application\Privileges;
12
use Application\Roles;
13
use Bluz\Auth\AbstractRowEntity;
14
use Bluz\Validator\Traits\Validator;
15
16
/**
17
 * User
18
 *
19
 * @package  Application\Users
20
 *
21
 * @property integer $id
22
 * @property string  $login
23
 * @property string  $email
24
 * @property string  $created
25
 * @property string  $updated
26
 * @property string  $status
27
 *
28
 * @SWG\Definition(definition="users", title="user", required={"id", "login", "status"})
29
 * @SWG\Property(property="id", type="integer", description="User UID", example=2)
30
 * @SWG\Property(property="login", type="string", description="Login", example="admin")
31
 * @SWG\Property(property="email", type="string", description="Email", example="[email protected]")
32
 * @SWG\Property(property="created", type="string", format="date-time", example="2017-01-01 20:17:01")
33
 * @SWG\Property(property="updated", type="string", format="date-time", example="2017-01-01 20:17:01")
34
 * @SWG\Property(property="status", type="string", enum={"pending", "active", "disabled", "deleted"})
35
 */
36
class Row extends AbstractRowEntity
0 ignored issues
show
Bug introduced by
There is one abstract method tryLogin in this class; you could implement it, or declare this class as abstract.
Loading history...
37
{
38
    use Validator;
39
40
    /**
41
     * Small cache of user privileges
42
     *
43
     * @var array
44
     */
45
    protected $privileges;
46
47
    /**
48
     * @return void
49
     */
50
    public function beforeSave()
51
    {
52
        $this->email = strtolower($this->email ?? '');
53
54
        $this->addValidator('login')
55
            ->required()
56
            ->latin()
57
            ->length(3, 255)
58
            ->callback(
59
                function ($login) {
60
                    $selector = static::getTable()
61
                        ::select()
62
                        ->where('login = ?', $login);
63
64
                    if ($this->id) {
65
                        $selector->andWhere('id != ?', $this->id);
66
                    }
67
68
                    $user = $selector->execute();
69
                    return !$user;
70
                },
71
                'User with this login is already exists'
72
            );
73
74
        $this->addValidator('email')
75
            ->required()
76
            ->email(true)
77
            ->callback(
78
                function ($email) {
79
                    $selector = static::getTable()
80
                        ::select()
81
                        ->where('email = ?', $email);
82
83
                    if ($this->id) {
84
                        $selector->andWhere('id != ?', $this->id);
85
                    }
86
87
                    $user = $selector->execute();
88
                    return !$user;
89
                },
90
                'User with this email is already exists'
91
            );
92
    }
93
94
    /**
95
     * @return void
96
     */
97
    public function beforeInsert()
98
    {
99
        $this->created = gmdate('Y-m-d H:i:s');
100
    }
101
102
    /**
103
     * @return void
104
     */
105
    public function beforeUpdate()
106
    {
107
        $this->updated = gmdate('Y-m-d H:i:s');
108
    }
109
110
    /**
111
     * Get user roles
112
     */
113
    public function getRoles()
114
    {
115
        return Roles\Table::getInstance()->getUserRoles($this->id);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 6
    public function getPrivileges(): array
122
    {
123 6
        if (!$this->privileges) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->privileges of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
124 6
            $this->privileges = Privileges\Table::getInstance()->getUserPrivileges($this->id);
125
        }
126 6
        return $this->privileges;
127
    }
128
129
    /**
130
     * Check user role
131
     *
132
     * @param integer $roleId
133
     *
134
     * @return boolean
135
     */
136 1
    public function hasRole($roleId)
137
    {
138 1
        $roles = Roles\Table::getInstance()->getUserRolesIdentity($this->id);
139
140 1
        return in_array($roleId, $roles);
141
    }
142
}
143