Completed
Push — master ( b69d36...99d479 )
by Klochok
07:59
created

ReminderController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 10
Bugs 0 Features 5
Metric Value
c 10
b 0
f 5
dl 0
loc 125
ccs 0
cts 108
cp 0
rs 10
wmc 10
lcom 1
cbo 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A behaviors() 0 9 1
B actions() 0 87 4
A getPeriodicityOptions() 0 4 1
A getTypeReminder() 0 4 1
A actionGetCount() 0 9 2
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\RenderAjaxAction;
11
use hipanel\actions\RenderJsonAction;
12
use hipanel\actions\SmartCreateAction;
13
use hipanel\actions\SmartDeleteAction;
14
use hipanel\actions\SmartUpdateAction;
15
use hipanel\actions\ValidateFormAction;
16
use hipanel\actions\ViewAction;
17
use hipanel\models\Reminder;
18
use hipanel\widgets\ReminderTop;
19
use Symfony\Component\EventDispatcher\Event;
20
use Yii;
21
use yii\base\Widget;
22
use yii\helpers\ArrayHelper;
23
use yii\web\NotFoundHttpException;
24
use yii\web\Response;
25
26
class ReminderController extends \hipanel\base\CrudController
27
{
28
    public function init()
29
    {
30
        $this->viewPath = '@hipanel/views/reminder';
31
    }
32
33
    public function behaviors()
34
    {
35
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(array('clas...y' => array('count'))); (array<string,string|string[]>[]) is incompatible with the return type of the parent method hipanel\base\Controller::behaviors of type array<string,array<strin...ng,string[]|boolean>>>>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
36
            [
37
                'class' => 'yii\filters\HttpCache',
38
                'only' => ['count'],
39
            ],
40
        ];
41
    }
42
43
    public function actions()
44
    {
45
        return [
46
            'set-orientation' => [
47
                'class' => OrientationAction::class,
48
                'allowedRoutes' => [
49
                    'reminder/index'
50
                ]
51
            ],
52
            'validate-form' => [
53
                'class' => ValidateFormAction::class,
54
            ],
55
            'view' => [
56
                'class' => ViewAction::class,
57
            ],
58
            'index' => [
59
                'class' => IndexAction::class,
60
                '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...
61
                    return [
62
63
                    ];
64
                },
65
            ],
66
            'create-modal' => [
67
                'class' => SmartCreateAction::class,
68
                'scenario' => 'create',
69
                'view' => 'create-modal',
70
                'data' => function ($action, $data) {
71
                    $object_id = \Yii::$app->request->get('object_id');
72
                    if (empty($object_id)) {
73
                        throw new NotFoundHttpException('Object ID is missing');
74
                    }
75
                    $data['model']->object_id = $object_id;
76
77
                    return $data;
78
                },
79
            ],
80
            'create' => [
81
                'class' => SmartCreateAction::class,
82
                'view' => 'create-modal',
83
            ],
84
            'update' => [
85
                'class' => SmartUpdateAction::class,
86
                'on beforeSave' => function ($event) {
87
                    /** @var \hipanel\actions\Action $action */
88
                    $action = $event->sender;
89
                    if (Yii::$app->request->isAjax) {
90
                        $reminder = Yii::$app->request->post('Reminder');
91
                        $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...
92
                        foreach ($action->collection->models as $model) {
93
                            $model->next_time = (new DateTime($model->next_time))->modify($reminder['next_time'])->format('Y-m-d H:i:s');
94
                        }
95
                    }
96
                },
97
                'POST ajax' => [ 'save' => true,
98
                    'success' => [
99
                        'class' => RenderJsonAction::class,
100
                        '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...
101
                            return [
102
                                'success' => true,
103
                                'widget' => ReminderTop::widget()
104
                            ];
105
                        },
106
                    ],
107
                ],
108
            ],
109
            'delete' => [
110
                'class' => SmartDeleteAction::class,
111
                'POST html | POST pjax' => [
112
                    'save' => true,
113
                    'success' => [
114
                        'class' => RedirectAction::class,
115
                    ],
116
                ],
117
            ],
118
            'ajax-reminders-list' => [
119
                'class' => RenderAjaxAction::class,
120
                'view' => '_ajaxReminderList',
121
                'params' => function ($action) {
122
                    $reminders = Reminder::find()->where(['to_site' => true])->all();
123
                    $remindInOptions = Reminder::reminderNextTimeOptions();
124
125
                    return compact(['reminders', 'remindInOptions']);
126
                }
127
            ]
128
        ];
129
    }
130
131
    public function getPeriodicityOptions()
132
    {
133
        return $this->getRefs('type,periodicity', 'hipanel/reminder');
134
    }
135
136
    public function getTypeReminder()
137
    {
138
        return $this->getRefs('type,reminder', 'hipanel/reminder');
139
    }
140
141
    public function actionGetCount()
142
    {
143
        if (Yii::$app->request->isAjax) {
144
            Yii::$app->response->format = Response::FORMAT_JSON;
145
            $count = Reminder::find()->toSite()->count();
146
147
            return compact('count');
148
        }
149
    }
150
}
151