RouteHandlerResolver::getInstance()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 7
Ratio 63.64 %

Importance

Changes 0
Metric Value
dl 7
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
4
namespace ElementsFramework\DynamicRouting\Service;
5
6
7
use ElementsFramework\DynamicRouting\Exception\HandlerNotFoundException;
8
use ElementsFramework\DynamicRouting\Handler\AbstractRouteTypeHandler;
9
use ReflectionClass;
10
11
class RouteHandlerResolver
12
{
13
14
    /**
15
     * Returns the instance of the wanted handler if registered.
16
     * @param $handlerIdentifier
17
     * @return AbstractRouteTypeHandler
18
     * @throws HandlerNotFoundException
19
     */
20
    public static function getInstance($handlerIdentifier)
21
    {
22 View Code Duplication
        foreach(config('dynamic-routing.handlers') as $handlerClass) {
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...
23
            $handlerMetadata = new ReflectionClass($handlerClass);
24
            if($handlerIdentifier == $handlerMetadata->getShortName()) {
25
                /** @var AbstractRouteTypeHandler $handler */
26
                return new $handlerClass();
27
            }
28
        }
29
        throw HandlerNotFoundException::fromIdentifier($handlerIdentifier);
30
    }
31
32
    /**
33
     * Checks if the handler with the given identifier exists.
34
     * @param $handlerIdentifier
35
     * @return bool
36
     */
37
    public static function handlerExists($handlerIdentifier)
38
    {
39 View Code Duplication
        foreach(config('dynamic-routing.handlers') as $handlerClass) {
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...
40
            $handlerMetadata = new ReflectionClass($handlerClass);
41
            if($handlerIdentifier == $handlerMetadata->getShortName()) {
42
                return true;
43
            }
44
        }
45
46
        return false;
47
    }
48
49
}