XrefResolverFactory::register()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 6
1
<?php
2
3
namespace ConfigToken\TreeCompiler\XrefResolver;
4
5
use ConfigToken\Exception\AlreadyRegisteredException;
6
use ConfigToken\TreeCompiler\XrefResolver\Types\InlineXrefResolver;
7
use ConfigToken\TreeCompiler\XrefResolver\Types\UrlXrefResolver;
8
use ConfigToken\TreeCompiler\XrefResolver\Types\LocalFileXrefResolver;
9
use ConfigToken\TreeCompiler\XrefResolver\Exception\UnknownXrefTypeException;
10
11
12
class XrefResolverFactory
13
{
14
    /** @var XrefResolverInterface[] */
15
    protected static $registeredByType = array();
16
17
    /**
18
     * Register all known resolvers.
19
     */
20 7
    protected static function registerKnownTypes()
21
    {
22 7
        if (!empty(static::$registeredByType)) {
23 7
            return;
24
        }
25 1
        static::internalRegister(new UrlXrefResolver());
26 1
        static::internalRegister(new LocalFileXrefResolver());
27 1
        static::internalRegister(new InlineXrefResolver());
28 1
    }
29
30
    /**
31
     * Get a registered resolver for the given type.
32
     *
33
     * @param string $xrefType
34
     * @return XrefResolverInterface
35
     *
36
     * @throws UnknownXrefTypeException
37
     */
38 7 View Code Duplication
    public static function getByType($xrefType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
39
    {
40 7
        static::registerKnownTypes();
41 7
        if (isset(static::$registeredByType[$xrefType])) {
42 7
            return static::$registeredByType[$xrefType];
43
        }
44
        throw new UnknownXrefTypeException($xrefType);
45
    }
46
47
    /**
48
     * Check if the given resolver is registerd.
49
     *
50
     * @param XrefResolverInterface $xrefResolver
51
     * @return boolean
52
     */
53
    public static function isRegistered(XrefResolverInterface $xrefResolver)
54
    {
55
        return (isset(static::$registeredByType[$xrefResolver::getType()]));
56
    }
57
58
    /**
59
     * Used internally to register a new resolver.
60
     *
61
     * @param XrefResolverInterface $xrefResolver
62
     */
63 1
    protected static function internalRegister(XrefResolverInterface $xrefResolver)
64
    {
65 1
        static::$registeredByType[$xrefResolver::getType()] = $xrefResolver;
66 1
    }
67
68
    /**
69
     * Register a new resolver.
70
     *
71
     * @param XrefResolverInterface $xrefResolver
72
     * @throws AlreadyRegisteredException
73
     */
74 View Code Duplication
    public static function register(XrefResolverInterface $xrefResolver)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
75
    {
76
        if (static::isRegistered($xrefResolver)) {
77
            throw new AlreadyRegisteredException(
78
                sprintf(
79
                    'Xref resolver for Xref type %s is already registered.',
80
                    $xrefResolver::getType()
81
                )
82
            );
83
        }
84
        static::internalRegister($xrefResolver);
85
    }
86
}