Row   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Test Coverage

Coverage 15.55%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 107
ccs 7
cts 45
cp 0.1555
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeSave() 0 41 3
A getPrivileges() 0 6 2
A getRoles() 0 3 1
A beforeInsert() 0 3 1
A beforeUpdate() 0 3 1
A hasRole() 0 5 1
1
<?php
2
3
/**
4
 * @copyright Bluz PHP Team
5
 * @link      https://github.com/bluzphp/skeleton
6
 */
7
8
declare(strict_types=1);
9
10
namespace Application\Users;
11
12
use Application\Privileges;
13
use Application\Roles;
14
use Bluz\Auth\AbstractIdentity;
0 ignored issues
show
Bug introduced by
The type Bluz\Auth\AbstractIdentity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Bluz\Validator\Traits\Validator;
0 ignored issues
show
Bug introduced by
The type Bluz\Validator\Traits\Validator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
17
/**
18
 * Row of User
19
 *
20
 * @package  Application\Users
21
 *
22
 * @property integer $id
23
 * @property string $login
24
 * @property string $email
25
 * @property string $created
26
 * @property string $updated
27
 * @property string $status
28
 *
29
 * @OA\Schema(schema="user", title="user", required={"id", "login", "status"})
30
 * @OA\Property(property="id", type="integer", description="User UID", example=2)
31
 * @OA\Property(property="login", type="string", description="Login", example="admin")
32
 * @OA\Property(property="email", type="string", description="Email", example="[email protected]")
33
 * @OA\Property(property="created", type="string", format="date-time", example="2017-01-01 20:17:01")
34
 * @OA\Property(property="updated", type="string", format="date-time", example="2017-01-01 20:17:01")
35
 * @OA\Property(property="status", type="string", enum={"pending", "active", "disabled", "deleted"})
36
 */
37
class Row extends AbstractIdentity
38
{
39
    use Validator;
40
41
    /**
42
     * Small cache of user privileges
43
     *
44
     * @var array
45
     */
46
    protected $privileges;
47
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @throws \Bluz\Validator\Exception\ComponentException
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 6
     */
124
    public function getPrivileges(): array
125 6
    {
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
            $this->privileges = Privileges\Table::getInstance()->getUserPrivileges($this->id);
128 6
        }
129
        return $this->privileges;
130
    }
131
132
    /**
133
     * Check user role
134
     *
135
     * @param integer $roleId
136
     *
137
     * @return boolean
138 1
     */
139
    public function hasRole($roleId): bool
140 1
    {
141
        $roles = Roles\Table::getInstance()->getUserRolesIdentity($this->id);
142 1
143
        return in_array($roleId, $roles, false);
144
    }
145
}
146