Test Failed
Pull Request — master (#160)
by Maximo
07:08
created

PasswordValidation::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 23
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 42
ccs 0
cts 34
cp 0
crap 12
rs 9.552
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\Validations;
5
6
use Phalcon\Validation;
7
use Phalcon\Validation\Validator\PresenceOf;
0 ignored issues
show
Bug introduced by
The type Phalcon\Validation\Validator\PresenceOf was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Phalcon\Validation\Validator\Confirmation;
9
use Phalcon\Validation\Validator\StringLength;
0 ignored issues
show
Bug introduced by
The type Phalcon\Validation\Validator\StringLength was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Exception;
11
12
class PasswordValidation
13
{
14
    /**
15
     * Validate the password given.
16
     *
17
     * @param string $newPassword
18
     * @param string $verifyPassword
19
     * @return boolean
20
     */
21
    public static function validate(string $newPassword, string $verifyPassword): bool
22
    {
23
        $data = [
24
            'new_password' => $newPassword,
25
            'verify_password' => $verifyPassword,
26
        ];
27
28
        //Ok let validate user password
29
        $validation = new Validation();
30
31
        $validation->add(
32
            'new_password',
33
            new PresenceOf([
34
                'message' => 'The password is required.'
35
            ])
36
        );
37
38
        $validation->add(
39
            'new_password',
40
            new StringLength([
41
                'min' => 8,
42
                'messageMinimum' => 'Password is too short. Minimum 8 characters.',
43
            ])
44
        );
45
46
        $validation->add(
47
            'new_password',
48
            new Confirmation([
49
                'message' => 'New password and confirmation do not match.',
50
                'with' => 'verify_password',
51
            ])
52
        );
53
54
        //validate this form for password
55
        $messages = $validation->validate($data);
56
        if (count($messages)) {
57
            foreach ($messages as $message) {
58
                throw new Exception($message->getMessage());
59
            }
60
        }
61
62
        return true;
63
    }
64
}
65