Passed
Branch master (7e4b7e)
by Mariano
04:51
created

ValidatorBuilder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 4 Features 3
Metric Value
wmc 3
c 7
b 4
f 3
lcom 0
cbo 2
dl 0
loc 28
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
 *
26
 * @author mcustiel
27
 */
28
class ValidatorBuilder extends AnnotationToImplementationBuilder
29
{
30
    /**
31
     * This method is used from AnnotationToImplementationBuilder trait. It checks the existence
32
     * of the Validator class and then checks it's of type ValidatorInterface.
33
     *
34
     * @param string $type Name of the class to instantiate.
35
     *
36
     * @throws \Mcustiel\SimpleRequest\Exception\ValidatorDoesNotExistException
37
     *                                                                          If class does not exist or is not of type ValidatorInterface.
38
     *
39
     * @return \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface The created object.
40
     */
41 97
    final protected function getClassForType($type)
42
    {
43 97
        if (!class_exists($type)) {
44 1
            throw new ValidatorDoesNotExistException("Validator class {$type} does not exist");
45
        }
46 96
        $validator = new $type;
47 96
        if (! ($validator instanceof ValidatorInterface)) {
48 1
            throw new ValidatorDoesNotExistException(
49 1
                "Validator class {$type} must implement " . ValidatorInterface::class
50 1
            );
51
        }
52
53 95
        return $validator;
54
    }
55
}
56