Completed
Push — master ( 778733...8dcd56 )
by Dmitry
03:19
created

Event   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 42.86%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 4
dl 0
loc 90
ccs 21
cts 49
cp 0.4286
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 7 2
B fireChanges() 0 39 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
        return false;
58
    }
59
60
    public function fireChanges(string $producer)
61
    {
62
        $this->pool->get($this->service->getName());
63
64
        $dispatcher = $this->app->get(Dispatcher::class);
65
        $changes = false;
66
67
        foreach ($this->pool->getMappers() as $mapper) {
68
            $spy = $mapper->getPlugin(Spy::class);
69
            if ($spy->hasChanges()) {
70
                // reduce changes list
71
                $changes = $spy->getChanges();
72
                foreach ($changes as $action => $collection) {
73
                    foreach ($collection as $space => $entities) {
74
                        $event = $this->service->getName().'.'.$space.'.'.$action;
75
76
                        if (!$this->service->eventExists($event)) {
77
                            unset($collection[$space]);
78
                        }
79
                    }
80
                    if (!count($collection)) {
81
                        unset($changes->$action);
82
                    }
83
                }
84
85
                if (count(get_object_vars($changes))) {
86
                    $changes = true;
87
                    $dispatcher->send('event.changes', [
88
                        'changes'  => $changes,
89
                        'producer' => $producer,
90
                        'service'  => $mapper->serviceName,
91
                    ]);
92
                }
93
94
                $spy->reset();
95
            }
96
        }
97
        return $changes;
98
    }
99
}
100