FilterBuilder   A
last analyzed

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\FilterDoesNotExistException;
21
use Mcustiel\SimpleRequest\Interfaces\FilterInterface;
22
23
/**
24
 * Builds Filter objects from the data taken from an annotation.
25
 *
26
 * @author mcustiel
27
 */
28
class FilterBuilder extends AnnotationToImplementationBuilder
29
{
30
    /**
31
     * This method is used from AnnotationToImplementationBuilder trait. It checks the existence
32
     * of the Filter class and then checks it's of type FilterInterface.
33
     *
34
     * @param string $type The type to instantiate.
35
     *
36
     * @return \Mcustiel\SimpleRequest\Interfaces\FilterInterface The instance created
37
     *
38
     * @throws \Mcustiel\SimpleRequest\Exception\FilterDoesNotExistException
39
     *                                                                       If class does not exist or does not implement FilterInterface
40
     */
41 18
    final protected function getClassForType($type)
42
    {
43 18
        if (!class_exists($type)) {
44 1
            throw new FilterDoesNotExistException("Filter class {$type} does not exist");
45
        }
46 17
        $filter = new $type;
47 17
        if (! ($filter instanceof FilterInterface)) {
48 1
            throw new FilterDoesNotExistException(
49 1
                "Filter class {$type} must implement " . FilterInterface::class
50 1
            );
51
        }
52
53 16
        return $filter;
54
    }
55
}
56