Completed
Push — master ( be488a...12d8ec )
by Dmitry
03:20
created

Event   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 43.4%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 100
ccs 23
cts 53
cp 0.434
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscription() 0 18 5
A fire() 0 7 1
A fireChangesPart() 0 7 2
A hasChanges() 0 11 3
B fireChanges() 0 48 9
1
<?php
2
3
namespace Basis;
4
5
use Basis\Dispatcher;
6
use Exception;
7
use ReflectionClass;
8
use Tarantool\Mapper\Plugin\Spy;
9
use Tarantool\Mapper\Pool;
10
11
class Event
12
{
13
    use Toolkit;
14
15 7
    public function getSubscription()
16
    {
17 7
        $subscription = [];
18 7
        foreach ($this->get(Filesystem::class)->listClasses('Listener') as $class) {
19 7
            $reflection = new ReflectionClass($class);
20 7
            if ($reflection->isAbstract()) {
21
                continue;
22
            }
23 7
            foreach ($reflection->getStaticPropertyValue('events') as $event) {
24 7
                if (!array_key_exists($event, $subscription)) {
25 7
                    $subscription[$event] = [];
26
                }
27 7
                $subscription[$event][] = substr($class, strlen('Listener\\'));
28
            }
29
        }
30
31 7
        return $subscription;
32
    }
33
34 1
    public function fire(string $event, $context)
35
    {
36 1
        $this->app->dispatch('event.fire', [
37 1
            'event'   => $this->get(Service::class)->getName().'.'.$event,
38 1
            'context' => $context,
39
        ]);
40 1
    }
41
42
    public function fireChangesPart(string $producer, int $fraction = 10)
43
    {
44
        if (++$this->counter % $fraction === 0) {
0 ignored issues
show
Bug introduced by
The property counter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
            return $this->fireChanges($producer);
46
        }
47
        return false;
48
    }
49
50
    public function hasChanges()
51
    {
52
        $hasChanges = false;
53
54
        foreach ($this->get(Pool::class)->getMappers() as $mapper) {
55
            if ($mapper->getPlugin(Spy::class)->hasChanges()) {
56
                $hasChanges = true;
57
            }
58
        }
59
        return $hasChanges;
60
    }
61
62 1
    public function fireChanges(string $producer)
63
    {
64 1
        $this->get(Pool::class)->get($this->get(Service::class)->getName());
65
66 1
        $dispatcher = $this->app->get(Dispatcher::class);
67 1
        $changed = false;
68
69 1
        foreach ($this->get(Pool::class)->getMappers() as $mapper) {
70 1
            $spy = $mapper->getPlugin(Spy::class);
71 1
            if ($spy->hasChanges()) {
72
                // reduce changes list
73
                $changes = $spy->getChanges();
74
                foreach ($changes as $action => $collection) {
75
                    foreach ($collection as $space => $entities) {
76
                        $event = $this->get(Service::class)->getName().'.'.$space.'.'.$action;
77
78
                        if (!$this->get(Service::class)->eventExists($event)) {
79
                            unset($collection[$space]);
80
                        }
81
                    }
82
                    if (!count($collection)) {
83
                        unset($changes->$action);
84
                    }
85
                }
86
87
                if (count(get_object_vars($changes))) {
88
                    $changed = true;
89
                    $data = $this->get(Converter::class)->toArray([
90
                        'changes'  => $changes,
91
                        'producer' => $producer,
92
                        'service'  => $mapper->serviceName,
93
                        'context' => $this->get(Context::class),
94
                    ]);
95
                    try {
96
                        // put changes to queue
97
                        $this->getQueue('event.changes')->put($data);
98
                    } catch (Exception $e) {
99
                        // use legacy https transport
100
                        // todo split data into chunks
101
                        $dispatcher->send('event.changes', $data);
102
                    }
103
                }
104
105
                $spy->reset();
106
            }
107
        }
108 1
        return $changed;
109
    }
110
}
111