Completed
Push — master ( fc9daa...0ac164 )
by Dmitry
01:50
created

Compute::afterUpdate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 2
1
<?php
2
3
namespace Tarantool\Mapper\Plugin;
4
5
use Closure;
6
use Exception;
7
use Tarantool\Mapper\Plugin;
8
use Tarantool\Mapper\Space;
9
use Tarantool\Mapper\Entity;
10
11
class Compute extends Plugin
12
{
13
    protected $dependency = [];
14
    protected $entities = [];
15
16
    public function afterCreate(Entity $entity, Space $space)
17
    {
18
        $name = $space->getName();
19
20
        if (array_key_exists($name, $this->dependency)) {
21
            foreach ($this->dependency[$name] as [$target, $callback]) {
22
                $this->initializePresenter($target, $callback, $entity);
0 ignored issues
show
Bug introduced by
The variable $target does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $callback does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
23
            }
24
        }
25
    }
26
27
    public function afterRemove(Entity $entity, Space $space)
28
    {
29
        $name = $space->getName();
30
31
        if (array_key_exists($name, $this->dependency)) {
32
            foreach ($this->dependency[$name] as [$target, $callback]) {
33
                $this->getMapper()->remove($target, ['id' => $entity->id]);
0 ignored issues
show
Bug introduced by
The variable $target does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
34
            }
35
        }
36
    }
37
38
    public function afterUpdate(Entity $entity, Space $space)
39
    {
40
        $name = $space->getName();
41
42
        if (array_key_exists($name, $this->dependency)) {
43
            foreach ($this->dependency[$name] as [$target, $callback]) {
44
                $child = $this->getMapper()->findOne($target, $entity->id);
0 ignored issues
show
Bug introduced by
The variable $target does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
45
                foreach ($callback($entity) as $k => $v) {
0 ignored issues
show
Bug introduced by
The variable $callback does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
46
                    $child->$k = $v;
47
                }
48
                $child->save();
49
            }
50
        }
51
    }
52
53
    public function beforeCreate(Entity $entity, Space $space)
54
    {
55
        if (in_array($entity, $this->entities)) {
56
            return true;
57
        }
58
        foreach ($this->dependency as $source => $dependencies) {
59
            foreach ($dependencies as [$target]) {
60
                if ($target == $space->getName()) {
0 ignored issues
show
Bug introduced by
The variable $target does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
61
                    throw new Exception("Space $target is computed from $source");
62
                }
63
            }
64
        }
65
    }
66
67
    public function register($source, $target, $callback)
68
    {
69
        if ($callback instanceof Closure) {
70
            if (!array_key_exists($source, $this->dependency)) {
71
                $this->dependency[$source] = [];
72
            }
73
            $this->dependency[$source][] = [$target, $callback];
74
            foreach ($this->getMapper()->find($source) as $entity) {
75
                $this->initializePresenter($target, $callback, $entity);
76
            }
77
        } else {
78
            $this->registerLuaTriggers($source, $target, $callback);
79
        }
80
    }
81
82
    protected function initializePresenter($target, $callback, Entity $source)
83
    {
84
        $entity = $this->getMapper()->getRepository($target)->create($source->id);
85
        foreach ($callback($source) as $k => $v) {
86
            $entity->$k = $v;
87
        }
88
        $this->entities[] = $entity;
89
        $entity->save();
90
    }
91
92
    protected function registerLuaTriggers($source, $destination, $body)
0 ignored issues
show
Unused Code introduced by
The parameter $source is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $destination is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $body is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        throw new Exception("Lua triggers not implemented");
95
    }
96
}
97