|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mathielen\ImportEngine\Storage\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Mathielen\ImportEngine\Storage\ServiceStorage; |
|
6
|
|
|
use Mathielen\ImportEngine\ValueObject\StorageSelection; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
8
|
|
|
|
|
9
|
|
|
class ServiceStorageProvider implements StorageProviderInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var array |
|
13
|
|
|
* key = servicename |
|
14
|
|
|
* value = array of methods |
|
15
|
|
|
*/ |
|
16
|
|
|
private $services; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ContainerInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $container; |
|
22
|
|
|
|
|
23
|
6 |
|
public function __construct(ContainerInterface $container, array $services) |
|
24
|
|
|
{ |
|
25
|
6 |
|
$this->container = $container; |
|
26
|
6 |
|
$this->services = $services; |
|
27
|
6 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* (non-PHPdoc). |
|
31
|
|
|
* |
|
32
|
|
|
* @see \Mathielen\ImportEngine\Storage\Provider\StorageProviderInterface::storage() |
|
33
|
|
|
*/ |
|
34
|
2 |
|
public function storage(StorageSelection $selection) |
|
35
|
|
|
{ |
|
36
|
2 |
|
$callable = $selection->getImpl(); |
|
37
|
|
|
|
|
38
|
2 |
|
$arguments = array(); |
|
39
|
2 |
|
if (is_array($callable) && isset($callable['arguments'])) { |
|
40
|
1 |
|
$arguments = $callable['arguments']; |
|
41
|
1 |
|
unset($callable['arguments']); |
|
42
|
1 |
|
} elseif (!is_callable($callable)) { |
|
43
|
|
|
throw new \InvalidArgumentException('StorageSelection must contain a callable or an extended callable (with arguments) as impl'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
return new ServiceStorage($callable, $arguments); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* (non-PHPdoc). |
|
51
|
|
|
* |
|
52
|
|
|
* @see \Mathielen\ImportEngine\Storage\Provider\StorageProviderInterface::select() |
|
53
|
|
|
*/ |
|
54
|
4 |
|
public function select($id = null) |
|
55
|
|
|
{ |
|
56
|
4 |
|
if (!is_array($id) || !isset($id['service']) || !isset($id['method']) || empty($id['service']) || empty($id['method'])) { |
|
57
|
1 |
|
throw new \InvalidArgumentException('Invalid id argument. Must be array and containing at least service and method property.'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
$serviceName = $id['service']; |
|
61
|
3 |
|
if (!isset($this->services[$serviceName])) { |
|
62
|
1 |
|
throw new \InvalidArgumentException("Service '$serviceName' is not registered in StorageProvider."); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
2 |
|
$method = $id['method']; |
|
66
|
2 |
|
if (!empty($this->services[$serviceName]['methods']) && !in_array($method, $this->services[$serviceName]['methods'])) { |
|
67
|
1 |
|
throw new \InvalidArgumentException("Method '$method' is not registered in StorageProvider for service '$serviceName'."); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
$service = $this->container->get($serviceName); |
|
71
|
1 |
|
$callable_with_arguments = array($service, $method, isset($id['arguments']) ? $id['arguments'] : null); |
|
72
|
|
|
|
|
73
|
1 |
|
return new StorageSelection($callable_with_arguments); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|