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
|
|
|
) { |
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 |
74
|
|
|
{ |
75
|
|
|
/** @var object $dataLoader */ |
76
|
6 |
|
$dataLoader = $this->container->get($this->serviceId); |
77
|
|
|
|
78
|
6 |
|
if (!$dataLoader instanceof DataLoaderInterface) { |
79
|
|
|
throw new ErrorException(sprintf( |
80
|
|
|
"Expected service with id '%s' to be of type %s!", |
81
|
|
|
$this->serviceId, |
82
|
|
|
DataLoaderInterface::class |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
return $dataLoader; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|