Completed
Push — master ( d4f643...31fba3 )
by JHONATAN
02:46
created

UnityOfWork::isDetached()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 6
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
    /**
36
     * @var WebserviceClientInterface
37
     */
38
    private $webserviceClient;
39
    
40 6
    public function __construct(WebserviceClientInterface $webserviceClient, MetadataFactoryInterface $metadataFactory)
41
    {
42 6
        $this->webserviceClient = $webserviceClient;
43 6
        $this->metadataFactory  = $metadataFactory;
44
        
45 6
        $this->cleanData      = new ObjectStorage($metadataFactory);
46 6
        $this->data           = new ObjectStorage($metadataFactory);
47 6
        $this->newObjects     = new SplObjectStorage();
48 6
        $this->removedObjects = new SplObjectStorage();
49 6
    }
50
    
51 5
    public function contains($object): bool
52
    {
53 5
        return $this->data->contains($object)
54 5
            || $this->newObjects->contains($object);
55
    }
56
    
57 6
    public function attach($object)
58
    {
59 6
        $id = $this->getIdValue($object);
60
        
61 6
        if (!$id) {
62 1
            $this->newObjects->attach($object);
63
            
64 1
            return;
65
        }
66
        
67 5
        if (!$this->cleanData->contains($object)) {
68 5
            $this->cleanData->attach(clone $object);
69
        }
70
        
71 5
        if (!$this->data->contains($object)) {
72 5
            $this->data->attach($object);
73
        }
74 5
    }
75
    
76 1
    public function detach($object)
77
    {
78 1
        if ($this->data->contains($object)) {
79 1
            $this->data->detach($object);
80
        }
81
        
82 1
        if ($this->data->contains($object)) {
83
            $this->data->detach($object);
84
        }
85
        
86 1
        if (!$this->newObjects->contains($object) && !$this->removedObjects->contains($object)) {
87 1
            $this->removedObjects->attach($object);
88
        }
89
        
90 1
        if ($this->newObjects->contains($object)) {
91
            $this->newObjects->detach($object);
92
        }
93 1
    }
94
    
95 5
    public function flush()
96
    {
97 5
        foreach ($this->newObjects as $newTransfer) {
98 1
            $this->webserviceClient->post($newTransfer);
99
        }
100
        
101 5
        foreach ($this->data as $transfer) {
102 3
            if ($this->cleanData->isEquals($transfer)) {
103
                continue;
104
            }
105
            
106 3
            $this->webserviceClient->put($transfer);
107
        }
108
        
109 5
        foreach ($this->removedObjects as $removedObject) {
110 1
            $this->webserviceClient->delete(get_class($removedObject), $this->getIdValue($removedObject));
111
        }
112 5
    }
113
    
114
    public function isNew($object): bool
115
    {
116
        return empty($this->getIdValue($object)) 
117
            && ($this->newObjects->contains($object)
118
                || !$this->cleanData->contains($object));
119
    }
120
    
121
    public function isDirty($object): bool
122
    {
123
        return $this->data->contains($object)
124
            && !$this->cleanData->isEquals($object);
125
    }
126
    
127
    public function isDetached($object): bool
128
    {
129
        return !empty($this->getIdValue($object))
130
            && !$this->cleanData->contains($object);
131
    }
132
133
    public function getIterator()
134
    {
135
        $iterator = new AppendIterator();
136
        $iterator->append($this->newObjects);
137
        $iterator->append($this->data);
0 ignored issues
show
Bug introduced by
$this->data of type Vox\Webservice\ObjectStorageInterface is incompatible with the type Iterator expected by parameter $iterator of AppendIterator::append(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
        $iterator->append(/** @scrutinizer ignore-type */ $this->data);
Loading history...
138
        
139
        return $iterator;
140
    }
141
142
    public function isEquals($object): bool
143
    {
144
        throw new BadMethodCallException('not implemented');
145
    }
146
}
147