1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apie\MockPlugin\ResourceFactories; |
4
|
|
|
|
5
|
|
|
use Apie\Core\Apie; |
6
|
|
|
use Apie\Core\Interfaces\ApiResourceFactoryInterface; |
7
|
|
|
use Apie\Core\Interfaces\ApiResourcePersisterInterface; |
8
|
|
|
use Apie\Core\Interfaces\ApiResourceRetrieverInterface; |
9
|
|
|
use Apie\Core\ResourceFactories\ChainableFactory; |
10
|
|
|
use Apie\MockPlugin\DataLayers\MockApiResourceDataLayer; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ApiResourceFactoryInterface implementation of a mock REST server. We require the original ApiResourceFactory to |
15
|
|
|
* find out which methods are allowed (GET, POST, PUT etc.). |
16
|
|
|
* |
17
|
|
|
* @TODO: add some hook to have some control over the mocking... |
18
|
|
|
*/ |
19
|
|
|
final class MockApiResourceFactory implements ApiResourceFactoryInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var MockApiResourceDataLayer |
23
|
|
|
*/ |
24
|
|
|
private $retriever; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Apie|ApiResourceFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $apie; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string[] |
33
|
|
|
*/ |
34
|
|
|
private $skippedResources; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param MockApiResourceDataLayer $retriever |
38
|
|
|
* @param Apie|ApiResourceFactoryInterface Apie |
39
|
|
|
* @param string[] $skippedResources resources that are not allowed to be mocked. |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
MockApiResourceDataLayer $retriever, |
43
|
|
|
$apie, |
44
|
|
|
array $skippedResources |
45
|
|
|
) { |
46
|
|
|
$this->retriever = $retriever; |
47
|
|
|
$this->apie = $apie; |
48
|
|
|
$this->skippedResources = $skippedResources; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
|
|
public function getApiResourceRetrieverInstance(string $identifier): ApiResourceRetrieverInterface |
55
|
|
|
{ |
56
|
|
|
$retriever = $this->getFactory()->getApiResourceRetrieverInstance($identifier); |
57
|
|
|
if (!in_array(get_class($retriever), $this->skippedResources)) { |
58
|
|
|
return $this->retriever; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $retriever; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
|
|
public function getApiResourcePersisterInstance(string $identifier): ApiResourcePersisterInterface |
68
|
|
|
{ |
69
|
|
|
$persister = $this->getFactory()->getApiResourcePersisterInstance($identifier); |
70
|
|
|
if (!in_array(get_class($persister), $this->skippedResources)) { |
71
|
|
|
return $this->retriever; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $persister; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
|
|
public function hasApiResourceRetrieverInstance(string $identifier): bool |
81
|
|
|
{ |
82
|
|
|
return $this->getFactory()->hasApiResourceRetrieverInstance($identifier); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
public function hasApiResourcePersisterInstance(string $identifier): bool |
89
|
|
|
{ |
90
|
|
|
return $this->getFactory()->hasApiResourcePersisterInstance($identifier); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function getFactory(): ApiResourceFactoryInterface |
94
|
|
|
{ |
95
|
|
|
if ($this->apie instanceof ApiResourceFactoryInterface) { |
96
|
|
|
return $this->apie; |
97
|
|
|
} |
98
|
|
|
$factory = $this->apie->getApiResourceFactory(); |
99
|
|
|
$prop = (new ReflectionClass($factory))->getProperty('factories'); |
100
|
|
|
$prop->setAccessible(true); |
101
|
|
|
$factories = $prop->getValue($factory); |
102
|
|
|
return $this->apie = new ChainableFactory( |
103
|
|
|
array_filter($factories, function (ApiResourceFactoryInterface $factory) { |
104
|
|
|
return !$factory instanceof MockApiResourceFactory; |
105
|
|
|
}) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|