TreeSerializerFactory::internalRegister()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace ConfigToken\TreeSerializer;
4
5
use ConfigToken\Exception\AlreadyRegisteredException;
6
use ConfigToken\TreeSerializer\Exception\UnknownContentTypeException;
7
use ConfigToken\TreeSerializer\Exception\UnknownFileExtensionException;
8
use ConfigToken\TreeSerializer\Types\IniTreeSerializer;
9
use ConfigToken\TreeSerializer\Types\JsonTreeSerializer;
10
use ConfigToken\TreeSerializer\Types\PhpTreeSerializer;
11
use ConfigToken\TreeSerializer\Types\XmlTreeSerializer;
12
use ConfigToken\TreeSerializer\Types\YmlTreeSerializer;
13
14
15
class TreeSerializerFactory
16
{
17
    /** @var TreeSerializerInterface[] */
18
    protected static $registeredByFileExtension = array();
19
    /** @var TreeSerializerInterface[] */
20
    protected static $registeredByContentType = array();
21
22 4
    protected static function registerKnownTypes()
23
    {
24 4
        if (!empty(static::$registeredByContentType)) {
25 4
            return;
26
        }
27 1
        static::internalRegister(new IniTreeSerializer());
28 1
        static::internalRegister(new JsonTreeSerializer());
29 1
        static::internalRegister(new PhpTreeSerializer());
30 1
        static::internalRegister(new XmlTreeSerializer());
31 1
        static::internalRegister(new YmlTreeSerializer());
32 1
    }
33
34
    /**
35
     * @param string $contentType
36
     * @return boolean
37
     */
38 1
    public static function isRegisteredByContentType($contentType)
39
    {
40 1
        static::registerKnownTypes();
41 1
        return isset(static::$registeredByContentType[$contentType]);
42
    }
43
44
    /**
45
     * @param string $contentType
46
     * @return TreeSerializerInterface
47
     *
48
     * @throws UnknownContentTypeException
49
     */
50 3
    public static function getByContentType($contentType)
51
    {
52 3
        static::registerKnownTypes();
53 3
        if (isset(static::$registeredByContentType[$contentType])) {
54 2
            return static::$registeredByContentType[$contentType];
55
        }
56 2
        throw new UnknownContentTypeException($contentType);
57
    }
58
59
    /**
60
     * @param string $fileExtension
61
     * @return boolean
62
     */
63 1
    public static function isRegisteredByFileExtension($fileExtension)
64
    {
65 1
        static::registerKnownTypes();
66 1
        return isset(static::$registeredByFileExtension[$fileExtension]);
67
    }
68
69
    /**
70
     * @param string $fileExtension
71
     * @return TreeSerializerInterface
72
     *
73
     * @throws UnknownFileExtensionException
74
     */
75 2
    public static function getByFileExtension($fileExtension)
76
    {
77 2
        static::registerKnownTypes();
78 2
        if (isset(static::$registeredByFileExtension[$fileExtension])) {
79 1
            return static::$registeredByFileExtension[$fileExtension];
80
        }
81 1
        throw new UnknownFileExtensionException($fileExtension);
82
    }
83
84 2
    public static function isRegistered(TreeSerializerInterface $treeSerializer)
85
    {
86
        try {
87 2
            static::getByContentType($treeSerializer::getContentType());
88 2
            return true;
89 1
        } catch (UnknownContentTypeException $e) {
90 1
            return false;
91
        }
92
    }
93
94
    /**
95
     * Used internally to register a new tree serializer implementation without performing checks.
96
     *
97
     * @param TreeSerializerInterface $treeSerializer
98
     */
99 1
    protected static function internalRegister(TreeSerializerInterface $treeSerializer)
100
    {
101 1
        static::$registeredByContentType[$treeSerializer::getContentType()] = $treeSerializer;
102 1
        static::$registeredByFileExtension[$treeSerializer::getFileExtension()] = $treeSerializer;
103 1
    }
104
105 2
    public static function register(TreeSerializerInterface $treeSerializer)
106
    {
107 2
        if (static::isRegistered($treeSerializer)) {
108 1
            throw new AlreadyRegisteredException(
109 1
                sprintf(
110 1
                    'Tree serializer for content type %s is already registered.',
111 1
                    $treeSerializer::getContentType()
112 1
                )
113 1
            );
114
        }
115 1
        static::internalRegister($treeSerializer);
116
    }
117
}