BadgesTable::validationCreate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 54
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 9.6716
c 0
b 0
f 0
cc 1
eloc 39
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
namespace App\Model\Table;
3
4
use Cake\ORM\Table;
5
use Cake\Validation\Validator;
6
7
class BadgesTable extends Table
8
{
9
10
    /**
11
     * Initialize method.
12
     *
13
     * @param array $config The configuration for the Table.
14
     *
15
     * @return void
16
     */
17
    public function initialize(array $config)
18
    {
19
        $this->table('badges');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::table() has been deprecated with message: 3.4.0 Use setTable()/getTable() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
20
        $this->displayField('name');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::displayField() has been deprecated with message: 3.4.0 Use setDisplayField()/getDisplayField() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
21
        $this->primaryKey('id');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::primaryKey() has been deprecated with message: 3.4.0 Use setPrimaryKey()/getPrimaryKey() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
22
23
        $this->addBehavior('Timestamp');
24
        $this->addBehavior('Xety/Cake3Upload.Upload', [
25
            'fields' => [
26
                'picture' => [
27
                    'path' => 'img/badges/:md5',
28
                    'overwrite' => true
29
                ]
30
            ]
31
        ]);
32
        $this->addBehavior('Translate', [
33
            'fields' => ['name'],
34
            'translationTable' => 'BadgesI18n'
35
        ]);
36
37
        $this->belongsToMany('Users', [
38
            'foreignKey' => 'badge_id',
39
            'targetForeignKey' => 'user_id',
40
            'joinTable' => 'badges_users',
41
        ]);
42
    }
43
44
    /**
45
     * Create validation rules.
46
     *
47
     * @param \Cake\Validation\Validator $validator Instance of the validator.
48
     *
49
     * @return \Cake\Validation\Validator
50
     */
51
    public function validationCreate(Validator $validator)
52
    {
53
        $validator
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Validation\Validator::provider() has been deprecated with message: 3.4.0 Use setProvider()/getProvider() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
54
            ->provider('upload', 'App\Model\Validation\UploadValidator')
55
            ->notEmpty('name', __("You must select a name."))
56
            ->add('name', 'minLength', [
57
                'rule' => ['minLength', 5],
58
                'message' => __("Please, {0} characters minimum for the name.", 5)
59
            ])
60
            ->allowEmpty('picture_file')
61
            ->add('picture_file', [
62
                'mimeType' => [
63
                    'rule' => ['mimeType', ['image/jpeg', 'image/png']],
64
                    'message' => __("The mimeType is not allowed."),
65
                    'on' => function ($context) {
66
                            return !empty($context['data']['picture_file']['name']);
67
                    }
68
                ],
69
                'fileExtension' => [
70
                    'rule' => ['extension', ['jpg', 'jpeg', 'png']],
71
                    'message' => __("The extension allowed are {0}.", '.jpg, .jpeg and .png'),
72
                    'on' => function ($context) {
73
                            return !empty($context['data']['picture_file']['name']);
74
                    }
75
                ],
76
                'fileSize' => [
77
                    'rule' => ['fileSize', '<', '500KB'],
78
                    'message' => __("The file exceeded the max allowed size of {0}", '500KB'),
79
                    'on' => function ($context) {
80
                            return !empty($context['data']['picture_file']['name']);
81
                    }
82
                ],
83
                'maxDimension' => [
84
                    'rule' => ['maxDimension', 130, 130],
85
                    'provider' => 'upload',
86
                    'message' => __(
87
                        "The file exceeded the max allowed dimension. Max height : {0} Max width : {1}",
88
                        130,
89
                        130
90
                    ),
91
                ]
92
            ])
93
            ->notEmpty('type', __("You must select a type."))
94
            ->add('type', 'inList', [
95
                'rule' => ['inList', ['comments', 'registration']],
96
                'message' => __("This type is not allowed. Allowed types : {0}", implode(", ", ['comments', 'registration'])),
97
            ])
98
            ->notEmpty('rule', __("You must select a rule."))
99
            ->add('rule', 'numeric', [
100
                'rule' => 'numeric'
101
            ]);
102
103
        return $validator;
104
    }
105
}
106