Test Failed
Pull Request — master (#147)
by Simone
01:55
created

Ruler   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 19
c 0
b 0
f 0
lcom 1
cbo 7
dl 0
loc 92
ccs 36
cts 45
cp 0.8
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromResourceAndKey() 0 4 1
A getRuleFromResource() 0 10 2
A isNotCustom() 0 6 1
A typeMismatch() 0 5 2
A classMismatch() 0 6 3
A ensureClassIsValid() 0 11 3
A ensureTypeIsValid() 0 16 3
A ensureRuleTypeIsEmail() 0 12 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\Rulers;
13
14
use Sensorario\Resources\Resource;
15
use Sensorario\Resources\Validators\Interfaces\Validator;
16
use Egulias\EmailValidator\EmailValidator;
17
use Egulias\EmailValidator\Validation\DNSCheckValidation;
18
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
19
use Egulias\EmailValidator\Validation\RFCValidation;
20
21
class Ruler
22
{
23
    private $resource;
24
25
    private $rule;
26
27
    private $fieldName;
28
29 6
    public function __construct(Resource $resource = null, $fieldName = null)
30
    {
31 6
        $this->resource = $resource;
32 6
        $this->fieldName = $fieldName;
33 6
    }
34
35 5
    public static function fromResourceAndKey(Resource $resource, $fieldName) : Ruler
36
    {
37 5
        return new static($resource, $fieldName);
38
    }
39
40 6
    public function getRuleFromResource()
41
    {
42 6
        if (!$this->resource) {
43 1
            throw new \Sensorario\Resources\Exceptions\UndefinedResourceException(
44 1
                'Oops! Cant get rule without resource'
45
            );
46
        }
47
48 5
        $this->rule = $this->resource->getRule($this->fieldName);
49 5
    }
50
51 5
    private function isNotCustom()
52
    {
53 5
        $this->getRuleFromResource();
54
55 5
        return $this->rule->isNotCustom();
56
    }
57
58 5
    public function typeMismatch()
59
    {
60 5
        return $this->isNotCustom()
61 5
            && $this->resource->getFieldType($this->fieldName) !== $this->rule->getRuleType();
62
    }
63
64 4
    public function classMismatch()
65
    {
66 4
        return $this->isNotCustom()
67 4
            && !($this->resource->get($this->fieldName) instanceof \Sensorario\Resources\Resource)
68 4
            && get_class($this->resource->get($this->fieldName)) != $this->rule->getValue();
69
    }
70
71 5
    public function ensureTypeIsValid()
72
    {
73 5
        if ($this->typeMismatch()) {
74 1
            if ($this->resource->isFieldNumericButString($this->fieldName)) {
75
                throw new \Sensorario\Resources\Exceptions\WrongPropertyValueException(
76
                    'Property `' . $this->fieldName . '` must be an integer!'
77
                );
78
            }
79
80 1
            throw new \Sensorario\Resources\Exceptions\AttributeTypeException(
81 1
                'Attribute `' . $this->fieldName
82
                . '` must be of type '
83 1
                . '`' . $this->rule->getExpectedType() . '`'
84
            );
85
        }
86 4
    }
87
88 4
    public function ensureClassIsValid()
89
    {
90 4
        if ($this->classMismatch()) {
91 1
            if ($this->rule->isValueNotAnObject()) {
92 1
                throw new \Sensorario\Resources\Exceptions\NotObjectTypeFoundException(
93 1
                    'Attribute `' . $this->fieldName
94 1
                    . '` must be an object of type ' . $this->rule->getValue()
95
                );
96
            }
97
        }
98 3
    }
99
100
    public function ensureRuleTypeIsEmail()
101
    {
102
        if (!$this->rule) {
103
            $this->getRuleFromResource();
104
        }
105
106
        if ($this->rule->isNotMail()) {
107
            throw new \Sensorario\Resources\Exceptions\InvalidCustomValidatorException(
108
                'Oops! `' . $this->rule->getRuleType() . '` custom validator is not available. Only email is.'
109
            );
110
        }
111
    }
112
}
113