Completed
Pull Request — master (#109)
by Simone
02:43
created

Ruler   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 2
dl 0
loc 77
rs 10
c 0
b 0
f 0

8 Methods

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