Completed
Push — master ( 7c1383...8fd89c )
by Cheren
05:32
created

UsersTable::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
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 Core\Event\EventManager;
20
use Cake\Validation\Validator;
21
use Community\Model\Entity\User;
22
use Cake\Datasource\EntityInterface;
23
24
/**
25
 * Class UsersTable
26
 *
27
 * @method      filterParams(array $query = [])
28
 * @method      User get($primaryKey, $options = [])
29
 * @property    GroupsTable $Groups
30
 *
31
 * @package     Community\Model\Table
32
 */
33
class UsersTable extends Table
34
{
35
36
    /**
37
     * Initialize a table instance. Called after the constructor.
38
     *
39
     * @param   array $config
40
     * @return  void
41
     *
42
     * @throws  \RuntimeException
43
     */
44 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...
45
    {
46
        parent::initialize($config);
47
48
        $this
49
            ->setPrimaryKey('id')
50
            ->setTable(CMS_TABLE_USERS)
51
            ->setDisplayField('name');
52
53
        $this->addBehavior('Timestamp');
54
        $this->addBehavior('Search.Search');
55
56
        $this->addAssociations([
57
            'belongsTo' => [
58
                'Groups' => [
59
                    'foreignKey' => 'group_id',
60
                    'className'  => 'Community.Groups'
61
                ]
62
            ]
63
        ]);
64
    }
65
66
    /**
67
     * Persists an entity based on the fields that are marked as dirty and
68
     * returns the same entity after a successful save or false in case
69
     * of any error.
70
     *
71
     * @param   \Cake\Datasource\EntityInterface $entity the entity to be saved
72
     * @param   array|\ArrayAccess $options The options to use when saving.
73
     * @return  \Cake\Datasource\EntityInterface|false
74
     */
75
    public function save(EntityInterface $entity, $options = [])
76
    {
77
        EventManager::trigger('Model.User.beforeSave', $this, [
78
            'user' => $entity
79
        ]);
80
81
        $success = parent::save($entity, $options);
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 75 can also be of type object<ArrayAccess>; however, Cake\ORM\Table::save() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
82
83
        EventManager::trigger('Model.User.afterSave', $this, [
84
            'user'    => $entity,
85
            'success' => $success
86
        ]);
87
88
        return $success;
89
    }
90
91
    /**
92
     * Default validation rules.
93
     *
94
     * @param   Validator $validator
95
     * @return  Validator
96
     */
97
    public function validationDefault(Validator $validator)
98
    {
99
        $validator
100
            ->requirePresence('login', 'create')
101
            ->notEmpty('login', __d('community', 'Login could not be empty.'))
102
            ->add('login', 'unique', [
103
                'provider' => 'table',
104
                'rule'     => 'validateUnique',
105
                'message'  => __d('community', 'User with this login already exists.')
106
            ])
107
            ->add('login', 'length', [
108
                'rule'    => ['minLength', MIN_LENGTH_LOGIN],
109
                'message' => __d('community', 'The minimum login length must be {0} characters', MIN_LENGTH_LOGIN)
110
            ]);
111
112
        $validator
113
            ->requirePresence('slug', 'create')
114
            ->notEmpty('slug', __d('community', 'Alias could not be empty.'))
115
            ->add('slug', 'unique', [
116
                'provider' => 'table',
117
                'rule'     => 'validateUnique',
118
                'message'  => __d('community', 'User with this alias already exists.')
119
            ])
120
            ->add('slug', 'length', [
121
                'rule'    => ['minLength', MIN_LENGTH_LOGIN],
122
                'message' => __d('community', 'The minimum alias length must be {0} characters', MIN_LENGTH_LOGIN)
123
            ]);
124
125
        $validator
126
            ->requirePresence('group_id', 'create')
127
            ->notEmpty('group_id', __d('community', 'Please, choose user group.'))
128
            ->notEmpty('name', __d('community', 'Please, enter you full name.'));
129
130
        $validator
131
            ->notEmpty('email', __d('community', 'Please, enter you email.'))
132
            ->add('email', 'unique', [
133
                'provider' => 'table',
134
                'rule'     => 'validateUnique',
135
                'message'  => __d('community', 'User with this email already exists.')
136
            ])
137
            ->add('email', 'valid', [
138
                'rule'    => 'email',
139
                'message' => __d('community', 'Please enter valid email.')
140
            ]);
141
142
        $validator
143
            ->notEmpty('password', __d('community', 'Please, enter you password.'))
144
            ->add('password', 'minLength', [
145
                'rule'    => ['minLength', MIN_LENGTH_PASS],
146
                'message' => __d('community', 'The minimum password length is {0}', MIN_LENGTH_PASS)
147
            ]);
148
149
        $validator
150
            ->notEmpty('password_confirm', __d('community', 'Please, confirm you password.'))
151
            ->add('password_confirm', 'no-misspelling', [
152
                'rule'    => ['compareWith', 'password'],
153
                'message' => __d('community', 'Passwords are not equal')
154
            ]);
155
156
        return $validator;
157
    }
158
}
159