Completed
Push — master ( 17d161...198cd4 )
by Dmitry
11:32
created

ApiController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 38
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A runAction() 0 6 1
A actionCommand() 0 7 1
A buildCommandName() 0 4 1
1
<?php
2
/**
3
 * HiAPI Yii2 base project for building API
4
 *
5
 * @link      https://github.com/hiqdev/hiapi
6
 * @package   hiapi
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiapi\console;
12
13
use hiapi\bus\ApiCommandsBusInterface;
14
use yii\base\Module;
15
use yii\console\Controller;
16
use yii\helpers\Inflector;
17
18
/**
19
 * Class ApiController
20
 *
21
 * @author Andrii Vasyliev <[email protected]>
22
 */
23
class ApiController extends Controller
24
{
25
26
    /**
27
     * @var AutoBusInterface|BranchedAutoBus
28
     */
29
    private $autoBus;
30
31
    public function __construct(
32
        string $id,
33
        Module $module,
34
        ApiCommandsBusInterface $autoBus,
35
        array $config = []
36
    ) {
37
        parent::__construct($id, $module, $config);
38
        $this->autoBus = $autoBus;
0 ignored issues
show
Documentation Bug introduced by
It seems like $autoBus of type object<hiapi\bus\ApiCommandsBusInterface> is incompatible with the declared type object<hiapi\console\Aut...onsole\BranchedAutoBus> of property $autoBus.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
    }
40
41
    public function runAction($id, $params = [])
42
    {
43
        $args = [$params[0], $params['_aliases'] ?? []];
44
45
        return \yii\base\Controller::runAction($id, $args);
46
    }
47
48
    public function actionCommand($route, $args)
49
    {
50
        [$resource, $action] = explode('/', $route, 2);
0 ignored issues
show
Bug introduced by
The variable $resource does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $action does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
51
        $result = $this->autoBus->runCommand($this->buildCommandName($resource, $action), $args);
52
53
        echo print_r($result, true);
54
    }
55
56
    private function buildCommandName($resource, $action, $bulk = false) // todo use $bulk
57
    {
58
        return $resource . ucfirst(Inflector::id2camel($action));
59
    }
60
}
61