Completed
Push — master ( 95b8e4...b19727 )
by Dmitry
06:52
created

src/Controller/Api.php (3 issues)

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;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Basis\Controller\Event.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Basis\Runner;
8
use Exception;
9
10
class Api
11
{
12
    public function index(Runner $runner, Event $event)
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...
13
    {
14
        if (!array_key_exists('rpc', $_REQUEST)) {
15
            return [
16
                'success' => false,
17
                'message' => 'No rpc defined',
18
            ];
19
        }
20
        $data = json_decode($_REQUEST['rpc']);
21
22
        if (!$data) {
23
            return [
24
                'success' => false,
25
                'message' => 'Invalid rpc format',
26
            ];
27
        }
28
29
        $request = is_array($data) ? $data : [$data];
30
31
        $response = [];
32
        foreach ($request as $rpc) {
33
            $result = $this->process($runner, $rpc);
34
            if (property_exists($rpc, 'tid')) {
35
                $result['tid'] = $rpc->tid;
36
            }
37
            $response[] = $result;
38
        }
39
40
        try {
41
            $event->fireChanges($request[0]->job);
42
        } catch (Exception $e) {
43
            return ['success' => false, 'message' => 'Fire changes failure: '.$e->getMessage()];
44
        }
45
46
        return is_array($data) ? $response : $response[0];
47
    }
48
49
    private function process($runner, $rpc)
50
    {
51
        if (!property_exists($rpc, 'job')) {
52
            return [
53
                'success' => false,
54
                'message' => 'Invalid rpc format: no job',
55
            ];
56
        }
57
58
        if (!property_exists($rpc, 'params')) {
59
            return [
60
                'success' => false,
61
                'message' => 'Invalid rpc format: no params',
62
            ];
63
        }
64
65
        try {
66
            $params = is_object($rpc->params) ? get_object_vars($rpc->params) : [];
67
            $data = $runner->dispatch(strtolower($rpc->job), $params);
68
            $data = $this->removeSystemObjects($data);
69
70
            return [
71
                'success' => true,
72
                'data' => $data,
73
            ];
74
        } catch (Exception $e) {
75
            $error = [
76
                'success' => false,
77
                'message' => $e->getMessage(),
78
                'trace' => explode(PHP_EOL, $e->getTraceAsString()),
79
            ];
80
            if (property_exists($e, 'remoteTrace')) {
81
                $error['remoteTrace'] = $e->remoteTrace;
0 ignored issues
show
The property remoteTrace does not seem to exist in Exception.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
82
            }
83
            return $error;
84
        }
85
    }
86
87
    private function removeSystemObjects($data)
88
    {
89
        if (!$data) {
90
            return [];
91
        }
92
93
        if (is_object($data)) {
94
            $data = get_object_vars($data);
95
        }
96
97
        foreach ($data as $k => $v) {
98
            if (is_array($v) || is_object($v)) {
99
                if ($v instanceof Application) {
100
                    unset($data[$k]);
101
                } else {
102
                    $data[$k] = $this->removeSystemObjects($v);
103
                }
104
            }
105
        }
106
107
        return $data;
108
    }
109
}
110