Completed
Push — master ( 45346a...33a6cb )
by Dmitry
05:07
created

Api::__process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Basis\Controller;
4
5
use Basis\Application;
6
use Basis\Event;
0 ignored issues
show
Bug introduced by
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 __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
Coding Style introduced by
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...
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;
0 ignored issues
show
Bug introduced by
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...
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