Completed
Push — master ( b663ee...50d896 )
by Dmitry
06:21
created

Event   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
lcom 2
cbo 4
dl 0
loc 75
ccs 0
cts 41
cp 0
rs 10
c 3
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fire() 0 7 1
C fireChanges() 0 32 8
A __construct() 0 7 1
B getSubscription() 0 18 5
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
    public function __construct(Application $app, Service $service, Pool $pool, Filesystem $filesystem)
17
    {
18
        $this->app = $app;
19
        $this->filesystem = $filesystem;
20
        $this->service = $service;
21
        $this->pool = $pool;
22
    }
23
24
    public function getSubscription()
25
    {
26
        $subscription = [];
27
        foreach ($this->filesystem->listClasses('Listener') as $class) {
28
            $reflection = new ReflectionClass($class);
29
            if ($reflection->isAbstract()) {
30
                continue;
31
            }
32
            foreach ($reflection->getStaticPropertyValue('events') as $event) {
33
                if (!array_key_exists($event, $subscription)) {
34
                    $subscription[$event] = [];
35
                }
36
                $subscription[$event][] = substr($class, strlen('Listener\\'));
37
            }
38
        }
39
40
        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
        foreach ($this->pool->getMappers() as $mapper) {
54
            $spy = $mapper->getPlugin(Spy::class);
55
            if ($spy->hasChanges()) {
56
                // reduce changes list
57
                $changes = $spy->getChanges();
58
                foreach ($changes as $action => $collection) {
59
                    foreach ($collection as $space => $entities) {
60
                        $event = $this->service->getName().'.'.$space.'.'.$action;
61
62
                        if (!$this->service->eventExists($event)) {
63
                            unset($collection[$space]);
64
                        }
65
                    }
66
                    if (!count($collection)) {
67
                        unset($changes->$action);
68
                    }
69
                }
70
71
                if (count(get_object_vars($changes))) {
72
                    $this->app->dispatch('event.changes', [
73
                        'producer' => $producer,
74
                        'changes' => $changes,
75
                        'service' => $mapper->serviceName,
76
                    ]);
77
                }
78
79
                $spy->reset();
80
            }
81
        }
82
    }
83
}
84