Completed
Push — master ( 28afa9...69e2c3 )
by JHONATAN
02:50
created

UnityOfWork::isNew()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Vox\Webservice;
4
5
use AppendIterator;
6
use BadMethodCallException;
7
use Metadata\MetadataFactoryInterface;
8
use SplObjectStorage;
9
10
/**
11
 * the unit of work keeps track of the transfers current state, works as a sort of memento pattern
12
 * its really important part of the persistence proccess
13
 * 
14
 * @author Jhonatan Teixeira <[email protected]>
15
 */
16
class UnityOfWork implements UnityOfWorkInterface
17
{
18
    use MetadataTrait;
19
    
20
    /**
21
     * @var ObjectStorageInterface
22
     */
23
    private $cleanData;
24
    
25
    /**
26
     * @var ObjectStorageInterface
27
     */
28
    private $data;
29
    
30
    /**
31
     * @var SplObjectStorage
32
     */
33
    private $newObjects;
34
    
35
    /**
36
     * @var SplObjectStorage
37
     */
38
    private $removedObjects;
39
    
40 20
    public function __construct(MetadataFactoryInterface $metadataFactory)
41
    {
42 20
        $this->metadataFactory = $metadataFactory;
43 20
        $this->cleanData       = new ObjectStorage($metadataFactory);
44 20
        $this->data            = new ObjectStorage($metadataFactory);
45 20
        $this->newObjects      = new SplObjectStorage();
46 20
        $this->removedObjects  = new SplObjectStorage();
47 20
    }
48
    
49 9
    public function contains($object): bool
50
    {
51 9
        return $this->data->contains($object)
52 9
            || $this->newObjects->contains($object);
53
    }
54
    
55 11
    public function attach($object)
56
    {
57 11
        $id = $this->getIdValue($object);
58
        
59 11
        if (!$id) {
60 3
            $this->newObjects->attach($object);
61
            
62 3
            return;
63
        }
64
        
65 11
        if (!$this->cleanData->contains($object)) {
66 11
            $this->cleanData->attach(clone $object);
67
        }
68
        
69 11
        if (!$this->data->contains($object)) {
70 11
            $this->data->attach($object);
71
        }
72 11
    }
73
    
74 9
    public function detach($object)
75
    {
76 9
        if ($this->cleanData->contains($object)) {
77 7
            $this->cleanData->detach($object);
78
        }
79
        
80 9
        if ($this->data->contains($object)) {
81 7
            $this->data->detach($object);
82
        }
83
        
84 9
        if ($this->newObjects->contains($object)) {
85 3
            $this->newObjects->detach($object);
86
        }
87 9
    }
88
    
89 1
    public function remove($object)
90
    {
91 1
        if (!$this->contains($object)) {
92
            throw new \RuntimeException('object ' . get_class($object) . ' is not managed');
93
        }
94
        
95 1
        if (!$this->removedObjects->contains($object)) {
96 1
            $this->removedObjects->attach($object);
97
        }
98
        
99 1
        $this->detach($object);
100 1
    }
101
    
102 9
    public function isNew($object): bool
103
    {
104 9
        return empty($this->getIdValue($object)) 
105 3
            && ($this->newObjects->contains($object)
106 9
                || !$this->cleanData->contains($object));
107
    }
108
    
109 7
    public function isDirty($object): bool
110
    {
111 7
        return $this->data->contains($object)
112 7
            && !$this->cleanData->isEquals($object);
113
    }
114
    
115
    public function isDetached($object): bool
116
    {
117
        return !empty($this->getIdValue($object))
118
            && !$this->cleanData->contains($object);
119
    }
120
121 9
    public function getIterator()
122
    {
123 9
        $iterator = new AppendIterator();
124 9
        $iterator->append($this->newObjects);
125 9
        $iterator->append($this->data->getIterator());
126 9
        $iterator->append($this->removedObjects);
127
        
128 9
        return $iterator;
129
    }
130
131
    public function isEquals($object): bool
132
    {
133
        throw new BadMethodCallException('not implemented');
134
    }
135
136 1
    public function isRemoved($object): bool
137
    {
138 1
        return $this->removedObjects->contains($object);
139
    }
140
141
    /**
142
     * @param string $className
143
     * @param scalar $id
144
     *
145
     * @return object
146
     */
147 5
    public function fetchByParams(...$params)
148
    {
149 5
        return $this->data->fetchByParams(...$params);
150
    }
151
}
152