1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mapado\RestClientSdk; |
4
|
|
|
|
5
|
|
|
use Mapado\RestClientSdk\Model\ModelHydrator; |
6
|
|
|
use Mapado\RestClientSdk\Model\Serializer; |
7
|
|
|
use Mapado\RestClientSdk\UnitOfWork; |
8
|
|
|
use ProxyManager\Configuration; |
9
|
|
|
use ProxyManager\Factory\LazyLoadingGhostFactory; |
10
|
|
|
use ProxyManager\Proxy\LazyLoadingInterface; |
11
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Sdk Client |
15
|
|
|
*/ |
16
|
|
|
class SdkClient |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var RestClient |
20
|
|
|
*/ |
21
|
|
|
protected $restClient; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Mapping |
25
|
|
|
*/ |
26
|
|
|
private $mapping; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Serializer |
30
|
|
|
*/ |
31
|
|
|
private $serializer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ModelHydrator |
35
|
|
|
*/ |
36
|
|
|
private $modelHydrator; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $repositoryList = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* proxyManagerConfig |
45
|
|
|
* |
46
|
|
|
* @var Configuration |
47
|
|
|
* @access private |
48
|
|
|
*/ |
49
|
|
|
private $proxyManagerConfig; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Cache item pool. |
53
|
|
|
* |
54
|
|
|
* @var CacheItemPoolInterface |
55
|
|
|
* @access private |
56
|
|
|
*/ |
57
|
|
|
protected $cacheItemPool; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Cache prefix. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
* @access private |
64
|
|
|
*/ |
65
|
|
|
protected $cachePrefix; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* unitOfWork |
69
|
|
|
* |
70
|
|
|
* @var UnitOfWork |
71
|
|
|
* @access private |
72
|
|
|
*/ |
73
|
|
|
private $unitOfWork; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Constructor. |
77
|
|
|
* |
78
|
|
|
* @param RestClient $restClient |
79
|
|
|
* @param Mapping $mapping |
80
|
|
|
* @param Serializer|null $serializer |
81
|
|
|
*/ |
82
|
|
|
public function __construct(RestClient $restClient, Mapping $mapping, UnitOfWork $unitOfWork, Serializer $serializer = null) |
83
|
|
|
{ |
84
|
1 |
|
$this->restClient = $restClient; |
85
|
1 |
|
$this->mapping = $mapping; |
86
|
1 |
|
$this->unitOfWork = $unitOfWork; |
87
|
1 |
|
if (!$serializer) { |
88
|
|
|
$serializer = new Serializer($this->mapping, $this->unitOfWork); |
89
|
|
|
} |
90
|
1 |
|
$this->serializer = $serializer; |
91
|
1 |
|
$this->serializer->setSdk($this); |
92
|
|
|
|
93
|
|
|
|
94
|
1 |
|
$this->modelHydrator = new ModelHydrator($this); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* setCacheItemPool |
99
|
|
|
* |
100
|
|
|
* @param CacheItemPoolInterface $cacheItemPool |
101
|
|
|
* @access public |
102
|
|
|
* @return SdkClient |
103
|
|
|
*/ |
104
|
|
|
public function setCacheItemPool(CacheItemPoolInterface $cacheItemPool, $cachePrefix = '') |
105
|
|
|
{ |
106
|
|
|
$this->cacheItemPool = $cacheItemPool; |
107
|
|
|
$this->cachePrefix = $cachePrefix; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* getCacheItemPool |
114
|
|
|
* |
115
|
|
|
* @return ?CacheItemPoolInterface |
116
|
|
|
*/ |
117
|
|
|
public function getCacheItemPool() |
118
|
|
|
{ |
119
|
|
|
return $this->cacheItemPool; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* getCachePrefix |
124
|
|
|
* |
125
|
|
|
* @access public |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getCachePrefix() |
129
|
|
|
{ |
130
|
|
|
return $this->cachePrefix; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* getRepository |
135
|
|
|
* |
136
|
|
|
* @param string $modelName |
137
|
|
|
* @access public |
138
|
|
|
* @return EntityRepository |
139
|
|
|
*/ |
140
|
|
|
public function getRepository($modelName) |
141
|
|
|
{ |
142
|
|
|
// get repository by key |
143
|
1 |
|
$metadata = $this->mapping->getClassMetadataByKey($modelName); |
144
|
1 |
|
if (!$metadata) { |
145
|
|
|
// get by classname |
146
|
1 |
|
$metadata = $this->mapping->getClassMetadata($modelName); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
$modelName = $metadata->getModelName(); |
150
|
|
|
|
151
|
1 |
|
if (!isset($this->repositoryList[$modelName])) { |
152
|
1 |
|
$repositoryName = $metadata->getRepositoryName() ?: '\Mapado\RestClientSdk\EntityRepository'; |
153
|
1 |
|
$this->repositoryList[$modelName] = new $repositoryName($this, $this->restClient, $this->unitOfWork, $modelName); |
154
|
|
|
} |
155
|
1 |
|
return $this->repositoryList[$modelName]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* getRestClient |
160
|
|
|
* |
161
|
|
|
* @access public |
162
|
|
|
* @return RestClient |
163
|
|
|
*/ |
164
|
|
|
public function getRestClient() |
165
|
|
|
{ |
166
|
1 |
|
return $this->restClient; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* getMapping |
171
|
|
|
* |
172
|
|
|
* @access public |
173
|
|
|
* @return Mapping |
174
|
|
|
*/ |
175
|
|
|
public function getMapping() |
176
|
|
|
{ |
177
|
1 |
|
return $this->mapping; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* getSerializer |
182
|
|
|
* |
183
|
|
|
* @access public |
184
|
|
|
* @return Serializer |
185
|
|
|
*/ |
186
|
|
|
public function getSerializer() |
187
|
|
|
{ |
188
|
1 |
|
return $this->serializer; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* getModelHydrator |
193
|
|
|
* |
194
|
|
|
* @access public |
195
|
|
|
* @return ModelHydrator |
196
|
|
|
*/ |
197
|
|
|
public function getModelHydrator() |
198
|
|
|
{ |
199
|
|
|
return $this->modelHydrator; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* createProxy |
204
|
|
|
* |
205
|
|
|
* @param string $id |
206
|
|
|
* @access public |
207
|
|
|
* @return \ProxyManager\Proxy\GhostObjectInterface |
208
|
|
|
*/ |
209
|
|
|
public function createProxy($id) |
210
|
|
|
{ |
211
|
|
|
$key = $this->mapping->getKeyFromId($id); |
212
|
|
|
$classMetadata = $this->mapping->getClassMetadataByKey($key); |
213
|
|
|
|
214
|
|
|
$modelName = $classMetadata->getModelName(); |
215
|
|
|
|
216
|
|
|
$sdk = $this; |
217
|
|
|
|
218
|
|
|
if ($this->proxyManagerConfig) { |
219
|
|
|
$factory = new LazyLoadingGhostFactory($this->proxyManagerConfig); |
220
|
|
|
} else { |
221
|
|
|
$factory = new LazyLoadingGhostFactory(); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$initializer = function ( |
225
|
|
|
LazyLoadingInterface &$proxy, |
226
|
|
|
$method, |
227
|
|
|
array $parameters, |
228
|
|
|
& $initializer |
229
|
|
|
) use ( |
230
|
|
|
$sdk, |
231
|
|
|
$classMetadata, |
232
|
|
|
$id |
233
|
|
|
) { |
234
|
|
|
$isAllowedMethod = $method === 'getId' |
235
|
|
|
|| $method === 'setId' |
236
|
|
|
|| $method === 'jsonSerialize' |
237
|
|
|
|| ($method === '__isset' && $parameters['name'] === 'id'); |
238
|
|
|
|
239
|
|
|
if (!$isAllowedMethod) { |
240
|
|
|
$initializer = null; // disable initialization |
241
|
|
|
// load data and modify the object here |
242
|
|
|
if ($id) { |
243
|
|
|
$repository = $sdk->getRepository($classMetadata->getModelName()); |
244
|
|
|
$model = $repository->find($id); |
245
|
|
|
|
246
|
|
|
$attributeList = $classMetadata->getAttributeList(); |
247
|
|
|
|
248
|
|
|
foreach ($attributeList as $attribute) { |
249
|
|
|
$getter = 'get' . ucfirst($attribute->getAttributeName()); |
250
|
|
|
$setter = 'set' . ucfirst($attribute->getAttributeName()); |
251
|
|
|
$value = $model->$getter(); |
252
|
|
|
$proxy->$setter($value); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return true; // confirm that initialization occurred correctly |
257
|
|
|
} |
258
|
|
|
}; |
259
|
|
|
|
260
|
|
|
$instance = $factory->createProxy($modelName, $initializer); |
261
|
|
|
$instance->setId($id); |
|
|
|
|
262
|
|
|
|
263
|
|
|
return $instance; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Setter for fileCachePath |
268
|
|
|
* |
269
|
|
|
* @param string $fileCachePath |
270
|
|
|
* @return SdkClient |
271
|
|
|
*/ |
272
|
|
|
public function setFileCachePath($fileCachePath) |
273
|
|
|
{ |
274
|
|
|
$this->proxyManagerConfig = new Configuration(); |
275
|
|
|
$this->proxyManagerConfig->setProxiesTargetDir($fileCachePath); |
276
|
|
|
|
277
|
|
|
return $this; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.