Event   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 49.09%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 2
dl 0
loc 103
ccs 27
cts 55
cp 0.4909
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 14 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 4
    public function getSubscription()
16
    {
17 4
        $subscription = [];
18 4
        foreach ($this->get(Filesystem::class)->listClasses('Listener') as $class) {
19 4
            $reflection = new ReflectionClass($class);
20 4
            if ($reflection->isAbstract()) {
21
                continue;
22
            }
23 4
            foreach ($reflection->getStaticPropertyValue('events') as $event) {
24 4
                if (!array_key_exists($event, $subscription)) {
25 4
                    $subscription[$event] = [];
26
                }
27 4
                $subscription[$event][] = substr($class, strlen('Listener\\'));
28
            }
29
        }
30
31 4
        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
        $serviceName = $this->get(Service::class)->getName();
55
        $this->get(Pool::class)->get($serviceName);
56
57
        foreach ($this->get(Pool::class)->getMappers() as $mapper) {
58
            if ($mapper->getPlugin(Spy::class)->hasChanges()) {
59
                $hasChanges = true;
60
            }
61
        }
62
        return $hasChanges;
63
    }
64
65 1
    public function fireChanges(string $producer)
66
    {
67 1
        $this->get(Pool::class)->get($this->get(Service::class)->getName());
68
69 1
        $dispatcher = $this->app->get(Dispatcher::class);
70 1
        $changed = false;
71
72 1
        foreach ($this->get(Pool::class)->getMappers() as $mapper) {
73 1
            $spy = $mapper->getPlugin(Spy::class);
74 1
            if ($spy->hasChanges()) {
75
                // reduce changes list
76 1
                $changes = $spy->getChanges();
77 1
                foreach ($changes as $action => $collection) {
78 1
                    foreach ($collection as $space => $entities) {
79 1
                        $event = $this->get(Service::class)->getName().'.'.$space.'.'.$action;
80
81 1
                        if (!$this->get(Service::class)->eventExists($event)) {
82
                            unset($collection[$space]);
83
                        }
84
                    }
85
                    if (!count($collection)) {
86
                        unset($changes->$action);
87
                    }
88
                }
89
90
                if (count(get_object_vars($changes))) {
91
                    $changed = true;
92
                    $data = $this->get(Converter::class)->toArray([
93
                        'changes'  => $changes,
94
                        'producer' => $producer,
95
                        'service'  => $mapper->serviceName,
96
                        'context' => $this->get(Context::class),
97
                    ]);
98
                    try {
99
                        // put changes to queue
100
                        $this->getQueue('event.changes')->put($data);
101
                    } catch (Exception $e) {
102
                        // use legacy https transport
103
                        // todo split data into chunks
104
                        $dispatcher->send('event.changes', $data);
105
                    }
106
                }
107
108
                $spy->reset();
109
            }
110
        }
111
        return $changed;
112
    }
113
}
114