Completed
Push — master ( 4dd992...7f3992 )
by Cheren
04:17
created

UsersTable::validationDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 9.5147
c 0
b 0
f 0
cc 1
eloc 45
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * CakeCMS Community
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Community
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Community".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Community\Model\Table;
17
18
use Core\ORM\Table;
19
use Cake\Validation\Validator;
20
21
/**
22
 * Class UsersTable
23
 *
24
 * @package Community\Model\Table
25
 * @method filterParams(array $query = [])
26
 * @property GroupsTable $Groups
27
 */
28
class UsersTable extends Table
29
{
30
31
    /**
32
     * Initialize a table instance. Called after the constructor.
33
     *
34
     * @param array $config
35
     * @return void
36
     * @throws \RuntimeException
37
     */
38 View Code Duplication
    public function initialize(array $config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        parent::initialize($config);
41
42
        $this
43
            ->setPrimaryKey('id')
44
            ->setTable(CMS_TABLE_USERS)
45
            ->setDisplayField('name');
46
47
        $this->addBehavior('Timestamp');
48
        $this->addBehavior('Search.Search');
49
50
        $this->addAssociations([
51
            'belongsTo' => [
52
                'Groups' => [
53
                    'foreignKey' => 'group_id',
54
                    'className'  => 'Community.Groups'
55
                ]
56
            ]
57
        ]);
58
    }
59
60
    /**
61
     * Default validation rules.
62
     *
63
     * @param Validator $validator
64
     * @return Validator
65
     */
66
    public function validationDefault(Validator $validator)
67
    {
68
        $validator
69
            ->requirePresence('login')
70
            ->notEmpty('login', __d('community', 'Login could not be empty.'))
71
            ->add('login', 'unique', [
72
                'message'  => __d('community', 'User with this login already exists.'),
73
                'rule'     => 'validateUnique',
74
                'provider' => 'table',
75
            ])
76
            ->add('login', 'length', [
77
                'rule'    => ['minLength', MIN_LENGTH_LOGIN],
78
                'message' => __d('community', 'The minimum login length must be {0} characters', MIN_LENGTH_LOGIN)
79
            ]);
80
81
        $validator
82
            ->requirePresence('slug')
83
            ->notEmpty('slug', __d('community', 'Alias could not be empty.'))
84
            ->add('slug', 'unique', [
85
                'message'  => __d('community', 'User with this alias already exists.'),
86
                'rule'     => 'validateUnique',
87
                'provider' => 'table',
88
            ])
89
            ->add('slug', 'length', [
90
                'rule'    => ['minLength', MIN_LENGTH_LOGIN],
91
                'message' => __d('community', 'The minimum alias length must be {0} characters', MIN_LENGTH_LOGIN)
92
            ]);
93
94
        $validator
95
            ->requirePresence('group_id')
96
            ->notEmpty('group_id', __d('community', 'Please, choose user group.'))
97
            ->notEmpty('name', __d('community', 'Please, enter you full name.'));
98
99
        $validator
100
            ->notEmpty('email', __d('community', 'Please, enter you email.'))
101
            ->add('email', 'unique', [
102
                'provider' => 'table',
103
                'rule'     => 'validateUnique',
104
                'message'  => __d('community', 'User with this email already exists.'),
105
            ])
106
            ->add('email', 'valid', [
107
                'rule'    => 'email',
108
                'message' => __d('community', 'Please enter valid email.'),
109
            ]);
110
111
        $validator
112
            ->notEmpty('password', __d('community', 'Please, enter you password.'))
113
            ->add('password', 'minLength', [
114
                'rule'    => ['minLength', MIN_LENGTH_PASS],
115
                'message' => __d('community', 'The minimum password length is {0}', MIN_LENGTH_PASS),
116
            ]);
117
118
        $validator
119
            ->notEmpty('password_confirm', __d('community', 'Please, confirm you password.'))
120
            ->add('password_confirm', 'no-misspelling', [
121
                'rule'    => ['compareWith', 'password'],
122
                'message' => __d('community', 'Passwords are not equal'),
123
            ]);
124
125
        return $validator;
126
    }
127
}
128