Completed
Push — master ( b698e6...b6882e )
by Dmitry
03:20
created

Event::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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