Completed
Push — master ( 2a6b2d...78117e )
by Dmitry
06:25
created

Api   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 96
ccs 0
cts 80
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C index() 0 36 8
B process() 0 33 5
C removeSystemObjects() 0 22 7
1
<?php
2
3
namespace Basis\Controller;
4
5
use Basis\Application;
6
use Basis\Config;
7
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...
8
use Basis\Filesystem;
9
use Basis\Runner;
10
use Exception;
11
12
class Api
13
{
14
    public function index(Config $config, Runner $runner, Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
15
    {
16
        if (!array_key_exists('rpc', $_REQUEST)) {
17
            return [
18
                'success' => false,
19
                'message' => 'No rpc defined',
20
            ];
21
        }
22
        $data = json_decode($_REQUEST['rpc']);
23
24
        if (!$data) {
25
            return [
26
                'success' => false,
27
                'message' => 'Invalid rpc format',
28
            ];
29
        }
30
31
        $request = is_array($data) ? $data : [$data];
32
33
        $response = [];
34
        foreach ($request as $rpc) {
35
            $result = $this->process($runner, $rpc);
36
            if (property_exists($rpc, 'tid')) {
37
                $result['tid'] = $rpc->tid;
38
            }
39
            $response[] = $result;
40
        }
41
42
        try {
43
            $event->fireChanges();
44
        } catch (Exception $e) {
45
            return ['success' => false, 'message' => $e->getMessage()];
46
        }
47
48
        return is_array($data) ? $response : $response[0];
49
    }
50
51
    private function process($runner, $rpc)
52
    {
53
        if (!property_exists($rpc, 'job')) {
54
            return [
55
                'success' => false,
56
                'message' => 'Invalid rpc format: no job',
57
            ];
58
        }
59
60
        if (!property_exists($rpc, 'params')) {
61
            return [
62
                'success' => false,
63
                'message' => 'Invalid rpc format: no params',
64
            ];
65
        }
66
67
        try {
68
            $params = is_object($rpc->params) ? get_object_vars($rpc->params) : [];
69
            $data = $runner->dispatch($rpc->job, $params);
70
            $data = $this->removeSystemObjects($data);
71
72
            return [
73
                'success' => true,
74
                'data' => $data,
75
            ];
76
        } catch (Exception $e) {
77
            return [
78
                'success' => false,
79
                'message' => $e->getMessage(),
80
                'trace' => explode(PHP_EOL, $e->getTraceAsString()),
81
            ];
82
        }
83
    }
84
85
    private function removeSystemObjects($data)
86
    {
87
        if (!$data) {
88
            return [];
89
        }
90
91
        if (is_object($data)) {
92
            $data = get_object_vars($data);
93
        }
94
95
        foreach ($data as $k => $v) {
96
            if (is_array($v) || is_object($v)) {
97
                if ($v instanceof Application) {
98
                    unset($data[$k]);
99
                } else {
100
                    $data[$k] = $this->removeSystemObjects($v);
101
                }
102
            }
103
        }
104
105
        return $data;
106
    }
107
}
108