Completed
Branch develop (5a78d2)
by Mariano
03:30
created

ValidatorBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 4 Features 3
Metric Value
wmc 3
c 6
b 4
f 3
lcom 0
cbo 2
dl 0
loc 26
ccs 9
cts 9
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getClassForType() 0 14 3
1
<?php
2
/**
3
 * This file is part of php-simple-request.
4
 *
5
 * php-simple-request is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * php-simple-request is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with php-simple-request.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace Mcustiel\SimpleRequest\Util;
19
20
use Mcustiel\SimpleRequest\Exception\ValidatorDoesNotExistException;
21
use Mcustiel\SimpleRequest\Interfaces\ValidatorInterface;
22
23
/**
24
 * Builds Validator objects from the data taken from an annotation.
25
 * @author mcustiel
26
 */
27
class ValidatorBuilder extends AnnotationToImplementationBuilder
28
{
29
    /**
30
     * This method is used from AnnotationToImplementationBuilder trait. It checks the existence
31
     * of the Validator class and then checks it's of type ValidatorInterface.
32
     *
33
     * @param  string $type Name of the class to instantiate.
34
     * @throws \Mcustiel\SimpleRequest\Exception\ValidatorDoesNotExistException
35
     *     If class does not exist or is not of type ValidatorInterface.
36
     * @return \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface The created object.
37
     */
38 90
    final protected function getClassForType($type)
39
    {
40 90
        if (!class_exists($type)) {
41 1
            throw new ValidatorDoesNotExistException("Validator class {$type} does not exist");
42
        }
43 89
        $validator = new $type;
44 89
        if (! ($validator instanceof ValidatorInterface)) {
45 1
            throw new ValidatorDoesNotExistException(
46 1
                "Validator class {$type} must implement " . ValidatorInterface::class
47 1
            );
48
        }
49
50 88
        return $validator;
51
    }
52
}
53