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

Api   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 32
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B index() 0 29 6
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
}