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

Handle   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 78
Duplicated Lines 10.26 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
C run() 8 71 10

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