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) { |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|
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.