Completed
Push — master ( 547bbe...6c1ef8 )
by Dmitry
03:22
created

Event   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 38.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 2
cbo 4
dl 0
loc 77
ccs 16
cts 42
cp 0.381
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getSubscription() 0 18 5
A __construct() 0 7 1
A fire() 0 7 1
C fireChanges() 0 34 8
1
<?php
2
3
namespace Basis;
4
5
use Tarantool\Mapper\Plugin\Spy;
6
use Tarantool\Mapper\Pool;
7
use ReflectionClass;
8
9
class Event
10
{
11
    private $app;
12
    private $filesystem;
13
    private $service;
14
    private $pool;
15
16 3
    public function __construct(Application $app, Service $service, Pool $pool, Filesystem $filesystem)
17
    {
18 3
        $this->app = $app;
19 3
        $this->filesystem = $filesystem;
20 3
        $this->service = $service;
21 3
        $this->pool = $pool;
22 3
    }
23
24 3
    public function getSubscription()
25
    {
26 3
        $subscription = [];
27 3
        foreach ($this->filesystem->listClasses('Listener') as $class) {
28 3
            $reflection = new ReflectionClass($class);
29 3
            if ($reflection->isAbstract()) {
30
                continue;
31
            }
32 3
            foreach ($reflection->getStaticPropertyValue('events') as $event) {
33 3
                if (!array_key_exists($event, $subscription)) {
34 3
                    $subscription[$event] = [];
35
                }
36 3
                $subscription[$event][] = substr($class, strlen('Listener\\'));
37
            }
38
        }
39
40 3
        return $subscription;
41
    }
42
43
    public function fire(string $event, $context)
44
    {
45
        $this->app->dispatch('event.fire', [
46
            'event' => $this->service->getName().'.'.$event,
47
            'context' => $context,
48
        ]);
49
    }
50
51
    public function fireChanges(string $producer)
52
    {
53
        $this->pool->get($this->service->getName());
54
55
        foreach ($this->pool->getMappers() as $mapper) {
56
            $spy = $mapper->getPlugin(Spy::class);
57
            if ($spy->hasChanges()) {
58
                // reduce changes list
59
                $changes = $spy->getChanges();
60
                foreach ($changes as $action => $collection) {
61
                    foreach ($collection as $space => $entities) {
62
                        $event = $this->service->getName().'.'.$space.'.'.$action;
63
64
                        if (!$this->service->eventExists($event)) {
65
                            unset($collection[$space]);
66
                        }
67
                    }
68
                    if (!count($collection)) {
69
                        unset($changes->$action);
70
                    }
71
                }
72
73
                if (count(get_object_vars($changes))) {
74
                    $this->app->dispatch('event.changes', [
75
                        'producer' => $producer,
76
                        'changes' => $changes,
77
                        'service' => $mapper->serviceName,
78
                    ]);
79
                }
80
81
                $spy->reset();
82
            }
83
        }
84
    }
85
}
86