Passed
Push — master ( 6df11d...8e8817 )
by Julien
05:21
created

User   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 92
ccs 42
cts 46
cp 0.913
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A preparePassword() 0 8 3
A beforeSave() 0 3 1
A validation() 0 38 4
A initialize() 0 17 1
A checkPassword() 0 3 2
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Models;
12
13
use Zemit\Models\Abstracts\AbstractUser;
14
use Phalcon\Filter\Validation\Validator\Between;
15
use Phalcon\Filter\Validation\Validator\Confirmation;
16
use Phalcon\Filter\Validation\Validator\Date;
17
use Phalcon\Filter\Validation\Validator\Email;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Zemit\Models\Email. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use Phalcon\Filter\Validation\Validator\PresenceOf;
19
use Phalcon\Filter\Validation\Validator\StringLength\Max;
20
use Phalcon\Filter\Validation\Validator\Uniqueness;
21
use Phalcon\Filter\Validation\Validator\InclusionIn;
22
23
use Zemit\Identity;
24
use Zemit\Models\Interfaces\UserInterface;
25
26
/**
27
 * @property UserGroup[] $GroupNode
28
 * @property Group[] $GroupList
29
 * @property UserRole[] $RoleNode
30
 * @property Role[] $RoleList
31
 * @property UserType[] $TypeNode
32
 * @property Type[] $TypeList
33
 * @property File[] $FileList
34
 * @property Identity $Identity
35
 *
36
 * @method UserGroup[] getGroupNode(?array $params = null)
37
 * @method Group[] getGroupList(?array $params = null)
38
 * @method UserRole[] getRoleNode(?array $params = null)
39
 * @method Role[] getRoleList(?array $params = null)
40
 * @method UserType[] getTypeNode(?array $params = null)
41
 * @method Type[] getTypeList(?array $params = null)
42
 * @method File[] getFileList(?array $params = null)
43
 */
44
class User extends AbstractUser implements UserInterface
45
{
46
    protected $deleted = self::NO;
47
48 4
    public function initialize(): void
49
    {
50 4
        parent::initialize();
51
52 4
        $this->hasMany('id', File::Class, 'userId', ['alias' => 'FileList']);
0 ignored issues
show
Bug introduced by
The constant Zemit\Models\File::Class was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53
54 4
        $this->hasMany('id', UserGroup::class, 'userId', ['alias' => 'GroupNode']);
55 4
        $this->hasManyToMany('id', UserGroup::Class, 'userId',
0 ignored issues
show
Bug introduced by
The constant Zemit\Models\UserGroup::Class was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
56 4
            'groupId', Group::class, 'id', ['alias' => 'GroupList']);
57
58 4
        $this->hasMany('id', UserRole::class, 'userId', ['alias' => 'RoleNode']);
59 4
        $this->hasManyToMany('id', UserRole::Class, 'userId',
0 ignored issues
show
Bug introduced by
The constant Zemit\Models\UserRole::Class was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
60 4
            'roleId', Role::class, 'id', ['alias' => 'RoleList']);
61
62 4
        $this->hasMany('id', UserType::class, 'userId', ['alias' => 'TypeNode']);
63 4
        $this->hasManyToMany('id', UserType::Class, 'userId',
0 ignored issues
show
Bug introduced by
The constant Zemit\Models\UserType::Class was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
64 4
            'typeId', Type::class, 'id', ['alias' => 'TypeList']);
65
    }
66
67 4
    public function validation(): bool
68
    {
69 4
        $validator = $this->genericValidation();
70
71 4
        $validator->add('username', new PresenceOf(['message' => $this->_('required')]));
72 4
        $validator->add('email', new Max(['max' => 120, 'message' => $this->_('length-exceeded')]));
73
74 4
        $validator->add('firstName', new PresenceOf(['message' => $this->_('first-name') . ': ' . $this->_('required')]));
75 4
        $validator->add('firstName', new Max(['max' => 60, 'message' => $this->_('first-name') . ': ' . $this->_('length-exceeded')]));
76
77 4
        $validator->add('lastName', new PresenceOf(['message' => $this->_('last-name') . ': ' . $this->_('required')]));
78 4
        $validator->add('lastName', new Max(['max' => 60, 'message' => $this->_('last-name') . ': ' . $this->_('length-exceeded')]));
79
80 4
        $validator->add('email', new PresenceOf(['message' => $this->_('required')]));
81 4
        $validator->add('email', new Email(['message' => 'email-not-valid']));
82 4
        $validator->add('email', new Uniqueness(['message' => $this->_('not-unique')]));
83 4
        $validator->add('email', new Max(['max' => 191, 'message' => $this->_('length-exceeded')]));
84
85 4
        $validator->add('gender', new Between(["minimum" => 0, "maximum" => 1, 'message' => $this->_('boolean-not-valid')]));
86
87 4
        if ($this->getDob()) {
88
            $validator->add('dob', new Date(['format' => self::DATE_FORMAT, 'message' => $this->_('date-not-valid')]));
0 ignored issues
show
Bug introduced by
The constant Zemit\Models\User::DATE_FORMAT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
89
        }
90
91 4
        $validator->add(['phone', 'phone2', 'cellphone', 'fax'], new Max(['max' => 60, 'message' => $this->_('length-exceeded')]));
92
93 4
        $validator->add('token', new Max(['max' => 120, 'message' => $this->_('length-exceeded')]));
94
95
        // Password
96 4
        if (!$this->hasSnapshotData() || $this->hasChanged('password')) {
97 4
            $validator->add(['password', 'passwordConfirm'], new Max(['max' => 255, 'message' => 'Le mot de passe ne doit pas dépasser :max caractères']));
98 4
            $validator->add('passwordConfirm', new Confirmation([
99 4
                'message' => 'La mot de passe et la confirmation doivent être identique',
100 4
                'with' => 'password',
101 4
            ]));
102
        }
103
104 4
        return $this->validate($validator);
105
    }
106
107
    /**
108
     * Prepare save after validation
109
     */
110 4
    public function beforeSave(): void
111
    {
112 4
        $this->preparePassword();
113
    }
114
115
    /**
116
     * Salt & hash the passwordConfirm field into password
117
     */
118 4
    public function preparePassword(): void
119
    {
120 4
        $password = $this->getPassword();
121 4
        $passwordConfirm = $this->getPasswordConfirm();
122 4
        if (!empty($passwordConfirm) && $password === $passwordConfirm) {
123
            $this->setPassword($this->hash($passwordConfirm));
124
        }
125 4
        $this->setPasswordConfirm(null);
126
    }
127
128
    /**
129
     * @param string|null $password
130
     *
131
     * @return bool If the hash is valid or not
132
     */
133
    public function checkPassword(string $password = null): bool
134
    {
135
        return $password && $this->checkHash($this->getPassword(), $password);
136
    }
137
}
138