Completed
Push — master ( 6233d3...c3d0ca )
by Dmitry
03:06
created

Event   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 44.68%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 4
dl 0
loc 86
ccs 21
cts 47
cp 0.4468
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSubscription() 0 18 5
A fire() 0 7 1
A fireChangesPart() 0 6 2
B fireChanges() 0 36 8
1
<?php
2
3
namespace Basis;
4
5
use Basis\Dispatcher;
6
use ReflectionClass;
7
use Tarantool\Mapper\Plugin\Spy;
8
use Tarantool\Mapper\Pool;
9
10
class Event
11
{
12
    private $app;
13
    private $filesystem;
14
    private $service;
15
    private $pool;
16
17 7
    public function __construct(Application $app, Service $service, Pool $pool, Filesystem $filesystem)
18
    {
19 7
        $this->app = $app;
20 7
        $this->filesystem = $filesystem;
21 7
        $this->service = $service;
22 7
        $this->pool = $pool;
23 7
    }
24
25 6
    public function getSubscription()
26
    {
27 6
        $subscription = [];
28 6
        foreach ($this->filesystem->listClasses('Listener') as $class) {
29 6
            $reflection = new ReflectionClass($class);
30 6
            if ($reflection->isAbstract()) {
31
                continue;
32
            }
33 6
            foreach ($reflection->getStaticPropertyValue('events') as $event) {
34 6
                if (!array_key_exists($event, $subscription)) {
35 6
                    $subscription[$event] = [];
36
                }
37 6
                $subscription[$event][] = substr($class, strlen('Listener\\'));
38
            }
39
        }
40
41 6
        return $subscription;
42
    }
43
44 1
    public function fire(string $event, $context)
45
    {
46 1
        $this->app->dispatch('event.fire', [
47 1
            'event'   => $this->service->getName().'.'.$event,
48 1
            'context' => $context,
49
        ]);
50 1
    }
51
52
    public function fireChangesPart(string $producer, int $fraction = 10)
53
    {
54
        if (++$this->counter % $fraction === 0) {
0 ignored issues
show
Bug introduced by
The property counter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
            return $this->fireChanges($producer);
56
        }
57
    }
58
59
    public function fireChanges(string $producer)
60
    {
61
        $this->pool->get($this->service->getName());
62
63
        $dispatcher = $this->app->get(Dispatcher::class);
64
65
        foreach ($this->pool->getMappers() as $mapper) {
66
            $spy = $mapper->getPlugin(Spy::class);
67
            if ($spy->hasChanges()) {
68
                // reduce changes list
69
                $changes = $spy->getChanges();
70
                foreach ($changes as $action => $collection) {
71
                    foreach ($collection as $space => $entities) {
72
                        $event = $this->service->getName().'.'.$space.'.'.$action;
73
74
                        if (!$this->service->eventExists($event)) {
75
                            unset($collection[$space]);
76
                        }
77
                    }
78
                    if (!count($collection)) {
79
                        unset($changes->$action);
80
                    }
81
                }
82
83
                if (count(get_object_vars($changes))) {
84
                    $dispatcher->send('event.changes', [
85
                        'changes'  => $changes,
86
                        'producer' => $producer,
87
                        'service'  => $mapper->serviceName,
88
                    ]);
89
                }
90
91
                $spy->reset();
92
            }
93
        }
94
    }
95
}
96