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