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

Event   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 8
c 6
b 1
f 1
lcom 1
cbo 3
dl 0
loc 42
ccs 0
cts 30
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C fireChanges() 0 28 7
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