Completed
Push — master ( 932cc5...b796f0 )
by Andrii
12:16
created

ApiController::buildCommandName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 3
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\controllers;
12
13
use hiapi\bus\ApiCommandsBusInterface;
14
use hiapi\components\QueryParamAuth;
15
use hiqdev\yii2\autobus\components\AutoBusInterface;
16
use hiqdev\yii2\autobus\components\BranchedAutoBus;
17
use Yii;
18
use yii\base\Module;
19
use yii\filters\auth\CompositeAuth;
20
use yii\filters\auth\HttpBearerAuth;
21
use yii\filters\ContentNegotiator;
22
use yii\web\Controller;
23
use yii\web\Response;
24
25
/**
26
 * Class ApiController
27
 *
28
 * @author Dmytro Naumenko <[email protected]>
29
 */
30
class ApiController extends Controller
31
{
32
    /**
33
     * @var \yii\web\Response
34
     */
35
    private $response;
36
    /**
37
     * @var AutoBusInterface|BranchedAutoBus
38
     */
39
    private $autoBus;
40
41
    public function __construct(
42
        string $id,
43
        Module $module,
44
        ApiCommandsBusInterface $autoBus,
45
        array $config = []
46
    ) {
47
        parent::__construct($id, $module, $config);
48
        $this->autoBus = $autoBus;
49
        $this->response = Yii::$app->response; // TODO: di
50
    }
51
52
    public function behaviors()
53
    {
54
        return array_merge(parent::behaviors(), [
55
            'contentNegotiator' => [
56
                'class' => ContentNegotiator::class,
57
                'formats' => [
58
                    'application/json' => Response::FORMAT_JSON,
59
                ],
60
            ],
61
            'auth' => [
62
                'class' => CompositeAuth::class,
63
                'optional' => ['command'],
64
                'authMethods' => [
65
                    HttpBearerAuth::class,
66
                    [
67
                        'class' => QueryParamAuth::class,
68
                        'tokenParam' => 'access_token',
69
                    ],
70
                ],
71
            ],
72
        ]);
73
    }
74
75
    public function actionCommand($version, $resource, $action, $bulk = false) // todo: use $version, $bulk
0 ignored issues
show
Unused Code introduced by
The parameter $version 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...
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
76
    {
77
        $handledCommand = $this->autoBus->runCommand($this->buildCommandName($resource, $action, $bulk), []);
78
79
        $this->response->setHeaders($handledCommand->getHeaders());
80
        $this->response->setStatusCode($handledCommand->getStatusCode());
81
        $this->response->setBody($handledCommand->getBody());
82
83
        return $this->response->send();
84
    }
85
86
    private function buildCommandName($resource, $action, $bulk = false) // todo use $bulk
0 ignored issues
show
Unused Code introduced by
The parameter $bulk 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...
87
    {
88
        return $resource . ucfirst($action);
89
    }
90
}
91