Passed
Push — master ( 66552f...f7aa08 )
by Mihail
05:29
created

Comments::actionDelete()   C

Complexity

Conditions 12
Paths 19

Size

Total Lines 45
Code Lines 26

Duplication

Lines 18
Ratio 40 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 18
loc 45
rs 5.1612
cc 12
eloc 26
nc 19
nop 2

How to fix   Complexity   

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 Apps\Controller\Admin;
4
5
6
use Apps\ActiveRecord\CommentAnswer;
7
use Apps\ActiveRecord\CommentPost;
8
use Apps\Model\Admin\Comments\FormSettings;
9
use Extend\Core\Arch\AdminController;
10
use Ffcms\Core\App;
11
use Ffcms\Core\Exception\NotFoundException;
12
use Ffcms\Core\Helper\HTML\SimplePagination;
13
use Ffcms\Core\Helper\Type\Arr;
14
use Apps\Model\Admin\Comments\FormCommentUpdate;
15
use Ffcms\Core\Helper\Type\Obj;
16
use Apps\Model\Admin\Comments\FormCommentDelete;
17
use Apps\Model\Admin\Comments\FormCommentModerate;
18
19
/**
20
 * Class Comments. Admin controller for management user comments.
21
 * This class provide general admin implementation of control for user comments and its settings.
22
 * @package Apps\Controller\Admin
23
 */
24
class Comments extends AdminController
25
{
26
    const VERSION = 0.1;
27
    const ITEM_PER_PAGE = 10;
28
29
    const TYPE_COMMENT = 'comment';
30
    const TYPE_ANSWER = 'answer';
31
32
    public $type = 'widget';
33
34
    /**
35
     * List user comments with pagination
36
     * @return string
37
     * @throws \Ffcms\Core\Exception\SyntaxException
38
     */
39 View Code Duplication
    public function actionIndex()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        // set current page and offset
42
        $page = (int)App::$Request->query->get('page');
43
        $offset = $page * self::ITEM_PER_PAGE;
44
45
        // initialize active record model
46
        $query = new CommentPost();
47
48
        // make pagination
49
        $pagination = new SimplePagination([
50
            'url' => ['comments/index'],
51
            'page' => $page,
52
            'step' => self::ITEM_PER_PAGE,
53
            'total' => $query->count()
54
        ]);
55
56
        // get result as active records object with offset
57
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
58
59
        // render output view
60
        return App::$View->render('index', [
61
            'records' => $records,
62
            'pagination' => $pagination
63
        ]);
64
    }
65
66
    /**
67
     * List comment - read comment and list answers
68
     * @param int $id
69
     * @return string
70
     * @throws NotFoundException
71
     * @throws \Ffcms\Core\Exception\SyntaxException
72
     */
73
    public function actionRead($id)
74
    {
75
        // find object in active record model
76
        $record = CommentPost::find($id);
77
        if ($record === null || $record === false) {
78
            throw new NotFoundException(__('Comment is not founded'));
79
        }
80
81
        // render response
82
        return App::$View->render('comment_read', [
83
            'record' => $record
84
        ]);
85
    }
86
87
    /**
88
     * Commentaries and answers edit action
89
     * @param string $type
90
     * @param int $id
91
     * @throws NotFoundException
92
     * @return string
93
     */
94
    public function actionEdit($type, $id)
95
    {
96
        // get active record by type and id from active records
97
        $record = null;
98
        switch ($type) {
99
            case static::TYPE_COMMENT:
100
                $record = CommentPost::find($id);
101
                break;
102
            case static::TYPE_ANSWER:
103
                $record = CommentAnswer::find($id);
104
                break;
105
        }
106
107
        // check if response is not empty
108
        if ($record === null || $record === false) {
109
            throw new NotFoundException(__('Comment is not founded'));
110
        }
111
112
        // init edit model
113
        $model = new FormCommentUpdate($record, $type);
0 ignored issues
show
Documentation introduced by
$record is of type object<Ffcms\Core\Arch\ActiveModel>, but the function expects a object<Apps\Model\Admin\...veRecord\CommentAnswer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
115
        // check if data is submited and validated
116
        if ($model->send() && $model->validate()) {
117
            $model->make();
118
            App::$Session->getFlashBag()->add('success', __('Comment or answer is successful updated'));
119
        }
120
121
        // render view
122
        return App::$View->render('edit', [
123
            'model' => $model->export()
124
        ]);
125
    }
126
127
    /**
128
     * Delete comments and answers single or multiply items
129
     * @param string $type
130
     * @param int $id
131
     * @throws NotFoundException
132
     * @return string
133
     */
134
    public function actionDelete($type, $id = 0)
135
    {
136
        // sounds like a multiply delete definition
137 View Code Duplication
        if ($id === 0 || (int)$id < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
138
            $ids = App::$Request->query->get('selected');
139
            if (Obj::isArray($ids) && Arr::onlyNumericValues($ids)) {
140
                $id = $ids;
141
            } else {
142
                throw new NotFoundException('Bad conditions');
143
            }
144
        } else {
145
            $id = [$id];
146
        }
147
148
        // prepare query to db
149
        $query = null;
150
        switch ($type) {
151
            case self::TYPE_COMMENT:
152
                $query = CommentPost::whereIn('id', $id);
153
                break;
154
            case self::TYPE_ANSWER:
155
                $query = CommentAnswer::whereIn('id', $id);
156
                break;
157
        }
158
159
        // check if result is not empty
160 View Code Duplication
        if ($query === null || $query->count() < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
            throw new NotFoundException(__('No comments found for this condition'));
162
        }
163
164
        // initialize model
165
        $model = new FormCommentDelete($query, $type);
166
167
        // check if delete is submited
168 View Code Duplication
        if ($model->send() && $model->validate()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
            $model->make();
170
            App::$Session->getFlashBag()->add('success', __('Comments or answers are successful deleted!'));
171
            App::$Response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index'));
172
        }
173
174
        // render view
175
        return App::$View->render('delete', [
176
            'model' => $model
177
        ]);
178
    }
179
180
    /**
181
     * Moderate guest comments and answer - make it publish
182
     * @param string $type
183
     * @param int $id
184
     * @throws NotFoundException
185
     * @return string
186
     */
187
    public function actionPublish($type, $id = 0)
188
    {
189
        // check if it multiple accept ids
190 View Code Duplication
        if ($id === 0 || (int)$id < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
            $ids = App::$Request->query->get('selected');
192
            if (Obj::isArray($ids) && Arr::onlyNumericValues($ids)) {
193
                $id = $ids;
194
            } else {
195
                throw new NotFoundException('Bad conditions');
196
            }
197
        } else {
198
            $id = [$id];
199
        }
200
201
        // build query
202
        $query = null;
203
        switch ($type) {
204
            case static::TYPE_COMMENT:
205
                $query = CommentPost::whereIn('id', $id)->where('moderate', '=', 1);
206
                break;
207
            case static::TYPE_ANSWER:
208
                $query = CommentAnswer::whereIn('id', $id)->where('moderate', '=', 1);
209
                break;
210
        }
211
212
        // check if result is not empty
213 View Code Duplication
        if ($query === null || $query->count() < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
214
            throw new NotFoundException(__('No comments found for this condition'));
215
        }
216
217
        // initialize moderation model
218
        $model = new FormCommentModerate($query, $type);
219
220
        // check if form is submited
221 View Code Duplication
        if ($model->send()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
222
            $model->make();
223
            App::$Session->getFlashBag()->add('success', __('Comments or answers are successful published'));
224
            App::$Response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index'));
225
        }
226
227
        return App::$View->render('publish', [
228
            'model' => $model
229
        ]);
230
    }
231
232
    /**
233
     * List answers
234
     * @return string
235
     */
236 View Code Duplication
    public function actionAnswerlist()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
    {
238
        // set current page and offset
239
        $page = (int)App::$Request->query->get('page');
240
        $offset = $page * self::ITEM_PER_PAGE;
241
242
        // initialize ar answers model
243
        $query = new CommentAnswer();
244
245
        // build pagination list
246
        $pagination = new SimplePagination([
247
            'url' => ['comments/answerlist'],
248
            'page' => $page,
249
            'step' => self::ITEM_PER_PAGE,
250
            'total' => $query->count()
251
        ]);
252
253
        // get result as active records object with offset
254
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
255
256
        // render output view
257
        return App::$View->render('answer_list', [
258
            'records' => $records,
259
            'pagination' => $pagination
260
        ]);
261
    }
262
263
    /**
264
     * Comment widget global settings
265
     * @return string
266
     * @throws \Ffcms\Core\Exception\SyntaxException
267
     */
268 View Code Duplication
    public function actionSettings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
269
    {
270
        // initialize settings model
271
        $model = new FormSettings($this->getConfigs());
272
273
        // check if form is send
274
        if ($model->send()) {
275
            if ($model->validate()) {
276
                $this->setConfigs($model->getAllProperties());
277
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
278
                App::$Response->redirect('comments/index');
279
            } else {
280
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
281
            }
282
        }
283
284
        // render view
285
        return App::$View->render('settings', [
286
            'model' => $model
287
        ]);
288
    }
289
290
291
292
293
}