1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vox\Webservice; |
4
|
|
|
|
5
|
|
|
use Metadata\MetadataFactoryInterface; |
6
|
|
|
use ProxyManager\Proxy\AccessInterceptorValueHolderInterface; |
7
|
|
|
use Vox\Webservice\Mapping\BelongsTo; |
8
|
|
|
use Vox\Webservice\Metadata\TransferMetadata; |
9
|
|
|
|
10
|
|
|
class TransferPersister implements TransferPersisterInterface |
11
|
|
|
{ |
12
|
|
|
use MetadataTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var MetadataFactoryInterface |
16
|
|
|
*/ |
17
|
|
|
private $metadataFactory; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var UnityOfWorkInterface |
21
|
|
|
*/ |
22
|
|
|
private $unityOfWork; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var WebserviceClientInterface |
26
|
|
|
*/ |
27
|
|
|
private $webserviceClient; |
28
|
|
|
|
29
|
13 |
|
public function __construct( |
30
|
|
|
MetadataFactoryInterface $metadataFactory, |
31
|
|
|
UnityOfWorkInterface $unityOfWork, |
32
|
|
|
WebserviceClientInterface $webserviceClient |
33
|
|
|
) { |
34
|
13 |
|
$this->metadataFactory = $metadataFactory; |
35
|
13 |
|
$this->unityOfWork = $unityOfWork; |
36
|
13 |
|
$this->webserviceClient = $webserviceClient; |
37
|
13 |
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
6 |
|
public function save($object) |
41
|
|
|
{ |
42
|
6 |
|
$transfer = $object; |
43
|
|
|
|
44
|
6 |
|
if ($object instanceof AccessInterceptorValueHolderInterface) { |
45
|
5 |
|
$transfer = $transfer->getWrappedValueHolderValue(); |
46
|
|
|
} |
47
|
|
|
|
48
|
6 |
|
$metadata = $this->getClassMetadata($transfer); |
49
|
|
|
|
50
|
6 |
|
foreach ($metadata->associations as $name => $association) { |
51
|
1 |
|
$assocTransfer = $association->getValue($transfer); |
52
|
|
|
|
53
|
1 |
|
if (!$assocTransfer) { |
54
|
|
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$this->persistAssociation($object, $assocTransfer, $metadata, $association); |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
$this->persistTransfer($object); |
61
|
6 |
|
} |
62
|
|
|
|
63
|
1 |
|
private function persistAssociation($object, $association, TransferMetadata $metadata, \Vox\Metadata\PropertyMetadata $property) |
64
|
|
|
{ |
65
|
1 |
|
if (!$this->unityOfWork->isNew($association) && !$this->unityOfWork->isDirty($association)) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$this->save($association); |
70
|
|
|
|
71
|
|
|
/* @var $belongsTo BelongsTo */ |
72
|
1 |
|
$belongsTo = $property->getAnnotation(BelongsTo::class); |
73
|
1 |
|
$foreignProperty = $metadata->propertyMetadata[$belongsTo->foreignField]; |
74
|
1 |
|
$foreignId = $foreignProperty->getValue($object); |
75
|
1 |
|
$currentId = $this->getIdValue($association); |
76
|
|
|
|
77
|
1 |
|
if ($foreignId !== $currentId) { |
78
|
1 |
|
$foreignProperty->setValue($object, $currentId); |
79
|
|
|
} |
80
|
1 |
|
} |
81
|
|
|
|
82
|
6 |
|
private function persistTransfer($object) |
83
|
|
|
{ |
84
|
6 |
|
if ($this->unityOfWork->isNew($object)) { |
85
|
2 |
|
$this->webserviceClient->post($object); |
86
|
2 |
|
$this->renewState($object); |
87
|
|
|
|
88
|
2 |
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
if ($this->unityOfWork->isDirty($object)) { |
92
|
4 |
|
$this->webserviceClient->put($object); |
93
|
4 |
|
$this->renewState($object); |
94
|
|
|
|
95
|
4 |
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
if ($this->unityOfWork->isRemoved($object)) { |
99
|
1 |
|
$this->webserviceClient->delete(get_class($object), $this->getIdValue($object)); |
100
|
|
|
} |
101
|
1 |
|
} |
102
|
|
|
|
103
|
5 |
|
private function renewState($object) |
104
|
|
|
{ |
105
|
5 |
|
$this->unityOfWork->detach($object); |
106
|
5 |
|
$this->unityOfWork->attach($object); |
107
|
5 |
|
} |
108
|
|
|
} |
109
|
|
|
|