Completed
Push — master ( 1340e5...a9d1b8 )
by
unknown
02:07
created

UnitOfWork::getDirtyFields()   C

Complexity

Conditions 12
Paths 16

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 21
nc 16
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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