Completed
Push — master ( 2851ee...e6d5b1 )
by Dmitry
04:10
created

Event::fireChanges()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 28
ccs 0
cts 24
cp 0
rs 6.7272
cc 7
eloc 15
nc 15
nop 0
crap 56
1
<?php
2
3
namespace Basis;
4
5
use Tarantool\Mapper\Plugins\Spy;
6
7
class Event
8
{
9
    private $dispatcher;
10
    private $service;
11
    private $spy;
12
13
    public function __construct(Dispatcher $dispatcher, Service $service, Spy $spy)
14
    {
15
        $this->dispatcher = $dispatcher;
16
        $this->service = $service;
17
        $this->spy = $spy;
18
    }
19
20
    public function fireChanges()
21
    {
22
        if ($this->spy->hasChanges()) {
23
            // reduce changes list
24
            $changes = $this->spy->getChanges();
25
            foreach ($changes as $action => $collection) {
26
                foreach ($collection as $space => $entities) {
27
                    $event = $this->service->getName().'.'.$space.'.'.$action;
28
29
                    if (!$this->service->eventExists($event)) {
30
                        unset($collection[$space]);
31
                    }
32
                }
33
                if (!count($collection)) {
34
                    unset($changes->$action);
35
                }
36
            }
37
38
            if (count($changes)) {
39
                $this->dispatcher->dispatch('event.changes', [
40
                    'changes' => $changes,
41
                    'service' => $this->service->getName(),
42
                ]);
43
            }
44
45
            $this->spy->reset();
46
        }
47
    }
48
}
49