Passed
Push — master ( a3cc07...eb52df )
by Nashwan
03:50
created

User::sanitize()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 7
nop 2
dl 0
loc 20
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * User entity class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Models
7
 * @since      1.0
8
 */
9
namespace EBloodBank\Models;
10
11
use DateTime;
12
use DateTimeZone;
13
use InvalidArgumentException;
14
use EBloodBank as EBB;
15
use EBloodBank\Traits\EntityMeta;
16
17
/**
18
 * User entity class
19
 *
20
 * @since 1.0
21
 *
22
 * @Entity(repositoryClass="EBloodBank\Models\UserRepository")
23
 * @Table(name="user")
24
 * @HasLifecycleCallbacks
25
 */
26
class User extends Entity
27
{
28
    use EntityMeta;
29
30
    /**
31
     * User ID
32
     * 
33
     * @var   int
34
     * @since 1.0
35
     *
36
     * @Id
37
     * @GeneratedValue
38
     * @Column(type="integer", name="user_id")
39
     */
40
    protected $id = 0;
41
42
    /**
43
     * User name
44
     * 
45
     * @var   string
46
     * @since 1.0
47
     *
48
     * @Column(type="string", name="user_name")
49
     */
50
    protected $name;
51
52
    /**
53
     * User email
54
     * 
55
     * @var   string
56
     * @since 1.0
57
     *
58
     * @Column(type="string", name="user_email", unique=true)
59
     */
60
    protected $email;
61
62
    /**
63
     * User password
64
     * 
65
     * @var   string
66
     * @since 1.0
67
     *
68
     * @Column(type="string", name="user_pass")
69
     */
70
    protected $pass;
71
72
    /**
73
     * User role
74
     * 
75
     * @var   string
76
     * @since 1.0
77
     *
78
     * @Column(type="string", name="user_role")
79
     */
80
    protected $role;
81
82
    /**
83
     * User creation datetime
84
     * 
85
     * @var   \DateTime
86
     * @since 1.0
87
     *
88
     * @Column(type="datetime", name="user_created_at")
89
     */
90
    protected $created_at;
91
92
    /**
93
     * User status
94
     * 
95
     * @var   string
96
     * @since 1.0
97
     *
98
     * @Column(type="string", name="user_status")
99
     */
100
    protected $status;
101
102
    /**
103
     * User meta
104
     * 
105
     * @var   array
106
     * @since 1.0
107
     *
108
     * @Column(type="json", name="user_meta")
109
     */
110
    protected $meta = [];
111
112
    /**
113
     * @return bool
114
     * @since  1.0
115
     */
116
    public function isExists()
117
    {
118
        $id = (int) $this->get('id');
119
        return ! empty($id);
120
    }
121
122
    /**
123
     * @return bool
124
     * @since  1.0
125
     */
126
    public function isPending()
127
    {
128
        return 'pending' === $this->get('status');
129
    }
130
131
    /**
132
     * @return bool
133
     * @since  1.0
134
     */
135
    public function isActivated()
136
    {
137
        return 'activated' === $this->get('status');
138
    }
139
140
    /**
141
     * @return void
142
     * @since  1.6
143
     * 
144
     * @PrePersist
145
     */
146
    public function doActionOnPrePersist()
147
    {
148
        $this->set('created_at', new DateTime('now', new DateTimeZone('UTC')));
149
    }
150
151
    /**
152
     * @return mixed
153
     * @since  1.0
154
     * @static
155
     */
156
    public static function sanitize($key, $value)
157
    {
158
        switch ($key) {
159
            case 'id':
160
                $value = EBB\sanitizeInteger($value);
0 ignored issues
show
Bug introduced by
The function sanitizeInteger was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

160
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeInteger($value);
Loading history...
161
                break;
162
            case 'name':
163
                $value = EBB\sanitizeTitle($value);
0 ignored issues
show
Bug introduced by
The function sanitizeTitle was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeTitle($value);
Loading history...
164
                break;
165
            case 'email':
166
                $value = EBB\sanitizeEmail($value);
0 ignored issues
show
Bug introduced by
The function sanitizeEmail was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

166
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeEmail($value);
Loading history...
167
                break;
168
            case 'role':
169
            case 'status':
170
                $value = EBB\sanitizeSlug($value);
0 ignored issues
show
Bug introduced by
The function sanitizeSlug was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

170
                $value = /** @scrutinizer ignore-call */ EBB\sanitizeSlug($value);
Loading history...
171
                break;
172
            case 'created_at':
173
                break;
174
        }
175
        return $value;
176
    }
177
178
    /**
179
     * @throws \InvalidArgumentException
180
     * @return bool
181
     * @since  1.0
182
     * @static
183
     */
184
    public static function validate($key, $value)
185
    {
186
        switch ($key) {
187
            case 'id':
188
                if (! EBB\isValidID($value)) {
0 ignored issues
show
Bug introduced by
The function isValidID was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

188
                if (! /** @scrutinizer ignore-call */ EBB\isValidID($value)) {
Loading history...
189
                    throw new InvalidArgumentException(__('Invalid user ID.'));
190
                }
191
                break;
192
            case 'name':
193
                if (! is_string($value) || empty($value)) {
194
                    throw new InvalidArgumentException(__('Invalid user name.'));
195
                }
196
                break;
197
            case 'email':
198
                if (! EBB\isValidEmail($value)) {
0 ignored issues
show
Bug introduced by
The function isValidEmail was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
                if (! /** @scrutinizer ignore-call */ EBB\isValidEmail($value)) {
Loading history...
199
                    throw new InvalidArgumentException(__('Invalid user e-mail.'));
200
                }
201
                break;
202
            case 'pass':
203
                if (! is_string($value) || empty($value)) {
204
                    throw new InvalidArgumentException(__('Invalid user password.'));
205
                }
206
                break;
207
            case 'role':
208
                if (! is_string($value)) {
209
                    throw new InvalidArgumentException(__('Invalid user role.'));
210
                }
211
                break;
212
            case 'status':
213
                if (! is_string($value) || empty($value)) {
214
                    throw new InvalidArgumentException(__('Invalid user status.'));
215
                }
216
                break;
217
        }
218
219
        return true;
220
    }
221
}
222