-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProviderFactory.php
150 lines (126 loc) · 4.29 KB
/
ProviderFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types=1);
namespace Upmind\ProvisionBase;
use Illuminate\Contracts\Filesystem\Filesystem;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Upmind\ProvisionBase\Exception\InvalidDataSetException;
use Upmind\ProvisionBase\Exception\InvalidProviderConfigurationException;
use Upmind\ProvisionBase\Provider\Contract\HasSystemInfo;
use Upmind\ProvisionBase\Provider\Contract\LogsDebugData;
use Upmind\ProvisionBase\Provider\Contract\ProviderInterface;
use Upmind\ProvisionBase\Registry\Registry;
use Upmind\ProvisionBase\Provider\DataSet\DataSet;
use Upmind\ProvisionBase\Provider\DataSet\SystemInfo;
use Upmind\ProvisionBase\Registry\Data\CategoryRegister;
use Upmind\ProvisionBase\Registry\Data\ProviderRegister;
/**
* Factory class for creating Provider instance/register wrapper instances
*/
class ProviderFactory
{
/**
* @var Registry
*/
protected $registry;
/**
* @var Filesystem
*/
protected $filesystem;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var SystemInfo
*/
protected $systemInfo;
/**
* @param Registry $registry
*/
public function __construct(
Registry $registry,
Filesystem $filesystem,
LoggerInterface $logger,
?SystemInfo $systemInfo = null
) {
$this->registry = $registry;
$this->filesystem = $filesystem;
$this->logger = $logger;
$this->systemInfo = $systemInfo ?? $this->createSystemInfo();
}
/**
* @param string|CategoryRegister $category Category identifier, class or register
* @param string|ProviderRegister $provider Provider identifier, class or register
* @param array|DataSet $providerConfiguration Provider configuration data
*
* @throws \Upmind\ProvisionBase\Exception\InvalidProviderConfigurationException If the given config data is invalid
*
* @return Provider Provider wrapper
*/
public function create($category, $provider, $providerConfiguration): Provider
{
if (!$categoryRegister = $this->registry->getCategory($category)) {
throw new RuntimeException('Category not found');
}
if (!$providerRegister = $categoryRegister->getProvider($provider)) {
throw new RuntimeException('Provider not found');
}
if (!$providerConfiguration instanceof DataSet) {
$dataSetClass = $providerRegister->getConstructor()->getParameter()->getClass();
/** @var DataSet $providerConfiguration */
$providerConfiguration = $dataSetClass::create($providerConfiguration);
}
try {
$providerConfiguration->validateIfNotYetValidated();
} catch (InvalidDataSetException $e) {
throw InvalidProviderConfigurationException::fromInvalidDataSet($e);
}
$providerClass = $providerRegister->getClass();
/** @var ProviderInterface $providerInstance */
$providerInstance = new $providerClass($providerConfiguration);
if ($providerInstance instanceof LogsDebugData) {
$providerInstance->setLogger($this->getLogger());
}
if ($providerInstance instanceof HasSystemInfo) {
$providerInstance->setSystemInfo($this->getSystemInfo());
}
return new Provider($providerRegister, $providerInstance);
}
/**
* Create default system info.
*/
public function createSystemInfo(): SystemInfo
{
$outgoingIp = isset($_SERVER['SERVER_ADDR'])
? $_SERVER['SERVER_ADDR']
: gethostbyname(gethostname() ?: php_uname('n'));
return new SystemInfo([
'outgoing_ips' => [$outgoingIp],
]);
}
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
public function getLogger(): LoggerInterface
{
return $this->logger;
}
public function setFilesystem(Filesystem $filesystem): void
{
$this->filesystem = $filesystem;
}
public function getFilesystem(): Filesystem
{
return $this->filesystem;
}
public function setSystemInfo(SystemInfo $systemInfo): void
{
$this->systemInfo = $systemInfo;
}
public function getSystemInfo(): SystemInfo
{
return $this->systemInfo;
}
}