FileClientFactory   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 54
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A registerKnownTypes() 0 9 2
A register() 0 4 1
A isRegistered() 0 5 1
A getClassName() 0 8 2
A make() 0 8 2
1
<?php
2
3
namespace ConfigToken\FileClient;
4
5
6
use ConfigToken\ConnectionSettings\ConnectionSettingsInterface;
7
use ConfigToken\FileClient\Exception\UnknownServerTypeException;
8
use ConfigToken\FileClient\Types\GitLabRepoClient;
9
use ConfigToken\FileClient\Types\LocalFileClient;
10
use ConfigToken\FileClient\Types\RemoteFileClient;
11
12
class FileClientFactory
13
{
14
    /** @var string[] */
15
    protected static $registeredByServerType = array();
16
17
    protected static function registerKnownTypes()
18
    {
19
        if (!empty(static::$registeredByServerType)) {
20
            return;
21
        }
22
        static::register(LocalFileClient::getServerType(), LocalFileClient::getClassName());
23
        static::register(RemoteFileClient::getServerType(), RemoteFileClient::getClassName());
24
        static::register(GitLabRepoClient::getServerType(), GitLabRepoClient::getClassName());
25
    }
26
27
    public static function register($serverType, $clientClassName)
28
    {
29
        static::$registeredByServerType[$serverType] = $clientClassName;
30
    }
31
32
    /**
33
     * @param string $serverType
34
     * @return boolean
35
     */
36
    public static function isRegistered($serverType)
37
    {
38
        static::registerKnownTypes();
39
        return isset(static::$registeredByServerType[$serverType]);
40
    }
41
42
    /**
43
     * @param string $serverType
44
     * @return FileClientInterface
45
     *
46
     * @throws UnknownServerTypeException
47
     */
48
    public static function getClassName($serverType)
49
    {
50
        static::registerKnownTypes();
51
        if (isset(static::$registeredByServerType[$serverType])) {
52
            return static::$registeredByServerType[$serverType];
53
        }
54
        throw new UnknownServerTypeException($serverType);
55
    }
56
57
    public static function make($serverType, ConnectionSettingsInterface $connectionSettings = null)
58
    {
59
        if (!static::isRegistered($serverType)) {
60
            throw new UnknownServerTypeException($serverType);
61
        }
62
        $clientClassName = static::getClassName($serverType);
63
        return new $clientClassName($connectionSettings);
64
    }
65
}