Completed
Push — master ( 878b1b...cb4691 )
by Anton
09:55 queued 08:01
created

Row::beforeSave()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 0
dl 0
loc 43
ccs 0
cts 30
cp 0
crap 12
rs 9.232
c 0
b 0
f 0
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\AbstractIdentity;
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
 * @OA\Schema(schema="user", title="user", required={"id", "login", "status"})
29
 * @OA\Property(property="id", type="integer", description="User UID", example=2)
30
 * @OA\Property(property="login", type="string", description="Login", example="admin")
31
 * @OA\Property(property="email", type="string", description="Email", example="[email protected]")
32
 * @OA\Property(property="created", type="string", format="date-time", example="2017-01-01 20:17:01")
33
 * @OA\Property(property="updated", type="string", format="date-time", example="2017-01-01 20:17:01")
34
 * @OA\Property(property="status", type="string", enum={"pending", "active", "disabled", "deleted"})
35
 */
36
class Row extends AbstractIdentity
37
{
38
    use Validator;
39
40
    /**
41
     * Small cache of user privileges
42
     *
43
     * @var array
44
     */
45
    protected $privileges;
46
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @throws \Bluz\Validator\Exception\ComponentException
51
     * @throws \Bluz\Db\Exception\TableNotFoundException
52
     */
53
    public function beforeSave(): void
54
    {
55
        $this->email = strtolower($this->email ?? '');
56
57
        $this->addValidator('login')
58
            ->required()
59
            ->latin()
60
            ->length(3, 255)
61
            ->callback(
62
                function ($login) {
63
                    $selector = static::getTable()
64
                        ::select()
65
                        ->where('login = ?', $login);
66
67
                    if ($this->id) {
68
                        $selector->andWhere('id != ?', $this->id);
69
                    }
70
71
                    $user = $selector->execute();
72
                    return !$user;
73
                },
74
                'User with this login is already exists'
75
            );
76
77
        $this->addValidator('email')
78
            ->required()
79
            ->email(true)
80
            ->callback(
81
                function ($email) {
82
                    $selector = static::getTable()
83
                        ::select()
84
                        ->where('email = ?', $email);
85
86
                    if ($this->id) {
87
                        $selector->andWhere('id != ?', $this->id);
88
                    }
89
90
                    $user = $selector->execute();
91
                    return !$user;
92
                },
93
                'User with this email is already exists'
94
            );
95
    }
96
97
    /**
98
     * @return void
99
     */
100
    public function beforeInsert(): void
101
    {
102
        $this->created = gmdate('Y-m-d H:i:s');
103
    }
104
105
    /**
106
     * @return void
107
     */
108
    public function beforeUpdate(): void
109
    {
110
        $this->updated = gmdate('Y-m-d H:i:s');
111
    }
112
113
    /**
114
     * Get user roles
115
     */
116
    public function getRoles()
117
    {
118
        return Roles\Table::getInstance()->getUserRoles($this->id);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 6
    public function getPrivileges(): array
125
    {
126 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...
127 6
            $this->privileges = Privileges\Table::getInstance()->getUserPrivileges($this->id);
128
        }
129 6
        return $this->privileges;
130
    }
131
132
    /**
133
     * Check user role
134
     *
135
     * @param integer $roleId
136
     *
137
     * @return boolean
138
     */
139 1
    public function hasRole($roleId): bool
140
    {
141 1
        $roles = Roles\Table::getInstance()->getUserRolesIdentity($this->id);
142
143 1
        return in_array($roleId, $roles, false);
144
    }
145
}
146