Completed
Push — master ( 3a3faa...cbf651 )
by Dmitry
04:19
created

Api::index()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 24
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 7
nop 1
crap 42
1
<?php
2
3
namespace Basis\Controllers;
4
5
use Basis\Filesystem;
6
use Basis\Runner;
7
use Exception;
8
9
class Api
10
{
11
    public function index(Runner $runner)
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...
12
    {
13
        try {
14
15
            if(!array_key_exists('rpc', $_REQUEST)) {
16
                throw new Exception("No rpc defined");
17
            }
18
            $data = json_decode($_REQUEST['rpc']);
19
            if(!$data) {
20
                throw new Exception("Invalid rpc format");
21
            }
22
23
            if(!$data->job || !$data->params) {
24
                throw new Exception("Invalid rpc format");
25
            }
26
27
            return [
28
                'success' => true,
29
                'data' => $runner->dispatch($data->job, get_object_vars($data->params)),
30
            ];
31
32
        } catch(Exception $e) {
33
34
            return [
35
                'success' => false,
36
                'message' => $e->getMessage()
37
            ];
38
        }
39
    }
40
}