Completed
Push — master ( 0287cb...35c8bf )
by Dmitry
04:37
created

Event::fireChanges()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 25
ccs 0
cts 21
cp 0
rs 8.439
cc 6
eloc 13
nc 8
nop 0
crap 42
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
28
                    $event = $this->service->getName().'.'.$space.'.'.$action;
29
30
                    if(!$this->service->eventExists($event)) {
31
                        unset($collection[$space]);
32
                    }
33
                }
34
                if(!count($collection)) {
35
                    unset($changes[$action]);
36
                }
37
            }
38
39
            $this->dispatcher->dispatch('event.changes', [
40
                'changes' => $spy->getChanges(),
0 ignored issues
show
Bug introduced by
The variable $spy does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
41
                'service' => $this->service,
42
            ]);
43
        }
44
    }
45
}
46