Completed
Push — master ( b65136...18f303 )
by Dmitry
05:16
created

Handle::run()   C

Complexity

Conditions 10
Paths 99

Size

Total Lines 71

Duplication

Lines 8
Ratio 11.27 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 8
loc 71
ccs 0
cts 61
cp 0
rs 6.766
c 0
b 0
f 0
cc 10
nc 99
nop 4
crap 110

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Basis\Job\Module;
4
5
use Basis\Application;
6
use Basis\Context;
7
use Basis\Dispatcher;
8
use Basis\Event;
9
use Basis\Job;
10
use Basis\Service;
11
use Exception;
12
13
class Handle extends Job
14
{
15
    public $event;
16
    public $eventId;
17
    public $context;
18
19
    public function run(Application $app, Dispatcher $dispatcher, Event $event, Service $service)
20
    {
21
        $start = microtime(1);
22
        $subscription = $event->getSubscription();
23
24
        $patterns = [];
25
        foreach (array_keys($subscription) as $pattern) {
26
            if ($service->eventMatch($this->event, $pattern)) {
27
                $patterns[] = $pattern;
28
            }
29
        }
30
31
        if (!count($patterns)) {
32
            $service->unsubscribe($this->event);
33
            return $dispatcher->send('event.feedback', [
34
                'eventId' => $this->eventId,
35
                'service' => $service->getName(),
36
                'result' => [
37
                    'message' => 'no subscription'
38
                ],
39
            ]);
40
        }
41
42
        $this->get(Context::class)->event = $this->eventId;
43
44
        $parts = explode('.', $this->event);
45
        $action = array_pop($parts);
46
        $space = implode('.', $parts);
47
48
        $listeners = [];
49
        foreach ($patterns as $pattern) {
50
            foreach ($subscription[$pattern] as $listener) {
51
                if (!array_key_exists($listener, $listeners)) {
52
                    $listeners[$listener] = $app->get('Listener\\'.$listener);
53
                    $listeners[$listener]->event = $this->event;
54
                    $listeners[$listener]->eventId = $this->eventId;
55
                    $listeners[$listener]->context = $this->context;
56
                    $listeners[$listener]->space = $space;
57
                    $listeners[$listener]->action = $action;
58
                }
59
            }
60
        }
61
62
63
        $data = [];
64
        $issues = [];
65 View Code Duplication
        foreach ($listeners as $nick => $listener) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
            try {
67
                $data[$nick] = $app->call([$listener, 'run']);
68
                $event->fireChanges($nick);
69
            } catch (Exception $e) {
70
                $issues[$nick] =  $e->getMessage();
71
            }
72
        }
73
74
        $result = [
75
            'data' => $data,
76
            'issues' => $issues,
77
            'time' => microtime(1) - $start,
78
        ];
79
80
        if ($this->sync) {
0 ignored issues
show
Bug introduced by
The property sync 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...
81
            return $result;
82
        }
83
84
        $dispatcher->send('event.feedback', [
85
            'eventId' => $this->eventId,
86
            'service' => $service->getName(),
87
            'result' => $result
88
        ]);
89
    }
90
}
91