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

src/Job/Module/Handle.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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