Repository navigation
Expand file tree
/
Copy pathConfig.php
More file actions
177 lines (155 loc) · 5.03 KB
/
Copy pathConfig.php
File metadata and controls
177 lines (155 loc) · 5.03 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types=1);
namespace Qameta\Allure\PHPUnit\Internal;
use Qameta\Allure\Hook\LifecycleHookInterface;
use Qameta\Allure\PHPUnit\Setup\ThreadDetectorInterface;
use Qameta\Allure\Setup\LinkTemplateInterface;
use RuntimeException;
use function class_exists;
use function is_a;
use function is_array;
use function is_callable;
use function is_int;
use function is_string;
final class Config implements ConfigInterface
{
public function __construct(
private readonly array $data,
) {
}
#[\Override]
public function getOutputDirectory(): ?string
{
$key = 'outputDirectory';
if (!isset($this->data[$key])) {
return null;
}
/** @psalm-var mixed $outputDirectory */
$outputDirectory = $this->data[$key];
return is_string($outputDirectory)
? $outputDirectory
: throw new RuntimeException("Config key \"{$key}\" should contain a string");
}
/**
* @return array<string, LinkTemplateInterface>
*/
#[\Override]
public function getLinkTemplates(): array
{
$key = 'linkTemplates';
$linkTemplates = [];
/** @psalm-var mixed $linkTemplateSource */
foreach ($this->getArrayFromData($key) as $linkKey => $linkTemplateSource) {
if (!is_string($linkKey)) {
throw new RuntimeException(
"Config key \"{$key}\" should contain an array with string keys only",
);
}
$linkTemplates[$linkKey] = $this->buildObject(
"{$key}/{$linkKey}",
$linkTemplateSource,
LinkTemplateInterface::class,
);
}
return $linkTemplates;
}
private function getArrayFromData(string $key): array
{
/** @psalm-var mixed $source */
$source = $this->data[$key] ?? [];
return is_array($source)
? $source
: throw new RuntimeException("Config key \"{$key}\" should contain an array");
}
/**
* @template T
* @param string $key
* @param mixed $source
* @param class-string<T> $expectedClass
* @return T
* @psalm-suppress MixedMethodCall
*/
private function buildObject(string $key, mixed $source, string $expectedClass): object
{
return match (true) {
$source instanceof $expectedClass => $source,
$this->isExpectedClassName($source, $expectedClass) => new $source(),
is_callable($source) => $this->buildObject($key, $source(), $expectedClass),
default => throw new RuntimeException(
"Config key \"{$key}\" contains invalid source of {$expectedClass}",
),
};
}
/**
* @template T
* @param mixed $source
* @param class-string<T> $expectedClass
* @return bool
* @psalm-assert-if-true class-string<T> $source
*/
private function isExpectedClassName(mixed $source, string $expectedClass): bool
{
return $this->isClassName($source) && is_a($source, $expectedClass, true);
}
/**
* @psalm-assert-if-true class-string $source
*/
private function isClassName(mixed $source): bool
{
return is_string($source) && class_exists($source);
}
#[\Override]
public function getSetupHook(): ?callable
{
$key = 'setupHook';
/** @psalm-var mixed $source */
$source = $this->data[$key] ?? null;
return isset($source)
? $this->buildCallable($key, $source)
: null;
}
/**
* @psalm-suppress MixedMethodCall
*/
private function buildCallable(string $key, mixed $source): callable
{
return match (true) {
is_callable($source) => $source,
$this->isClassName($source) => $this->buildCallable($key, new $source()),
default => throw new RuntimeException("Config key \"{$key}\" should contain a callable"),
};
}
#[\Override]
public function getThreadDetector(): ?ThreadDetectorInterface
{
$key = 'threadDetector';
/** @var mixed $threadDetector */
$threadDetector = $this->data[$key] ?? null;
return isset($threadDetector)
? $this->buildObject($key, $threadDetector, ThreadDetectorInterface::class)
: null;
}
/**
* @return list<LifecycleHookInterface>
*/
#[\Override]
public function getLifecycleHooks(): array
{
$key = 'lifecycleHooks';
$hooks = [];
/** @psalm-var mixed $hookSource */
foreach ($this->getArrayFromData($key) as $index => $hookSource) {
if (!is_int($index)) {
throw new RuntimeException(
"Config key \"{$key}\" should contain an array with integer keys only",
);
}
$hooks[] = $this->buildObject(
"{$key}/{$index}",
$hookSource,
LifecycleHookInterface::class,
);
}
return $hooks;
}
}