ConnectionSettingsFactory   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 0
loc 86
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerKnownTypes() 0 10 2
A register() 0 8 2
A isRegistered() 0 9 2
A getClassName() 0 11 3
A make() 0 8 2
1
<?php
2
3
namespace ConfigToken\ConnectionSettings;
4
5
6
use ConfigToken\ConnectionSettings\Types\GenericConnectionSettings;
7
use ConfigToken\ConnectionSettings\Types\GitLabRepoConnectionSettings;
8
use ConfigToken\ConnectionSettings\Types\LocalFileConnectionSettings;
9
use ConfigToken\ConnectionSettings\Types\RemoteFileConnectionSettings;
10
use ConfigToken\FileClient\Exception\UnknownServerTypeException;
11
use ConfigToken\FileClient\FileClientInterface;
12
13
class ConnectionSettingsFactory
14
{
15
    /** @var string[] */
16
    protected static $registeredByServerType = array();
17
    /** @var string|null */
18
    protected static $fallbackClassName = null;
19
20
    protected static function registerKnownTypes()
21
    {
22
        if (!empty(static::$registeredByServerType)) {
23
            return;
24
        }
25
        static::register(GenericConnectionSettings::getServerType(), GenericConnectionSettings::getClassName());
26
        static::register(LocalFileConnectionSettings::getServerType(), LocalFileConnectionSettings::getClassName());
27
        static::register(RemoteFileConnectionSettings::getServerType(), RemoteFileConnectionSettings::getClassName());
28
        static::register(GitLabRepoConnectionSettings::getServerType(), GitLabRepoConnectionSettings::getClassName());
29
    }
30
31
    /**
32
     * Register the given class name for the given server type.
33
     *
34
     * @param string|null $serverType If null, register fallback class name.
35
     * @param $connectionSettingsClassName
36
     */
37
    public static function register($serverType, $connectionSettingsClassName)
38
    {
39
        if (isset($serverType)) {
40
            static::$registeredByServerType[$serverType] = $connectionSettingsClassName;
41
        } else {
42
            static::$fallbackClassName = $connectionSettingsClassName;
43
        }
44
    }
45
46
    /**
47
     * Check if there is a class name registered for the given server type.
48
     *
49
     * @param string $serverType
50
     * @return boolean
51
     */
52
    public static function isRegistered($serverType)
53
    {
54
        static::registerKnownTypes();
55
        if (isset($serverType)) {
56
            return isset(static::$registeredByServerType[$serverType]);
57
        } else {
58
            return isset(static::$fallbackClassName);
59
        }
60
    }
61
62
    /**
63
     * Get the class name corresponding to the given server type.
64
     *
65
     * @param string $serverType
66
     * @return FileClientInterface
67
     *
68
     * @throws UnknownServerTypeException
69
     */
70
    public static function getClassName($serverType)
71
    {
72
        static::registerKnownTypes();
73
        if (isset(static::$registeredByServerType[$serverType])) {
74
            return static::$registeredByServerType[$serverType];
75
        }
76
        if (isset(static::$fallbackClassName)) {
77
            return static::$fallbackClassName;
78
        }
79
        throw new UnknownServerTypeException($serverType);
80
    }
81
82
    /**
83
     * Create a ConnectionSettings instance for the given server type.
84
     *
85
     * @param string $serverType
86
     * @param array|null $parameters
87
     * @return mixed
88
     * @throws UnknownServerTypeException
89
     */
90
    public static function make($serverType, $parameters = null)
91
    {
92
        if (!static::isRegistered($serverType)) {
93
            throw new UnknownServerTypeException($serverType);
94
        }
95
        $connectionSettingsClassName = static::getClassName($serverType);
96
        return new $connectionSettingsClassName($parameters);
97
    }
98
}