1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace W2w\Lib\Apie\Core\ResourceFactories; |
5
|
|
|
|
6
|
|
|
use W2w\Lib\Apie\Exceptions\CouldNotConstructApiResourceClassException; |
7
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourceFactoryInterface; |
8
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourcePersisterInterface; |
9
|
|
|
use W2w\Lib\Apie\Interfaces\ApiResourceRetrieverInterface; |
10
|
|
|
|
11
|
|
|
class ChainableFactory implements ApiResourceFactoryInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ApiResourceRetrieverInterface[] |
15
|
|
|
*/ |
16
|
|
|
private $instantiatedRetrievers = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ApiResourcePersisterInterface[] |
20
|
|
|
*/ |
21
|
|
|
private $instantiatedPersisters = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ApiResourceFactoryInterface[] |
25
|
|
|
*/ |
26
|
|
|
private $factories; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ApiResourceFactoryInterface[] |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $factories) |
32
|
|
|
{ |
33
|
|
|
$this->factories = $factories; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
|
|
public function hasApiResourceRetrieverInstance(string $identifier): bool |
40
|
|
|
{ |
41
|
|
|
if (isset($this->instantiatedRetrievers[$identifier])) { |
42
|
|
|
return true; |
43
|
|
|
} |
44
|
|
|
foreach ($this->factories as $factory) { |
45
|
|
|
if ($factory->hasApiResourceRetrieverInstance($identifier)) { |
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function getApiResourceRetrieverInstance(string $identifier): ApiResourceRetrieverInterface |
56
|
|
|
{ |
57
|
|
|
if (isset($this->instantiatedRetrievers[$identifier])) { |
58
|
|
|
return $this->instantiatedRetrievers[$identifier]; |
59
|
|
|
} |
60
|
|
|
foreach ($this->factories as $factory) { |
61
|
|
|
if ($factory->hasApiResourceRetrieverInstance($identifier)) { |
62
|
|
|
$res = $factory->getApiResourceRetrieverInstance($identifier); |
63
|
|
|
if (get_class($res) === $identifier && $res instanceof ApiResourcePersisterInterface) { |
64
|
|
|
$this->instantiatedPersisters[$identifier] = $res; |
65
|
|
|
} |
66
|
|
|
return $this->instantiatedRetrievers[$identifier] = $res; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
throw new CouldNotConstructApiResourceClassException($identifier); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function hasApiResourcePersisterInstance(string $identifier): bool |
76
|
|
|
{ |
77
|
|
|
if (isset($this->instantiatedPersisters[$identifier])) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
foreach ($this->factories as $factory) { |
81
|
|
|
if ($factory->hasApiResourcePersisterInstance($identifier)) { |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
|
|
public function getApiResourcePersisterInstance(string $identifier): ApiResourcePersisterInterface |
92
|
|
|
{ |
93
|
|
|
if (isset($this->instantiatedPersisters[$identifier])) { |
94
|
|
|
return $this->instantiatedPersisters[$identifier]; |
95
|
|
|
} |
96
|
|
|
foreach ($this->factories as $factory) { |
97
|
|
|
if ($factory->hasApiResourcePersisterInstance($identifier)) { |
98
|
|
|
$res = $factory->getApiResourcePersisterInstance($identifier); |
99
|
|
|
if (get_class($res) === $identifier && $res instanceof ApiResourceRetrieverInterface) { |
100
|
|
|
$this->instantiatedRetrievers[$identifier] = $res; |
101
|
|
|
} |
102
|
|
|
return $this->instantiatedPersisters[$identifier] = $res; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
throw new CouldNotConstructApiResourceClassException($identifier); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|