Completed
Push — master ( 4967cd...fefc41 )
by Dmitry
16:49
created

Handle::run()   B

Complexity

Conditions 9
Paths 51

Size

Total Lines 56

Duplication

Lines 8
Ratio 14.29 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 8
loc 56
ccs 0
cts 50
cp 0
rs 7.4044
c 0
b 0
f 0
cc 9
nc 51
nop 4
crap 90

How to fix   Long Method   

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($info->event);
0 ignored issues
show
Bug introduced by Dmitry Krokhin
The variable $info does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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
        $listeners = [];
43
        foreach ($patterns as $pattern) {
44
            foreach ($subscription[$pattern] as $listener) {
45
                if (!array_key_exists($listener, $listeners)) {
46
                    $listeners[$listener] = $app->get('Listener\\'.$listener);
47
                    $listeners[$listener]->event = $this->event;
48
                    $listeners[$listener]->context = $this->context;
49
                }
50
            }
51
        }
52
53
54
        $data = [];
55
        $issues = [];
56 View Code Duplication
        foreach ($listeners as $nick => $listener) {
0 ignored issues
show
Duplication introduced by Dmitry Krokhin
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...
57
            try {
58
                $data[$nick] = $app->call([$listener, 'run']);
59
                $event->fireChanges($nick);
60
            } catch (Exception $e) {
61
                $issues[$nick] =  $e->getMessage();
62
            }
63
        }
64
65
        $dispatcher->send('event.feedback', [
66
            'eventId' => $this->eventId,
67
            'service' => $service->getName(),
68
            'result' => [
69
                'data' => $data,
70
                'issues' => $issues,
71
                'time' => microtime(1) - $start,
72
            ]
73
        ]);
74
    }
75
}
76