ApiContainerController::actionIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace roaresearch\yii2\roa\controllers;
4
5
use Yii;
6
use yii\{
7
    helpers\ArrayHelper,
8
    web\GoneHttpException,
9
    web\NotFoundHttpException
10
};
11
12
/**
13
 * Lists all the available versions for an api and handles error responses.
14
 *
15
 * @property \roaresearch\yii2\roa\modules\ApiContainer $module
16
 *
17
 * @author Angel (Faryshta) Guevara <[email protected]>
18
 */
19
class ApiContainerController extends \yii\rest\Controller
20
{
21
    /**
22
     * @inheritdoc
23
     */
24 1
    public function behaviors()
25
    {
26 1
        return [];
27
    }
28
29
    /**
30
     * Lists the available versions and their respective stability for the
31
     * parent module.
32
     *
33
     * @return string[]
34
     */
35 1
    public function actionIndex(): array
36
    {
37 1
        return ArrayHelper::map(
38 1
            $this->module->versionModules,
0 ignored issues
show
Bug introduced by
It seems like $this->module->versionModules can also be of type object; however, parameter $array of yii\helpers\BaseArrayHelper::map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            /** @scrutinizer ignore-type */ $this->module->versionModules,
Loading history...
Bug Best Practice introduced by
The property versionModules does not exist on roaresearch\yii2\roa\modules\ApiContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
39
            'id',
40
            'factSheet'
41
        );
42
    }
43
44
    /**
45
     * Handles the exceptions catched by the system bootstrapping process.
46
     * @return \Exception
47
     */
48
    public function actionError(): \Exception
49
    {
50
        if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
51
            $exception = new NotFoundHttpException(
52
                Yii::t('yii', 'Page not found.')
53
            );
54
        }
55
56
        Yii::$app->response->statusCode = isset($exception->statusCode)
57
            ? $exception->statusCode
58
            : 500;
59
60
        return $exception;
61
    }
62
63
    /**
64
     * Action shown when a resource is  no longer available.
65
     *
66
     * @throws GoneHttpException
67
     */
68
    public function actionGone()
69
    {
70
        throw new GoneHttpException(
71
            'The resource you seek is obsolete, visit '
72
                . $this->module->getSelfLink()
73
                . ' to get the fact sheets of all available versions.'
74
        );
75
    }
76
}
77