1 | <?php |
||
22 | final class ChoosingDataLoaderFactory implements DataLoaderFactoryInterface |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var ContainerInterface |
||
27 | */ |
||
28 | private $container; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $serviceId; |
||
34 | |||
35 | 6 | public function __construct( |
|
36 | ContainerInterface $container, |
||
37 | array $serviceIdMap, |
||
38 | string $choosingParameterName, |
||
39 | string $defaultServiceId |
||
40 | ) { |
||
41 | 6 | if ($container->hasParameter($choosingParameterName)) { |
|
42 | /** @var string $determinator */ |
||
43 | 3 | $determinator = $container->getParameter($choosingParameterName); |
|
44 | |||
45 | 3 | if (!isset($serviceIdMap[$determinator])) { |
|
46 | throw new InvalidArgumentException(sprintf( |
||
47 | "Invalid value '%s' for parameter '%s', allowed values are: %s", |
||
48 | $determinator, |
||
49 | $choosingParameterName, |
||
50 | implode(", ", array_keys($serviceIdMap)) |
||
51 | )); |
||
52 | } |
||
53 | |||
54 | 3 | $serviceId = $serviceIdMap[$determinator]; |
|
55 | |||
56 | } else { |
||
57 | /** @var string $serviceId */ |
||
58 | 3 | $serviceId = $defaultServiceId; |
|
59 | } |
||
60 | |||
61 | # Spot errors early (this should not actually load the service) |
||
62 | 6 | if (!$container->has($serviceId)) { |
|
63 | throw new InvalidArgumentException(sprintf( |
||
64 | "The specified service '%s' does not exist!", |
||
65 | $serviceId |
||
66 | )); |
||
67 | } |
||
68 | |||
69 | 6 | $this->container = $container; |
|
70 | 6 | $this->serviceId = $serviceId; |
|
71 | 6 | } |
|
72 | |||
73 | 6 | public function createDataLoader(): DataLoaderInterface |
|
88 | |||
89 | } |
||
90 |