Completed
Push — master ( 86cfb6...ebc154 )
by Dmitry
03:36
created

Event   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 48.48%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 13
c 6
b 1
f 1
lcom 2
cbo 4
dl 0
loc 63
ccs 16
cts 33
cp 0.4848
rs 10

3 Methods

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