| Conditions | 9 |
| Paths | 18 |
| Total Lines | 46 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 90 |
| Changes | 0 | ||
| 1 | <?php |
||
| 12 | public function index(Application $app, BasisEvent $event, Service $service) |
||
|
|
|||
| 13 | { |
||
| 14 | try { |
||
| 15 | if (!array_key_exists('event', $_REQUEST)) { |
||
| 16 | throw new Exception('No event defined'); |
||
| 17 | } |
||
| 18 | |||
| 19 | if (!array_key_exists('context', $_REQUEST)) { |
||
| 20 | throw new Exception('No context defined'); |
||
| 21 | } |
||
| 22 | |||
| 23 | $context = json_decode($_REQUEST['context']); |
||
| 24 | |||
| 25 | if (!$context) { |
||
| 26 | throw new Exception('Invalid context'); |
||
| 27 | } |
||
| 28 | |||
| 29 | $subscription = $event->getSubscription(); |
||
| 30 | |||
| 31 | if (!array_key_exists($_REQUEST['event'], $subscription)) { |
||
| 32 | $service->unsubscribe($_REQUEST['event']); |
||
| 33 | throw new Exception("No subscription on event ".$_REQUEST['event'], 1); |
||
| 34 | } |
||
| 35 | |||
| 36 | $result = []; |
||
| 37 | |||
| 38 | foreach ($subscription[$_REQUEST['event']] as $listener) { |
||
| 39 | $instance = $app->get('Listener\\'.$listener); |
||
| 40 | foreach ($context as $k => $v) { |
||
| 41 | $instance->$k = $v; |
||
| 42 | } |
||
| 43 | if (!method_exists($instance, 'run')) { |
||
| 44 | throw new Exception('No run method for '.$class); |
||
| 45 | } |
||
| 46 | |||
| 47 | $result[$listener] = $app->call([$instance, 'run']); |
||
| 48 | } |
||
| 49 | |||
| 50 | return [ |
||
| 51 | 'success' => true, |
||
| 52 | 'data' => $result, |
||
| 53 | ]; |
||
| 54 | } catch (Exception $e) { |
||
| 55 | return ['success' => false, 'message' => $e->getMessage()]; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 |
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: