Completed
Push — master ( fefc41...ecdd2c )
by Dmitry
31:36
created

Event::fire()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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