1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vox\Webservice; |
4
|
|
|
|
5
|
|
|
use AppendIterator; |
6
|
|
|
use BadMethodCallException; |
7
|
|
|
use Metadata\MetadataFactoryInterface; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use SplObjectStorage; |
10
|
|
|
|
11
|
|
|
class UnityOfWork implements UnityOfWorkInterface |
12
|
|
|
{ |
13
|
|
|
use MetadataTrait; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ObjectStorageInterface |
17
|
|
|
*/ |
18
|
|
|
private $cleanData; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ObjectStorageInterface |
22
|
|
|
*/ |
23
|
|
|
private $data; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var SplObjectStorage |
27
|
|
|
*/ |
28
|
|
|
private $newObjects; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var SplObjectStorage |
32
|
|
|
*/ |
33
|
|
|
private $removedObjects; |
34
|
|
|
|
35
|
13 |
|
public function __construct(MetadataFactoryInterface $metadataFactory) |
36
|
|
|
{ |
37
|
13 |
|
$this->metadataFactory = $metadataFactory; |
38
|
13 |
|
$this->cleanData = new ObjectStorage($metadataFactory); |
39
|
13 |
|
$this->data = new ObjectStorage($metadataFactory); |
40
|
13 |
|
$this->newObjects = new SplObjectStorage(); |
41
|
13 |
|
$this->removedObjects = new SplObjectStorage(); |
42
|
13 |
|
} |
43
|
|
|
|
44
|
7 |
|
public function contains($object): bool |
45
|
|
|
{ |
46
|
7 |
|
return $this->data->contains($object) |
47
|
7 |
|
|| $this->newObjects->contains($object); |
48
|
|
|
} |
49
|
|
|
|
50
|
8 |
|
public function attach($object) |
51
|
|
|
{ |
52
|
8 |
|
$id = $this->getIdValue($object); |
53
|
|
|
|
54
|
8 |
|
if (!$id) { |
55
|
2 |
|
$this->newObjects->attach($object); |
56
|
|
|
|
57
|
2 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
8 |
|
if (!$this->cleanData->contains($object)) { |
61
|
8 |
|
$this->cleanData->attach(clone $object); |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
if (!$this->data->contains($object)) { |
65
|
8 |
|
$this->data->attach($object); |
66
|
|
|
} |
67
|
8 |
|
} |
68
|
|
|
|
69
|
6 |
|
public function detach($object) |
70
|
|
|
{ |
71
|
6 |
|
if ($this->cleanData->contains($object)) { |
72
|
5 |
|
$this->cleanData->detach($object); |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
if ($this->data->contains($object)) { |
76
|
5 |
|
$this->data->detach($object); |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
if ($this->newObjects->contains($object)) { |
80
|
2 |
|
$this->newObjects->detach($object); |
81
|
|
|
} |
82
|
6 |
|
} |
83
|
|
|
|
84
|
1 |
|
public function remove($object) |
85
|
|
|
{ |
86
|
1 |
|
if (!$this->contains($object)) { |
87
|
|
|
throw new \RuntimeException('object ' . get_class($object) . 'is not managed'); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
if (!$this->removedObjects->contains($object)) { |
91
|
1 |
|
$this->removedObjects->attach($object); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
$this->detach($object); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
6 |
|
public function isNew($object): bool |
98
|
|
|
{ |
99
|
6 |
|
return empty($this->getIdValue($object)) |
100
|
2 |
|
&& ($this->newObjects->contains($object) |
101
|
6 |
|
|| !$this->cleanData->contains($object)); |
102
|
|
|
} |
103
|
|
|
|
104
|
5 |
|
public function isDirty($object): bool |
105
|
|
|
{ |
106
|
5 |
|
return $this->data->contains($object) |
107
|
5 |
|
&& !$this->cleanData->isEquals($object); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function isDetached($object): bool |
111
|
|
|
{ |
112
|
|
|
return !empty($this->getIdValue($object)) |
113
|
|
|
&& !$this->cleanData->contains($object); |
114
|
|
|
} |
115
|
|
|
|
116
|
6 |
|
public function getIterator() |
117
|
|
|
{ |
118
|
6 |
|
$iterator = new AppendIterator(); |
119
|
6 |
|
$iterator->append($this->newObjects); |
120
|
6 |
|
$iterator->append($this->data->getIterator()); |
121
|
6 |
|
$iterator->append($this->removedObjects); |
122
|
|
|
|
123
|
6 |
|
return $iterator; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function isEquals($object): bool |
127
|
|
|
{ |
128
|
|
|
throw new BadMethodCallException('not implemented'); |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
public function isRemoved($object): bool |
132
|
|
|
{ |
133
|
1 |
|
return $this->removedObjects->contains($object); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|