-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExtractor.php
86 lines (73 loc) · 2.88 KB
/
Extractor.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
<?php
declare(strict_types=1);
namespace Kiboko\Plugin\Akeneo\Factory;
use Kiboko\Contract\Configurator;
use Kiboko\Plugin\Akeneo;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception as Symfony;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
final readonly class Extractor implements Configurator\FactoryInterface
{
private Processor $processor;
private ConfigurationInterface $configuration;
/** @var iterable<Akeneo\Capacity\CapacityInterface> */
private iterable $capacities;
public function __construct(
private ExpressionLanguage $interpreter = new ExpressionLanguage(),
) {
$this->processor = new Processor();
$this->configuration = new Akeneo\Configuration\Extractor();
$this->capacities = [
new Akeneo\Capacity\Extractor\All($this->interpreter),
new Akeneo\Capacity\Extractor\Get(
new Akeneo\Handler\GetEndpointHandlerFactory($this->interpreter),
),
new Akeneo\Capacity\Extractor\ListPerPage($this->interpreter),
];
}
public function configuration(): ConfigurationInterface
{
return $this->configuration;
}
/**
* @throws Configurator\ConfigurationExceptionInterface
*/
public function normalize(array $config): array
{
try {
return $this->processor->processConfiguration($this->configuration, $config);
} catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) {
throw new Configurator\InvalidConfigurationException($exception->getMessage(), previous: $exception);
}
}
public function validate(array $config): bool
{
try {
$this->normalize($config);
return true;
} catch (Configurator\ConfigurationExceptionInterface) {
return false;
}
}
private function findCapacity(array $config): Akeneo\Capacity\CapacityInterface
{
foreach ($this->capacities as $capacity) {
if ($capacity->applies($config)) {
return $capacity;
}
}
throw new NoApplicableCapacityException(message: 'No capacity was able to handle the configuration.');
}
public function compile(array $config): Repository\Extractor
{
try {
$builder = new Akeneo\Builder\Extractor(
$this->findCapacity($config)->getBuilder($config)
);
} catch (NoApplicableCapacityException $exception) {
throw new Configurator\InvalidConfigurationException(message: 'Your Akeneo API configuration is using some unsupported capacity, check your "type" and "method" properties to a suitable set.', previous: $exception);
}
return new Repository\Extractor($builder);
}
}