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

Handle   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 12.7 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 8
loc 63
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 8 56 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
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
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