|
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
|
|
|
} |