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\Controller; |
||
4 | |||
5 | use Basis\Application; |
||
6 | use Basis\Event as BasisEvent; |
||
7 | use Basis\Service; |
||
8 | use Exception; |
||
9 | |||
10 | class Event |
||
11 | { |
||
12 | public function index(Application $app, BasisEvent $event, Service $service) |
||
13 | { |
||
14 | $start = microtime(1); |
||
15 | |||
16 | try { |
||
17 | $info = $this->getEventInfo(); |
||
18 | $subscription = $event->getSubscription(); |
||
19 | |||
20 | $patterns = []; |
||
21 | foreach (array_keys($subscription) as $pattern) { |
||
22 | if ($service->eventMatch($info->event, $pattern)) { |
||
23 | $patterns[] = $pattern; |
||
24 | } |
||
25 | } |
||
26 | |||
27 | if (!count($patterns)) { |
||
28 | $service->unsubscribe($info->event); |
||
29 | throw new Exception("No subscription on event ".$info->event); |
||
30 | } |
||
31 | |||
32 | $listeners = []; |
||
33 | foreach ($patterns as $pattern) { |
||
34 | foreach ($subscription[$pattern] as $listener) { |
||
35 | if (!array_key_exists($listener, $listeners)) { |
||
36 | $listeners[$listener] = $app->get('Listener\\'.$listener); |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | |||
41 | $result = []; |
||
42 | $issues = []; |
||
43 | foreach ($listeners as $nick => $listener) { |
||
44 | $result[$nick] = $this->handleEvent($app, $listener, $info); |
||
45 | try { |
||
46 | $event->fireChanges($nick); |
||
47 | } catch (Exception $e) { |
||
48 | $issues[$nick] = $e->getMessage(); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | |||
53 | return [ |
||
54 | 'success' => true, |
||
55 | 'data' => $result, |
||
56 | 'issues' => $issues, |
||
57 | 'time' => microtime(1) - $start, |
||
58 | ]; |
||
59 | |||
60 | } catch (Exception $e) { |
||
61 | return ['success' => false, 'message' => $e->getMessage()]; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | private function getEventInfo() |
||
0 ignored issues
–
show
|
|||
66 | { |
||
67 | if (!array_key_exists('event', $_REQUEST)) { |
||
68 | throw new Exception('No event defined'); |
||
69 | } |
||
70 | |||
71 | if (!array_key_exists('context', $_REQUEST)) { |
||
72 | throw new Exception('No context defined'); |
||
73 | } |
||
74 | |||
75 | $context = json_decode($_REQUEST['context']); |
||
76 | |||
77 | if (!$context) { |
||
78 | throw new Exception('Invalid context'); |
||
79 | } |
||
80 | |||
81 | return (object) [ |
||
82 | 'event' => $_REQUEST['event'], |
||
83 | 'context' => $context, |
||
84 | ]; |
||
85 | } |
||
86 | |||
87 | private function handleEvent($app, $instance, $info) |
||
88 | { |
||
89 | foreach ($info as $k => $v) { |
||
90 | $instance->$k = $v; |
||
91 | } |
||
92 | if (!method_exists($instance, 'run')) { |
||
93 | throw new Exception('No run method for '.$class); |
||
94 | } |
||
95 | |||
96 | return $app->call([$instance, 'run']); |
||
97 | } |
||
98 | } |
||
99 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: