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; |
||
7 | use Basis\Runner; |
||
8 | use Exception; |
||
9 | |||
10 | class Api |
||
11 | { |
||
12 | public function __process(Runner $runner, Event $event) |
||
13 | { |
||
14 | return $this->index($runner, $event); |
||
15 | } |
||
16 | |||
17 | public function index(Runner $runner, Event $event) |
||
0 ignored issues
–
show
|
|||
18 | { |
||
19 | if (!array_key_exists('rpc', $_REQUEST)) { |
||
20 | return [ |
||
21 | 'success' => false, |
||
22 | 'message' => 'No rpc defined', |
||
23 | ]; |
||
24 | } |
||
25 | $data = json_decode($_REQUEST['rpc']); |
||
26 | |||
27 | if (!$data) { |
||
28 | return [ |
||
29 | 'success' => false, |
||
30 | 'message' => 'Invalid rpc format', |
||
31 | ]; |
||
32 | } |
||
33 | |||
34 | $request = is_array($data) ? $data : [$data]; |
||
35 | |||
36 | $response = []; |
||
37 | foreach ($request as $rpc) { |
||
38 | $start = microtime(1); |
||
39 | $result = $this->process($runner, $rpc); |
||
40 | if (is_null($result)) { |
||
41 | $result = []; |
||
42 | } |
||
43 | $result['timing'] = microtime(1) - $start; |
||
44 | if (property_exists($rpc, 'tid')) { |
||
45 | $result['tid'] = $rpc->tid; |
||
46 | } |
||
47 | $response[] = $result; |
||
48 | } |
||
49 | |||
50 | try { |
||
51 | $event->fireChanges($request[0]->job); |
||
52 | } catch (Exception $e) { |
||
53 | return ['success' => false, 'message' => 'Fire changes failure: '.$e->getMessage()]; |
||
54 | } |
||
55 | |||
56 | return is_array($data) ? $response : $response[0]; |
||
57 | } |
||
58 | |||
59 | private function process($runner, $rpc) |
||
60 | { |
||
61 | if (!property_exists($rpc, 'job')) { |
||
62 | return [ |
||
63 | 'success' => false, |
||
64 | 'message' => 'Invalid rpc format: no job', |
||
65 | ]; |
||
66 | } |
||
67 | |||
68 | if (!property_exists($rpc, 'params')) { |
||
69 | return [ |
||
70 | 'success' => false, |
||
71 | 'message' => 'Invalid rpc format: no params', |
||
72 | ]; |
||
73 | } |
||
74 | |||
75 | try { |
||
76 | $params = is_object($rpc->params) ? get_object_vars($rpc->params) : []; |
||
77 | $data = $runner->dispatch(strtolower($rpc->job), $params); |
||
78 | $data = $this->removeSystemObjects($data); |
||
79 | |||
80 | return [ |
||
81 | 'success' => true, |
||
82 | 'data' => $data, |
||
83 | ]; |
||
84 | } catch (Exception $e) { |
||
85 | $error = [ |
||
86 | 'success' => false, |
||
87 | 'message' => $e->getMessage(), |
||
88 | 'trace' => explode(PHP_EOL, $e->getTraceAsString()), |
||
89 | ]; |
||
90 | if (property_exists($e, 'remoteTrace')) { |
||
91 | $error['remoteTrace'] = $e->remoteTrace; |
||
92 | } |
||
93 | return $error; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | private function removeSystemObjects($data) |
||
98 | { |
||
99 | if (!$data) { |
||
100 | return []; |
||
101 | } |
||
102 | |||
103 | if (is_object($data)) { |
||
104 | $data = get_object_vars($data); |
||
105 | } |
||
106 | |||
107 | foreach ($data as $k => $v) { |
||
108 | if (is_array($v) || is_object($v)) { |
||
109 | if ($v instanceof Application) { |
||
110 | unset($data[$k]); |
||
111 | } else { |
||
112 | $data[$k] = $this->removeSystemObjects($v); |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | |||
117 | return $data; |
||
118 | } |
||
119 | } |
||
120 |
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: