Completed
Push — master ( 96f8f0...f08c31 )
by Dmitry
02:59
created

Event   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 45.1%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 0
loc 93
ccs 23
cts 51
cp 0.451
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscription() 0 18 5
A fire() 0 7 1
A fireChangesPart() 0 7 2
A hasChanges() 0 11 3
B fireChanges() 0 41 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
    use Toolkit;
13
14 7
    public function getSubscription()
15
    {
16 7
        $subscription = [];
17 7
        foreach ($this->get(Filesystem::class)->listClasses('Listener') as $class) {
18 7
            $reflection = new ReflectionClass($class);
19 7
            if ($reflection->isAbstract()) {
20
                continue;
21
            }
22 7
            foreach ($reflection->getStaticPropertyValue('events') as $event) {
23 7
                if (!array_key_exists($event, $subscription)) {
24 7
                    $subscription[$event] = [];
25
                }
26 7
                $subscription[$event][] = substr($class, strlen('Listener\\'));
27
            }
28
        }
29
30 7
        return $subscription;
31
    }
32
33 1
    public function fire(string $event, $context)
34
    {
35 1
        $this->app->dispatch('event.fire', [
36 1
            'event'   => $this->get(Service::class)->getName().'.'.$event,
37 1
            'context' => $context,
38
        ]);
39 1
    }
40
41
    public function fireChangesPart(string $producer, int $fraction = 10)
42
    {
43
        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...
44
            return $this->fireChanges($producer);
45
        }
46
        return false;
47
    }
48
49
    public function hasChanges()
50
    {
51
        $hasChanges = false;
52
53
        foreach ($this->get(Pool::class)->getMappers() as $mapper) {
54
            if ($mapper->getPlugin(Spy::class)->hasChanges()) {
55
                $hasChanges = true;
56
            }
57
        }
58
        return $hasChanges;
59
    }
60
61 1
    public function fireChanges(string $producer)
62
    {
63 1
        $this->get(Pool::class)->get($this->get(Service::class)->getName());
64
65 1
        $dispatcher = $this->app->get(Dispatcher::class);
0 ignored issues
show
Unused Code introduced by
$dispatcher is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66 1
        $changed = false;
67
68 1
        foreach ($this->get(Pool::class)->getMappers() as $mapper) {
69 1
            $spy = $mapper->getPlugin(Spy::class);
70 1
            if ($spy->hasChanges()) {
71
                // reduce changes list
72
                $changes = $spy->getChanges();
73
                foreach ($changes as $action => $collection) {
74
                    foreach ($collection as $space => $entities) {
75
                        $event = $this->get(Service::class)->getName().'.'.$space.'.'.$action;
76
77
                        if (!$this->get(Service::class)->eventExists($event)) {
78
                            unset($collection[$space]);
79
                        }
80
                    }
81
                    if (!count($collection)) {
82
                        unset($changes->$action);
83
                    }
84
                }
85
86
                if (count(get_object_vars($changes))) {
87
                    $changed = true;
88
                    $data = $this->get(Converter::class)->toArray([
89
                        'changes'  => $changes,
90
                        'producer' => $producer,
91
                        'service'  => $mapper->serviceName,
92
                        'context' => $this->get(Context::class),
93
                    ]);
94
                    $this->getQueue('event.changes')->put($data);
95
                }
96
97
                $spy->reset();
98
            }
99
        }
100 1
        return $changed;
101
    }
102
}
103