Completed
Push — master ( dc27cd...c0eb3b )
by Rasmus
07:46
created

CheckEmail::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
dl 15
loc 15
ccs 9
cts 9
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 3
crap 4
1
<?php
2
3
namespace mindplay\kissform\Validators;
4
5
use mindplay\kissform\Facets\FieldInterface;
6
use mindplay\kissform\Facets\ValidatorInterface;
7
use mindplay\kissform\InputModel;
8
use mindplay\kissform\InputValidation;
9
use mindplay\lang;
10
11
/**
12
 * Validate e-mail address.
13
 */
14 View Code Duplication
class CheckEmail implements ValidatorInterface
0 ignored issues
show
Duplication introduced by
This class 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...
15
{
16
    /**
17
     * @var string|null
18
     */
19
    private $error;
20
21
    /**
22
     * @param string|null $error optional custom error message
23
     */
24 2
    public function __construct($error = null)
25
    {
26 2
        $this->error = $error;
27 2
    }
28
29 1
    public function validate(FieldInterface $field, InputModel $model, InputValidation $validation)
30
    {
31 1
        $input = $model->getInput($field);
32
33 1
        if ($input === null) {
34 1
            return; // no input
35
        }
36
37 1
        if (filter_var($input, FILTER_VALIDATE_EMAIL) === false) {
38 1
            $model->setError(
39 1
                $field,
40 1
                $this->error ?: lang::text("mindplay/kissform", "email", ["field" => $validation->getLabel($field)])
41
            );
42
        }
43 1
    }
44
}
45