Completed
Push — master ( d14d6a...42d5bc )
by Dmitry
01:43
created

Spy::afterUpdate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Tarantool\Mapper\Plugin;
6
7
use Tarantool\Mapper\Entity;
8
use Tarantool\Mapper\Plugin;
9
use Tarantool\Mapper\Space;
10
11
class Spy extends Plugin
12
{
13
    private $created = [];
14
    private $updated = [];
15
    private $removed = [];
16
17
    public function afterCreate(Entity $instance, Space $space) : Entity
18
    {
19
        return $this->created[$this->getKey($instance, $space)] = $instance;
20
    }
21
22
    public function afterUpdate(Entity $instance, Space $space) : Entity
23
    {
24
        $key = $this->getKey($instance, $space);
25
        if (!array_key_exists($key, $this->created)) {
26
            $this->updated[$key] = $instance;
27
        }
28
29
        return $instance;
30
    }
31
32
    public function beforeRemove(Entity $instance, Space $space) : Entity
33
    {
34
        $key = $this->getKey($instance, $space);
35
36
        if (array_key_exists($key, $this->created)) {
37
            unset($this->created[$key]);
38
            return $instance;
39
        }
40
41
        if (array_key_exists($key, $this->updated)) {
42
            unset($this->updated[$key]);
43
        }
44
45
        return $this->removed[$key] = $instance;
46
    }
47
48
    public function reset() : self
49
    {
50
        $this->created = [];
51
        $this->updated = [];
52
        $this->removed = [];
53
        return $this;
54
    }
55
56
    private function getKey(Entity $instance, Space $space) : string
57
    {
58
        $key = [$space->getName()];
59
60
        $format = $space->getFormat();
61 View Code Duplication
        foreach ($space->getPrimaryIndex()['parts'] as $part) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            $field = array_key_exists(0, $part) ? $part[0] : $part['field'];
63
            $key[] = $instance->{$format[$field]['name']};
64
        }
65
66
        return implode(':', $key);
67
    }
68
69
    public function getChanges()
70
    {
71
        $result = (object) [];
72
73
        foreach (['created', 'updated', 'removed'] as $action) {
74
            $data = [];
75
            foreach ($this->$action as $key => $row) {
76
                list($space) = explode(':', $key);
77
                if (!array_key_exists($space, $data)) {
78
                    $data[$space] = [];
79
                }
80
                $data[$space][] = $row;
81
            }
82
            $result->$action = $data;
83
        }
84
85
        return $result;
86
    }
87
88
    public function hasChanges() : bool
89
    {
90
        return count($this->created) + count($this->updated) + count($this->removed) > 0;
91
    }
92
}
93