Validator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 128
rs 10
c 1
b 0
f 0
wmc 13
lcom 1
cbo 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 39 5
A check() 0 19 3
A getError() 0 4 1
A buildValidator() 0 17 4
1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Validator;
17
18
use Hogosha\Monitor\Exception\ValidatorException;
19
use Symfony\Component\OptionsResolver\Options;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
22
/**
23
 * Class Validator.
24
 */
25
class Validator
26
{
27
    /**
28
     * $type.
29
     *
30
     * @var string
31
     */
32
    protected $type;
33
34
    /**
35
     * $match.
36
     *
37
     * @var string
38
     */
39
    protected $match;
40
41
    /**
42
     * $error.
43
     *
44
     * @var string
45
     */
46
    protected $error;
47
48
    /**
49
     * Constructor.
50
     *
51
     * @param array|null $options
52
     */
53
    public function __construct(array $options = array())
54
    {
55
        $resolver = new OptionsResolver();
56
        $resolver->setRequired(
57
            [
58
                'type',
59
                'match',
60
            ]
61
        );
62
63
        $resolver->setDefault('type', function (Options $options, $value) {
64
            if (null === $value) {
65
                return;
66
            }
67
        });
68
69
        $resolver->setDefault('match', function (Options $options, $value) {
70
            if (null === $value) {
71
                return;
72
            }
73
        });
74
75
        $resolver->setAllowedValues('type', function ($value) {
76
            if (in_array($value, ['json', 'xml', 'html'])) {
77
                return true;
78
            }
79
80
            if (null == $value) {
81
                return  true;
82
            }
83
        });
84
85
        $resolver->setAllowedTypes('type', ['string', 'null']);
86
87
        $options = $resolver->resolve($options);
88
89
        $this->type = $options['type'];
90
        $this->match = $options['match'];
91
    }
92
93
    /**
94
     * check.
95
     *
96
     * @param string $value
97
     *
98
     * @return bool
99
     */
100
    public function check($value)
101
    {
102
        $validator = $this->buildValidator();
103
104
        // If no validator then the result is true
105
        if (null == $validator) {
106
            return;
107
        }
108
109
        try {
110
            $validator->check($value, $this->match);
111
112
            return true;
113
        } catch (ValidatorException $e) {
114
            $this->error = $e->getMessage();
115
116
            return false;
117
        }
118
    }
119
120
    /**
121
     * getError.
122
     *
123
     * @return string
124
     */
125
    public function getError()
126
    {
127
        return $this->error;
128
    }
129
130
    /**
131
     * buildValidator.
132
     *
133
     * @return ValidatorInterface
134
     */
135
    private function buildValidator()
136
    {
137
        switch ($this->type) {
138
            case 'json':
139
                return new JsonValidator();
140
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
141
            case 'html':
142
                return new HtmlValidator();
143
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
144
            case 'xml':
145
                return new XmlValidator();
146
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
147
            default:
148
                return;
149
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
150
        }
151
    }
152
}
153