Completed
Push — master ( 680409...f2b9a8 )
by Dmitry
02:54
created

src/Controller/Api.php (1 issue)

Upgrade to new PHP Analysis Engine

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 Basis\Service;
9
use Basis\Toolkit;
10
use Exception;
11
12
class Api
13
{
14
    use Toolkit;
15
16
    public function __process()
17
    {
18
        return $this->index();
19
    }
20
21
    public function index()
0 ignored issues
show
index uses the super-global variable $_REQUEST which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
22
    {
23
        if (!array_key_exists('rpc', $_REQUEST)) {
24
            return [
25
                'success' => false,
26
                'message' => 'No rpc defined',
27
            ];
28
        }
29
        $data = json_decode($_REQUEST['rpc']);
30
31
        if (!$data) {
32
            return [
33
                'success' => false,
34
                'message' => 'Invalid rpc format',
35
            ];
36
        }
37
38
        $request = is_array($data) ? $data : [$data];
39
40
        $response = [];
41
        foreach ($request as $rpc) {
42
            $start = microtime(1);
43
            $result = $this->process($rpc);
44
            if (is_null($result)) {
45
                $result = [];
46
            }
47
            $result['timing'] = microtime(1) - $start;
48
            if (property_exists($rpc, 'tid')) {
49
                $result['tid'] = $rpc->tid;
50
            }
51
            $response[] = $result;
52
        }
53
54
        try {
55
            $this->get(Event::class)->fireChanges($request[0]->job);
56
        } catch (Exception $e) {
57
            return ['success' => false, 'message' => 'Fire changes failure: '.$e->getMessage()];
58
        }
59
60
        return is_array($data) ? $response : $response[0];
61
    }
62
63
    private function process($rpc)
64
    {
65
        if (!property_exists($rpc, 'job')) {
66
            return [
67
                'success' => false,
68
                'message' => 'Invalid rpc format: no job',
69
            ];
70
        }
71
72
        if (!property_exists($rpc, 'params')) {
73
            return [
74
                'success' => false,
75
                'message' => 'Invalid rpc format: no params',
76
            ];
77
        }
78
79
        try {
80
            $params = is_object($rpc->params) ? get_object_vars($rpc->params) : [];
81
            $data = $this->get(Runner::class)->dispatch(strtolower($rpc->job), $params);
82
            $data = $this->removeSystemObjects($data);
83
84
            return [
85
                'success' => true,
86
                'data' => $data,
87
            ];
88
        } catch (Exception $e) {
89
            $error = [
90
                'success' => false,
91
                'message' => $e->getMessage(),
92
                'service' => $this->get(Service::class)->getName(),
93
                'trace' => explode(PHP_EOL, $e->getTraceAsString()),
94
            ];
95
            if (property_exists($e, 'remoteTrace')) {
96
                $error['remoteTrace'] = $e->remoteTrace;
97
            }
98
            return $error;
99
        }
100
    }
101
102
    private function removeSystemObjects($data)
103
    {
104
        if (!$data) {
105
            return [];
106
        }
107
108
        if (is_object($data)) {
109
            $data = get_object_vars($data);
110
        }
111
112
        foreach ($data as $k => $v) {
113
            if (is_array($v) || is_object($v)) {
114
                if ($v instanceof Application) {
115
                    unset($data[$k]);
116
                } else {
117
                    $data[$k] = $this->removeSystemObjects($v);
118
                }
119
            }
120
        }
121
122
        return $data;
123
    }
124
}
125