Completed
Push — master ( 281390...1cd9a0 )
by Dmitry
11:47 queued 09:45
created

Api::index()   C

Complexity

Conditions 7
Paths 11

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 32
ccs 0
cts 26
cp 0
rs 6.7272
cc 7
eloc 18
nc 11
nop 1
crap 56
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(!property_exists($data, 'job') || !property_exists($data, 'params')) {
24
                throw new Exception("Invalid rpc format");
25
            }
26
27
            $params = is_object($data->params) ? get_object_vars($data->params) : [];
28
29
            return [
30
                'success' => true,
31
                'data' => $runner->dispatch($data->job, $params),
32
            ];
33
34
        } catch(Exception $e) {
35
36
            return [
37
                'success' => false,
38
                'message' => $e->getMessage(),
39
                'trace' => explode(PHP_EOL, $e->getTraceAsString()),
40
            ];
41
        }
42
    }
43
}
44