Completed
Push — master ( f0a08a...a3469c )
by Dmitry
05:34
created

Event::fire()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
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 7
    public function __construct(Application $app, Service $service, Pool $pool, Filesystem $filesystem)
17
    {
18 7
        $this->app = $app;
19 7
        $this->filesystem = $filesystem;
20 7
        $this->service = $service;
21 7
        $this->pool = $pool;
22 7
    }
23
24 6
    public function getSubscription()
25
    {
26 6
        $subscription = [];
27 6
        foreach ($this->filesystem->listClasses('Listener') as $class) {
28 6
            $reflection = new ReflectionClass($class);
29 6
            if ($reflection->isAbstract()) {
30
                continue;
31
            }
32 6
            foreach ($reflection->getStaticPropertyValue('events') as $event) {
33 6
                if (!array_key_exists($event, $subscription)) {
34 6
                    $subscription[$event] = [];
35
                }
36 6
                $subscription[$event][] = substr($class, strlen('Listener\\'));
37
            }
38
        }
39
40 6
        return $subscription;
41
    }
42
43 1
    public function fire(string $event, $context)
44
    {
45 1
        $this->app->dispatch('event.fire', [
46 1
            'event' => $this->service->getName().'.'.$event,
47 1
            'context' => $context,
48
        ]);
49 1
    }
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