Completed
Push — master ( 5a6cbe...c8e6dd )
by Dmitry
05:03
created

Api::process()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 28
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 8
nop 2
crap 30
1
<?php
2
3
namespace Basis\Controller;
4
5
use Basis\Config;
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\Filesystem;
8
use Basis\Runner;
9
use Exception;
10
11
class Api
12
{
13
    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...
14
    {
15
        if (!array_key_exists('rpc', $_REQUEST)) {
16
            return [
17
                'success' => false,
18
                'message' => 'No rpc defined',
19
            ];
20
        }
21
        $data = json_decode($_REQUEST['rpc']);
22
23
        if (!$data) {
24
            return [
25
                'success' => false,
26
                'message' => 'Invalid rpc format',
27
            ];
28
        }
29
30
        $request = is_array($data) ? $data : [$data];
31
32
        $response = [];
33
        foreach ($request as $rpc) {
34
            $result = $this->process($runner, $rpc);
35
            if (property_exists($rpc, 'tid')) {
36
                $result['tid'] = $rpc->tid;
37
            }
38
            $response[] = $result;
39
        }
40
41
        try {
42
            $event->fireChanges();
43
        } catch (Exception $e) {
44
            return ['success' => false, 'message' => $e->getMessage()];
45
        }
46
47
        return is_array($data) ? $response : $response[0];
48
    }
49
50
    private function process($runner, $rpc)
51
    {
52
        if (!property_exists($rpc, 'job')) {
53
            return [
54
                'success' => false,
55
                'message' => 'Invalid rpc format: no job',
56
            ];
57
        }
58
59
        if (!property_exists($rpc, 'params')) {
60
            return [
61
                'success' => false,
62
                'message' => 'Invalid rpc format: no params',
63
            ];
64
        }
65
66
        try {
67
            $params = is_object($rpc->params) ? get_object_vars($rpc->params) : [];
68
            return [
69
                'success' => true,
70
                'data' => $runner->dispatch($rpc->job, $params),
71
            ];
72
        } catch (Exception $e) {
73
            return [
74
                'success' => false,
75
                'message' => $e->getMessage(),
76
                'trace' => explode(PHP_EOL, $e->getTraceAsString()),
77
            ];
78
        }
79
    }
80
}
81