Failed Conditions
Pull Request — master (#321)
by Anton
23:35 queued 08:33
created

Row   A

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 beforeInsert() 0 3 1
A beforeUpdate() 0 3 1
A getPrivileges() 0 6 2
A getRoles() 0 3 1
A hasRole() 0 5 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\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...
14
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...
15
16
/**
17
 * Row of 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
     */
52
    public function beforeSave(): void
53
    {
54
        $this->email = strtolower($this->email ?? '');
55
56
        $this->addValidator('login')
57
            ->required()
58
            ->latin()
59
            ->length(3, 255)
60
            ->callback(
61
                function ($login) {
62
                    $selector = static::getTable()
63
                        ::select()
64
                        ->where('login = ?', $login);
65
66
                    if ($this->id) {
67
                        $selector->andWhere('id != ?', $this->id);
68
                    }
69
70
                    $user = $selector->execute();
71
                    return !$user;
72
                },
73
                'User with this login is already exists'
74
            );
75
76
        $this->addValidator('email')
77
            ->required()
78
            ->email(true)
79
            ->callback(
80
                function ($email) {
81
                    $selector = static::getTable()
82
                        ::select()
83
                        ->where('email = ?', $email);
84
85
                    if ($this->id) {
86
                        $selector->andWhere('id != ?', $this->id);
87
                    }
88
89
                    $user = $selector->execute();
90
                    return !$user;
91
                },
92
                'User with this email is already exists'
93
            );
94
    }
95
96
    /**
97
     * @return void
98
     */
99
    public function beforeInsert(): void
100
    {
101
        $this->created = gmdate('Y-m-d H:i:s');
102
    }
103
104
    /**
105
     * @return void
106
     */
107
    public function beforeUpdate(): void
108
    {
109
        $this->updated = gmdate('Y-m-d H:i:s');
110
    }
111
112
    /**
113
     * Get user roles
114
     */
115
    public function getRoles()
116
    {
117
        return Roles\Table::getInstance()->getUserRoles($this->id);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 6
    public function getPrivileges(): array
124
    {
125 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...
126 6
            $this->privileges = Privileges\Table::getInstance()->getUserPrivileges($this->id);
127
        }
128 6
        return $this->privileges;
129
    }
130
131
    /**
132
     * Check user role
133
     *
134
     * @param integer $roleId
135
     *
136
     * @return boolean
137
     */
138 1
    public function hasRole($roleId): bool
139
    {
140 1
        $roles = Roles\Table::getInstance()->getUserRolesIdentity($this->id);
141
142 1
        return in_array($roleId, $roles, false);
143
    }
144
}
145