Completed
Push — master ( 188ba1...86d637 )
by Samuel
02:55
created

Validator::validate()   C

Complexity

Conditions 12
Paths 7

Size

Total Lines 56
Code Lines 34

Duplication

Lines 9
Ratio 16.07 %

Code Coverage

Tests 41
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 56
ccs 41
cts 41
cp 1
rs 6.7092
cc 12
eloc 34
nc 7
nop 0
crap 12

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kelemen\ApiNette\Validator;
4
5
use Kelemen\ApiNette\Exception\ValidatorException;
6
use Kelemen\ApiNette\Validator\Input\CookieInput;
7
use Kelemen\ApiNette\Validator\Input\FileInput;
8
use Kelemen\ApiNette\Validator\Input\GetInput;
9
use Kelemen\ApiNette\Validator\Input\InputInterface;
10
use Kelemen\ApiNette\Validator\Input\JsonInput;
11
use Kelemen\ApiNette\Validator\Input\PostInput;
12
use Kelemen\ApiNette\Validator\Input\PostRawInput;
13
use Nette\Utils\Validators;
14
15
class Validator
16
{
17
    private $validations;
18
19
    private $validators = [];
20
21
    private $inputs = [];
22
23
    private $errors = [];
24
25 12
    public function __construct()
26
    {
27 12
        $this->inputs = [
28 12
            'get' => new GetInput(),
29 12
            'post' => new PostInput(),
30 12
            'cookie' => new CookieInput(),
31 12
            'file' => new FileInput(),
32 12
            'postRaw' => new PostRawInput(),
33 12
            'json' => new JsonInput()
34 6
        ];
35 12
    }
36
37 12
    public function setValidations(array $validations)
38
    {
39 12
        $this->validations = $validations;
40 12
        $this->errors = []; // Reset errors
41 12
    }
42
43 4
    public function setValidator($name, callable $callback)
44
    {
45 4
        $this->validators[$name] = $callback;
46 4
    }
47
48 12
    public function setInput($name, InputInterface $input)
49
    {
50 12
        $this->inputs[$name] = $input;
51 12
    }
52
53 12
    public function validate()
54
    {
55 12
        foreach ($this->validations as $validation) {
56 12
            if (!isset($this->inputs[$validation->getType()])) {
57 2
                throw new ValidatorException('Type ' . $validation->getType() . ' not registered');
58
            }
59
60 12
            $data = $this->inputs[$validation->getType()]->getData();
61 12
            $rules = explode('|', $validation->getRules());
62
63 12
            if (in_array('required', $rules)) {
64 10
                if (!isset($data[$validation->getKey()])) {
65 2
                    $this->errors[] = 'Validation for ' . $validation->getKey() . ' failed | required';
66 2
                    continue;
67
                }
68
69 8
                unset($rules[array_search('required', $rules)]);
70 4
            }
71
72 12
            if (!isset($data[$validation->getKey()])) {
73 2
                continue;
74
            }
75
76 12
            $value = $data[$validation->getKey()];
77
78 12
            foreach ($rules as $rule) {
79 12
                list($type) = explode(':', $rule);
80
81 12
                if (!isset($this->validators[$type])) {
82 12
                    $result = Validators::is($value, $rule);
83 12 View Code Duplication
                    if (!$result) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
84 2
                        $this->errors[] = 'Validation for ' . $validation->getKey() . '(' . $value . ') failed | ' . $rule;
85 1
                    }
86 12
                    continue;
87
                }
88
89 4
                if (strpos($rule, ':') === false) {
90 4
                    $result = call_user_func($this->validators[$rule], $value);
91 4 View Code Duplication
                    if (!$result) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
92 2
                        $this->errors[] = 'Validation for ' . $validation->getKey() . '(' . $value . ') failed | ' . $rule;
93 1
                    }
94 4
                    continue;
95
                } else {
96 4
                    list($type, $ruleParams) = explode(':', $rule, 2);
97 4
                    $result = call_user_func_array($this->validators[$type], [
98 4
                        'value' => $value,
99 2
                        'ruleParams' => $ruleParams
100 2
                    ]);
101 4 View Code Duplication
                    if (!$result) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
102 2
                        $this->errors[] = 'Validation for ' . $validation->getKey() . '(' . $value . ') failed | ' . $rule;
103 1
                    }
104 8
                    continue;
105
                }
106 6
            }
107 6
        }
108 10
    }
109
110 10
    public function isValid()
111
    {
112 10
        return empty($this->errors);
113
    }
114
115 10
    public function getErrors()
116
    {
117 10
        return $this->errors;
118
    }
119
}
120