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

Event::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 2
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