1 | <?php |
||
2 | /** |
||
3 | * Copyright (C) 2018 Gerrit Addiks. |
||
4 | * This package (including this file) was released under the terms of the GPL-3.0. |
||
5 | * You should have received a copy of the GNU General Public License along with this program. |
||
6 | * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
||
7 | * @license GPL-3.0 |
||
8 | * @author Gerrit Addiks <[email protected]> |
||
9 | */ |
||
10 | |||
11 | namespace Addiks\RDMBundle\DataLoader; |
||
12 | |||
13 | use InvalidArgumentException; |
||
14 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
15 | use Addiks\RDMBundle\DataLoader\DataLoaderInterface; |
||
16 | use Addiks\RDMBundle\DataLoader\DataLoaderFactoryInterface; |
||
17 | use ErrorException; |
||
18 | |||
19 | /** |
||
20 | * A simple data-loader-factory that chooses which data-loader to create using a parameter from the container. |
||
21 | */ |
||
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 | private string|null $entityManagerServiceId = null |
||
41 | 6 | ) { |
|
42 | if ($container->hasParameter($choosingParameterName)) { |
||
43 | 3 | /** @var string $determinator */ |
|
44 | $determinator = $container->getParameter($choosingParameterName); |
||
45 | 3 | ||
46 | if (!isset($serviceIdMap[$determinator])) { |
||
47 | throw new InvalidArgumentException(sprintf( |
||
48 | "Invalid value '%s' for parameter '%s', allowed values are: %s", |
||
49 | $determinator, |
||
50 | $choosingParameterName, |
||
51 | implode(", ", array_keys($serviceIdMap)) |
||
52 | )); |
||
53 | } |
||
54 | 3 | ||
55 | $serviceId = $serviceIdMap[$determinator]; |
||
56 | |||
57 | } else { |
||
58 | 3 | /** @var string $serviceId */ |
|
59 | $serviceId = $defaultServiceId; |
||
60 | } |
||
61 | |||
62 | 6 | # Spot errors early (this should not actually load the service) |
|
63 | if (!$container->has($serviceId)) { |
||
64 | throw new InvalidArgumentException(sprintf( |
||
65 | "The specified service '%s' does not exist!", |
||
66 | $serviceId |
||
67 | )); |
||
68 | } |
||
69 | 6 | ||
70 | 6 | $this->container = $container; |
|
71 | $this->serviceId = $serviceId; |
||
72 | } |
||
73 | 6 | ||
74 | public function createDataLoader(): DataLoaderInterface |
||
75 | { |
||
76 | 6 | /** @var object $dataLoader */ |
|
77 | $dataLoader = $this->container->get($this->serviceId); |
||
78 | 6 | ||
79 | if (!$dataLoader instanceof DataLoaderInterface) { |
||
80 | throw new ErrorException(sprintf( |
||
81 | "Expected service with id '%s' to be of type %s!", |
||
82 | $this->serviceId, |
||
83 | DataLoaderInterface::class |
||
84 | )); |
||
85 | } |
||
86 | 6 | ||
87 | if (!empty($this->entityManagerServiceId)) { |
||
88 | $dataLoader->boot(function () { |
||
89 | return $this->container->get($this->entityManagerServiceId); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
90 | }); |
||
91 | } |
||
92 | |||
93 | return $dataLoader; |
||
94 | } |
||
95 | |||
96 | } |
||
97 |