Completed
Push — master ( 2bf8bf...1a74b9 )
by Klochok
15:40
created

ReminderController::actions()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 78
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 8
Bugs 0 Features 4
Metric Value
cc 4
eloc 50
c 8
b 0
f 4
nc 1
nop 0
dl 0
loc 78
ccs 0
cts 47
cp 0
crap 20
rs 8.5839

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace hipanel\controllers;
4
5
use DateTime;
6
use hipanel\actions\IndexAction;
7
use hipanel\actions\OrientationAction;
8
use hipanel\actions\RedirectAction;
9
use hipanel\actions\RenderAction;
10
use hipanel\actions\RenderJsonAction;
11
use hipanel\actions\SmartCreateAction;
12
use hipanel\actions\SmartDeleteAction;
13
use hipanel\actions\SmartUpdateAction;
14
use hipanel\actions\ValidateFormAction;
15
use hipanel\actions\ViewAction;
16
use hipanel\models\Reminder;
17
use hipanel\widgets\ReminderTop;
18
use Symfony\Component\EventDispatcher\Event;
19
use Yii;
20
use yii\base\Widget;
21
use yii\helpers\ArrayHelper;
22
use yii\web\NotFoundHttpException;
23
24
class ReminderController extends \hipanel\base\CrudController
25
{
26
    public function init()
27
    {
28
        $this->viewPath = '@hipanel/views/reminder';
29
    }
30
31
    public function actions()
32
    {
33
        return [
34
            'set-orientation' => [
35
                'class' => OrientationAction::class,
36
                'allowedRoutes' => [
37
                    'reminder/index'
38
                ]
39
            ],
40
            'validate-form' => [
41
                'class' => ValidateFormAction::class,
42
            ],
43
            'view' => [
44
                'class' => ViewAction::class,
45
            ],
46
            'index' => [
47
                'class' => IndexAction::class,
48
                'data' => function ($action, $data) {
0 ignored issues
show
Unused Code introduced by
The parameter $action 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 introduced by
The parameter $data 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...
49
                    return [
50
51
                    ];
52
                },
53
            ],
54
            'create-modal' => [
55
                'class' => SmartCreateAction::class,
56
                'scenario' => 'create',
57
                'view' => 'create-modal',
58
                'data' => function ($action, $data) {
59
                    $object_id = \Yii::$app->request->get('object_id');
60
                    if (empty($object_id)) {
61
                        throw new NotFoundHttpException('Object ID is missing');
62
                    }
63
                    $data['model']->object_id = $object_id;
64
65
                    return $data;
66
                },
67
            ],
68
            'create' => [
69
                'class' => SmartCreateAction::class,
70
                'view' => 'create-modal',
71
            ],
72
            'update' => [
73
                'class' => SmartUpdateAction::class,
74
                'on beforeSave' => function ($event) {
75
                    /** @var \hipanel\actions\Action $action */
76
                    $action = $event->sender;
77
                    if (Yii::$app->request->isAjax) {
78
                        $reminder = Yii::$app->request->post('Reminder');
79
                        $action->collection->set(Reminder::find()->where(['id' => $reminder['id']])->one());
0 ignored issues
show
Bug introduced by
It seems like \hipanel\models\Reminder...reminder['id']))->one() targeting hiqdev\hiart\ActiveQuery::one() can also be of type null; however, hiqdev\hiart\Collection::set() does only seem to accept array|object<hiqdev\hiart\ActiveRecord>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
80
                        foreach ($action->collection->models as $model) {
81
                            $model->next_time = (new DateTime($model->next_time))->modify($reminder['next_time'])->format('Y-m-d H:i:s');
82
                        }
83
                    }
84
                },
85
                'POST ajax' => [
86
                    'save' => true,
87
                    'success' => [
88
                        'class' => RenderJsonAction::class,
89
                        'return' => function ($action) {
0 ignored issues
show
Unused Code introduced by
The parameter $action 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...
90
                            return [
91
                                'success' => true,
92
                                'widget' => ReminderTop::widget()
93
                            ]; // todo: wise resulting
94
                        },
95
                    ],
96
                ],
97
            ],
98
            'delete' => [
99
                'class' => SmartDeleteAction::class,
100
                'POST html | POST pjax' => [
101
                    'save' => true,
102
                    'success' => [
103
                        'class' => RedirectAction::class,
104
                    ],
105
                ],
106
            ]
107
        ];
108
    }
109
110
    public function getPeriodicityOptions()
111
    {
112
        return $this->getRefs('type,periodicity', 'hipanel/reminder');
113
    }
114
115
    public function getTypeReminder()
116
    {
117
        return $this->getRefs('type,reminder', 'hipanel/reminder');
118
    }
119
}
120