RecurrenceController::actionFrequencyTypes()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
namespace app\modules\v1\controllers;
4
5
use app\core\exceptions\InvalidArgumentException;
6
use app\core\models\Recurrence;
7
use app\core\requests\RecurrenceUpdateStatusRequest;
8
use app\core\traits\ServiceTrait;
9
use app\core\types\RecurrenceFrequency;
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\db\Exception;
13
use yii\web\NotFoundHttpException;
14
15
/**
16
 * Recurrence controller for the `v1` module
17
 */
18
class RecurrenceController extends ActiveController
19
{
20
    use ServiceTrait;
21
22
    public $modelClass = Recurrence::class;
23
    public $partialMatchAttributes = ['name'];
24
25
    /**
26
     * @param int $id
27
     * @return Recurrence
28
     * @throws Exception
29
     * @throws InvalidArgumentException
30
     * @throws NotFoundHttpException
31
     * @throws InvalidConfigException
32
     */
33
    public function actionUpdateStatus(int $id): Recurrence
34
    {
35
        $params = Yii::$app->request->bodyParams;
36
        $model = new RecurrenceUpdateStatusRequest();
37
        /** @var RecurrenceUpdateStatusRequest $model */
38
        $model = $this->validate($model, $params);
39
40
        return $this->recurrenceService->updateStatus($id, $model->status);
41
    }
42
43
44
    /**
45
     * @return array
46
     * @throws \Exception
47
     */
48
    public function actionFrequencyTypes()
49
    {
50
        $items = [];
51
        $texts = RecurrenceFrequency::texts();
52
        foreach (RecurrenceFrequency::names() as $key => $name) {
53
            $items[] = ['type' => $name, 'name' => data_get($texts, $key)];
54
        }
55
        return $items;
56
    }
57
}
58