Completed
Pull Request — master (#58)
by
unknown
02:03
created

UnitOfWork::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Mapado\RestClientSdk;
4
5
use Mapado\RestClientSdk\Mapping\ClassMetadata;
6
use Mapado\RestClientSdk\Mapping;
7
8
/**
9
 * UnitOfWork
10
 */
11
class UnitOfWork
12
{
13
    /**
14
     * mapping
15
     *
16
     * @var Mapping
17
     * @access private
18
     */
19
    private $mapping;
20
21
    /**
22
     * storage for every entity retrieved
23
     *
24
     * @var array
25
     */
26
    private $storage;
27
28
    /**
29
     * Constructor.
30
     */
31
    public function __construct(Mapping $mapping)
32
    {
33
        $this->mapping = $mapping;
34
        $this->storage = [];
35
    }
36
37
    /**
38
     * getDirtyData
39
     *
40
     * return the new serialized model with only needed fields to update
41
     *
42
     * @param array $newSerializedModel
43
     * @param array $oldSerializedModel
44
     * @param ClassMetadata $classMetadata
45
     * @access public
46
     * @return array
47
     */
48
    public function getDirtyData(array $newSerializedModel, array $oldSerializedModel, ClassMetadata $classMetadata)
49
    {
50
        return $this->getDirtyFields($newSerializedModel, $oldSerializedModel, $classMetadata);
51
    }
52
53
    /**
54
     * registerClean
55
     *
56
     * @param string $id
57
     * @param object $entity
58
     * @access public
59
     * @return UnitOfWork
60
     */
61
    public function registerClean($id, $entity)
62
    {
63
        if (is_object($entity)) {
64
            $entityStored = clone $entity;
65
            $this->storage[$id] = $entityStored;
66
        }
67
68
        return $this;
69
    }
70
71
    /**
72
     * getDirtyEntity
73
     *
74
     * @param string $id
75
     * @access public
76
     * @return mixed
77
     */
78
    public function getDirtyEntity($id)
79
    {
80
        if (isset($this->storage[$id])) {
81
            return $this->storage[$id];
82
        }
83
        return null;
84
    }
85
86
    /**
87
     * clear
88
     *
89
     * @param string $id
90
     * @access public
91
     * @return UnitOfWork
92
     */
93
    public function clear($id)
94
    {
95
        unset($this->storage[$id]);
96
        return $this;
97
    }
98
99
    /**
100
     * getDirtyFields
101
     *
102
     * compares serialize object and returns only modified fields
103
     *
104
     * @param array $newArrayModel
0 ignored issues
show
Bug introduced by
There is no parameter named $newArrayModel. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
105
     * @param array $oldSerializedModel
106
     * @param ClassMetadata $classMetadata
107
     * @access private
108
     * @return array
109
     */
110
    private function getDirtyFields(array $newSerializedModel, array $oldSerializedModel, ClassMetadata $classMetadata = null)
111
    {
112
        $dirtyFields = [];
113
114
        foreach ($newSerializedModel as $key => $value) {
115
            if (array_key_exists($key, $oldSerializedModel)) {
116
                if (is_array($value)) {
117
                    $currentClassMetadata = $classMetadata->getRelation($key) ? $this->mapping->getClassMetadata($classMetadata->getRelation($key)->getTargetEntity()) : null;
0 ignored issues
show
Bug introduced by
It seems like $classMetadata is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
118
                    $idSerializedKey = $currentClassMetadata ? $currentClassMetadata->getIdSerializeKey() : null;
119
                    $recursiveDiff = $this->getDirtyFields($value, $oldSerializedModel[$key], $currentClassMetadata);
120
                    if (count($recursiveDiff)) {
121
                        $dirtyFields[$key] = $recursiveDiff;
122
                        $dirtyFields[$key] = $this->addIdentifiers($value, $dirtyFields[$key], $idSerializedKey);
123
124
                        // if theres only ids not objects, keep them
125
                        foreach ($value as $valueKey => $valueId) {
126
                            if (is_string($valueId) && is_int($valueKey)) {
127
                                $dirtyFields[$key][$valueKey] = $valueId;
128
                            }
129
                        }
130
                    } elseif (count($value) != count($oldSerializedModel[$key])) {
131
                        // get all objects ids of new array
132
                        $dirtyFields[$key] = [];
133
                        $dirtyFields[$key] = $this->addIdentifiers($value, $dirtyFields[$key], $idSerializedKey);
134
                    }
135
                } else {
136
                    if ($value != $oldSerializedModel[$key]) {
137
                        $dirtyFields[$key] = $value;
138
                    }
139
                }
140
            } else {
141
                $dirtyFields[$key] = $value;
142
            }
143
        }
144
145
        return $dirtyFields;
146
    }
147
148
    /**
149
     * addIdentifiers
150
     *
151
     * add defined identifiers to given model
152
     *
153
     * @param array $newSerializedModel
154
     * @param array $dirtyFields
155
     * @access private
156
     * @return array
157
     */
158
    private function addIdentifiers($newSerializedModel, $dirtyFields, $idSerializedKey = null)
159
    {
160
        foreach ($newSerializedModel as $key => $value) {
161
            if ($idSerializedKey && isset($value[$idSerializedKey])) {
162
                $dirtyFields[$key][$idSerializedKey] = $value[$idSerializedKey];
163
            } elseif (is_string($value) && is_int($key)) {
164
                $dirtyFields[$key] = $value;
165
            }
166
        }
167
168
        return $dirtyFields;
169
    }
170
}
171