1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vox\Webservice\Factory; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
6
|
|
|
use Doctrine\Common\Cache\Cache; |
7
|
|
|
use Metadata\Cache\CacheInterface; |
8
|
|
|
use Metadata\Cache\DoctrineCacheAdapter; |
9
|
|
|
use Metadata\Cache\FileCache; |
10
|
|
|
use Metadata\MetadataFactoryInterface; |
11
|
|
|
use ProxyManager\Configuration; |
12
|
|
|
use RuntimeException; |
13
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
14
|
|
|
use Vox\Metadata\Factory\MetadataFactoryFactory; |
15
|
|
|
use Vox\Metadata\Factory\MetadataFactoryFactoryInterface; |
16
|
|
|
use Vox\Serializer\Factory\SerializerFactory; |
17
|
|
|
use Vox\Serializer\Factory\SerializerFactoryInterface; |
18
|
|
|
use Vox\Webservice\ClientRegistryInterface; |
19
|
|
|
use Vox\Webservice\Event\PersistenceEvents; |
20
|
|
|
use Vox\Webservice\Event\TransactionEventListener; |
21
|
|
|
use Vox\Webservice\EventDispatcher; |
22
|
|
|
use Vox\Webservice\EventDispatcherInterface; |
23
|
|
|
use Vox\Webservice\Metadata\TransferMetadata; |
24
|
|
|
use Vox\Webservice\Proxy\ProxyFactory; |
25
|
|
|
use Vox\Webservice\Proxy\ProxyFactoryInterface; |
26
|
|
|
use Vox\Webservice\Transaction; |
27
|
|
|
use Vox\Webservice\TransferManager; |
28
|
|
|
use Vox\Webservice\TransferManagerInterface; |
29
|
|
|
use Vox\Webservice\WebserviceClientInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class to help the creation of a TransferManager |
33
|
|
|
* |
34
|
|
|
* @author Jhonatan Teixeira <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class TransferManagerBuilder |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var ProxyFactoryInterface |
40
|
|
|
*/ |
41
|
|
|
private $proxyFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $metadataCache; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $cacheDir = '/tmp/cache'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $metadataDriver = 'annotation'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var ClientRegistryInterface |
60
|
|
|
*/ |
61
|
|
|
private $clientRegistry; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $metadataPath; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
private $metadataClassName = TransferMetadata::class; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var bool |
75
|
|
|
*/ |
76
|
|
|
private $debug = false; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var Cache |
80
|
|
|
*/ |
81
|
|
|
private $doctrineCache; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var SerializerFactoryInterface |
85
|
|
|
*/ |
86
|
|
|
private $serializerFactory; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var MetadataFactoryFactoryInterface |
90
|
|
|
*/ |
91
|
|
|
private $metadaFactoryFactory; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var ClientFactory |
95
|
|
|
*/ |
96
|
|
|
private $clientFactory; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var SerializerInterface |
100
|
|
|
*/ |
101
|
|
|
private $serializer; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @var MetadataFactoryInterface |
105
|
|
|
*/ |
106
|
|
|
private $metadaFactory; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @var WebserviceClientInterface |
110
|
|
|
*/ |
111
|
|
|
private $webserviceClient; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @var EventDispatcherInterface |
115
|
|
|
*/ |
116
|
|
|
private $eventDispatcher; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @var bool |
120
|
|
|
*/ |
121
|
|
|
private $isTransactional = false; |
122
|
|
|
|
123
|
9 |
|
public function __construct( |
124
|
|
|
Cache $doctrineCache = null, |
125
|
|
|
SerializerFactoryInterface $serializerFactory = null, |
126
|
|
|
MetadataFactoryFactoryInterface $metadaFactoryFactory = null, |
127
|
|
|
ClientFactory $clientFactory = null |
128
|
|
|
) { |
129
|
9 |
|
$this->doctrineCache = $doctrineCache; |
130
|
9 |
|
$this->serializerFactory = $serializerFactory ?? new SerializerFactory(); |
|
|
|
|
131
|
9 |
|
$this->metadaFactoryFactory = $metadaFactoryFactory ?? new MetadataFactoryFactory(); |
132
|
9 |
|
$this->clientFactory = $clientFactory ?? new ClientFactory(); |
133
|
9 |
|
} |
134
|
|
|
|
135
|
8 |
|
private function createMetadataFactory(): MetadataFactoryInterface |
136
|
|
|
{ |
137
|
8 |
|
switch ($this->metadataDriver) { |
138
|
8 |
|
case 'annotation': |
139
|
6 |
|
return $this->metadaFactoryFactory->createAnnotationMetadataFactory( |
140
|
6 |
|
$this->metadataClassName, |
141
|
6 |
|
$this->annotationReader ?? new AnnotationReader() |
|
|
|
|
142
|
|
|
); |
143
|
2 |
|
case 'yaml': |
144
|
2 |
|
return $this->metadaFactoryFactory |
145
|
2 |
|
->createYmlMetadataFactory($this->metadataPath, $this->metadataClassName); |
146
|
|
|
default: |
147
|
|
|
throw new RuntimeException('invalid driver provided'); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
2 |
|
private function createMetadataCache(): CacheInterface |
152
|
|
|
{ |
153
|
2 |
|
switch ($this->metadataCache) { |
154
|
2 |
|
case 'file': |
155
|
1 |
|
return new FileCache($this->cacheDir); |
156
|
1 |
|
case 'doctrine': |
157
|
1 |
|
return new DoctrineCacheAdapter('metadata', $this->doctrineCache); |
158
|
|
|
default: |
159
|
|
|
throw new RuntimeException('invalid metadata cache chosen'); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
9 |
|
private function getProxyFactory(): ProxyFactoryInterface |
164
|
|
|
{ |
165
|
9 |
|
if (isset($this->proxyFactory)) { |
166
|
|
|
return $this->proxyFactory; |
167
|
|
|
} |
168
|
|
|
|
169
|
9 |
|
$config = new Configuration(); |
170
|
9 |
|
$config->setProxiesTargetDir($this->cacheDir); |
171
|
9 |
|
$proxyFactory = new ProxyFactory($config); |
172
|
9 |
|
$proxyFactory->registerProxyAutoloader(); |
173
|
|
|
|
174
|
9 |
|
return $this->proxyFactory = $proxyFactory; |
175
|
|
|
} |
176
|
|
|
|
177
|
9 |
|
public function createTransferManager(): TransferManagerInterface |
178
|
|
|
{ |
179
|
9 |
|
$metadataFactory = $this->metadaFactory ?? $this->createMetadataFactory(); |
180
|
9 |
|
$serializer = $this->serializer ?? $this->serializerFactory->createSerialzer($metadataFactory); |
181
|
|
|
|
182
|
9 |
|
if (null !== $this->metadataCache) { |
183
|
2 |
|
$metadataFactory->setCache($this->createMetadataCache()); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
9 |
|
if (!isset($this->clientRegistry)) { |
187
|
|
|
throw new RuntimeException('no client registry added'); |
188
|
|
|
} |
189
|
|
|
|
190
|
9 |
|
$webServiceClient = $this->webserviceClient ?? $this->clientFactory |
191
|
9 |
|
->createWebserviceClient($this->clientRegistry, $metadataFactory, $serializer, $serializer); |
|
|
|
|
192
|
|
|
|
193
|
9 |
|
if ($this->isTransactional && !isset($this->eventDispatcher)) { |
194
|
1 |
|
$this->eventDispatcher = new EventDispatcher(); |
195
|
|
|
} |
196
|
|
|
|
197
|
9 |
|
$transferManager = new TransferManager( |
198
|
9 |
|
$metadataFactory, |
199
|
9 |
|
$webServiceClient, |
200
|
9 |
|
$this->getProxyFactory(), |
201
|
9 |
|
$this->eventDispatcher |
202
|
|
|
); |
203
|
|
|
|
204
|
9 |
|
if ($this->isTransactional) { |
205
|
1 |
|
$this->eventDispatcher->addListener( |
206
|
|
|
[ |
207
|
1 |
|
PersistenceEvents::POST_PERSIST, |
208
|
1 |
|
PersistenceEvents::PRE_UPDATE, |
209
|
1 |
|
PersistenceEvents::POST_REMOVE, |
210
|
1 |
|
PersistenceEvents::ON_EXCEPTION, |
211
|
|
|
], |
212
|
1 |
|
new TransactionEventListener(new Transaction($transferManager, $webServiceClient, $metadataFactory)) |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
9 |
|
return $transferManager; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function withProxyFactory(ProxyFactoryInterface $proxyFactory) |
220
|
|
|
{ |
221
|
|
|
$this->proxyFactory = $proxyFactory; |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
2 |
|
public function withMetadataCache(string $metadataCache) |
227
|
|
|
{ |
228
|
2 |
|
$this->metadataCache = $metadataCache; |
229
|
|
|
|
230
|
2 |
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
2 |
|
public function withCacheDir(string $cacheDir) |
234
|
|
|
{ |
235
|
2 |
|
$this->cacheDir = $cacheDir; |
236
|
|
|
|
237
|
2 |
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
4 |
|
public function withMetadataDriver(string $metadataDriver) |
241
|
|
|
{ |
242
|
4 |
|
$this->metadataDriver = $metadataDriver; |
243
|
|
|
|
244
|
4 |
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
9 |
|
public function withClientRegistry(ClientRegistryInterface $clientRegistry) |
248
|
|
|
{ |
249
|
9 |
|
$this->clientRegistry = $clientRegistry; |
250
|
|
|
|
251
|
9 |
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
2 |
|
public function withMetadataPath(string $metadataPath) |
255
|
|
|
{ |
256
|
2 |
|
$this->metadataPath = $metadataPath; |
257
|
|
|
|
258
|
2 |
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
2 |
|
public function withMetadataClassName(string $metadataClassName) |
262
|
|
|
{ |
263
|
2 |
|
$this->metadataClassName = $metadataClassName; |
264
|
|
|
|
265
|
2 |
|
return $this; |
266
|
|
|
} |
267
|
|
|
|
268
|
2 |
|
public function withDebug(bool $debug) |
269
|
|
|
{ |
270
|
2 |
|
$this->debug = $debug; |
271
|
|
|
|
272
|
2 |
|
return $this; |
273
|
|
|
} |
274
|
|
|
|
275
|
1 |
|
public function withSerializer(SerializerInterface $serializer) |
276
|
|
|
{ |
277
|
1 |
|
$this->serializer = $serializer; |
278
|
|
|
|
279
|
1 |
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
1 |
|
public function withMetadaFactory(MetadataFactoryInterface $metadaFactory) |
283
|
|
|
{ |
284
|
1 |
|
$this->metadaFactory = $metadaFactory; |
285
|
|
|
|
286
|
1 |
|
return $this; |
287
|
|
|
} |
288
|
|
|
|
289
|
2 |
|
public function withWebserviceClient(WebserviceClientInterface $webserviceClient) |
290
|
|
|
{ |
291
|
2 |
|
$this->webserviceClient = $webserviceClient; |
292
|
|
|
|
293
|
2 |
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
1 |
|
public function withEventDispatcher(EventDispatcherInterface $eventDispatcher) |
297
|
|
|
{ |
298
|
1 |
|
$this->eventDispatcher = $eventDispatcher; |
299
|
|
|
|
300
|
1 |
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function withClientFactory(ClientFactory $clientFactory) |
304
|
|
|
{ |
305
|
|
|
$this->clientFactory = $clientFactory; |
306
|
|
|
|
307
|
|
|
return $this; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
public function withDoctrineCache(Cache $doctrineCache) |
311
|
|
|
{ |
312
|
|
|
$this->doctrineCache = $doctrineCache; |
313
|
|
|
|
314
|
|
|
$this->withMetadataCache('doctrine'); |
315
|
|
|
|
316
|
|
|
return $this; |
317
|
|
|
} |
318
|
|
|
|
319
|
1 |
|
public function isTransactional(bool $transactional = true) |
320
|
|
|
{ |
321
|
1 |
|
$this->isTransactional = $transactional; |
322
|
|
|
|
323
|
1 |
|
return $this; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.