RightType   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 51
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A check() 0 20 3
A ensureEmailIsValid() 0 20 3
1
<?php
2
3
/**
4
 * This file is part of sensorario/resources repository
5
 *
6
 * (c) Simone Gentili <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sensorario\Resources\Validators\Validators;
13
14
use Egulias\EmailValidator\EmailValidator;
15
use Egulias\EmailValidator\Validation\DNSCheckValidation;
16
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
17
use Egulias\EmailValidator\Validation\RFCValidation;
18
use Sensorario\Resources\Resource;
19
use Sensorario\Resources\Rulers\Rule;
20
use Sensorario\Resources\Rulers\Ruler;
21
use Sensorario\Resources\Validators\Interfaces\Validator;
22
23
final class RightType implements Validator
24
{
25
    private $validator;
26
27 42
    public function __construct()
28
    {
29 42
        $this->validator = new EmailValidator();
30 42
    }
31
32 42
    public function check(Resource $resource)
33
    {
34 42
        foreach ($resource->properties() as $key => $value) {
35 32
            if ($resource->isRuleNotDefinedFor($key)) {
36 21
                return;
37
            }
38
39 12
            $ruler = Ruler::fromResourceAndKey($resource, $key);
40
41 12
            $rule = $resource->getRule($key);
42
43 12
            $rule->ensureRuleNameIsValid();
44
45 12
            $this->ensureEmailIsValid($rule, $resource, $key, $ruler);
46
47
48 10
            $ruler->ensureTypeIsValid();
49 8
            $ruler->ensureClassIsValid();
50
        }
51 16
    }
52
53 12
    private function ensureEmailIsValid(Rule $rule, Resource $resource, $key, $ruler)
54
    {
55 12
        if ($rule->isCustom()) {
56 6
            $ruler->ensureRuleTypeIsEmail();
57
58 5
            $isValid = $this->validator->isValid(
59 5
                $resource->get($key),
60 5
                new MultipleValidationWithAnd([
61 5
                    new RFCValidation(),
62 5
                    new DNSCheckValidation()
63
                ])
64
            );
65
66 5
            if (false === $isValid) {
67 1
                throw new \Sensorario\Resources\Exceptions\EmailException(
68 1
                    'Oops! Invalid email address'
69
                );
70
            }
71
        }
72 10
    }
73
}
74