Passed
Push — master ( aaaee4...c2a29b )
by Mihail
04:03
created

Comments   B

Complexity

Total Complexity 27

Size/Duplication

Total Lines 218
Duplicated Lines 33.49 %

Coupling/Cohesion

Components 0
Dependencies 17

Importance

Changes 6
Bugs 5 Features 0
Metric Value
wmc 27
c 6
b 5
f 0
lcom 0
cbo 17
dl 73
loc 218
rs 7.8571

6 Methods

Rating   Name   Duplication   Size   Complexity  
B actionIndex() 26 26 1
A actionRead() 0 13 3
C actionEdit() 0 32 7
C actionDelete() 0 45 12
B actionAnswerlist() 26 26 1
A actionSettings() 21 21 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
18
/**
19
 * Class Comments. Admin controller for management user comments.
20
 * This class provide general admin implementation of control for user comments and its settings.
21
 * @package Apps\Controller\Admin
22
 */
23
class Comments extends AdminController
24
{
25
    const VERSION = 0.1;
26
    const ITEM_PER_PAGE = 10;
27
28
    const TYPE_COMMENT = 'comment';
29
    const TYPE_ANSWER = 'answer';
30
31
    public $type = 'widget';
32
33
    /**
34
     * List user comments with pagination
35
     * @return string
36
     * @throws \Ffcms\Core\Exception\SyntaxException
37
     */
38 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...
39
    {
40
        // set current page and offset
41
        $page = (int)App::$Request->query->get('page');
42
        $offset = $page * self::ITEM_PER_PAGE;
43
44
        // initialize active record model
45
        $query = new CommentPost();
46
47
        // make pagination
48
        $pagination = new SimplePagination([
49
            'url' => ['comments/index'],
50
            'page' => $page,
51
            'step' => self::ITEM_PER_PAGE,
52
            'total' => $query->count()
53
        ]);
54
55
        // get result as active records object with offset
56
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
57
58
        // render output view
59
        return App::$View->render('index', [
60
            'records' => $records,
61
            'pagination' => $pagination
62
        ]);
63
    }
64
65
    /**
66
     * List comment - read comment and list answers
67
     * @param int $id
68
     * @return string
69
     * @throws NotFoundException
70
     * @throws \Ffcms\Core\Exception\SyntaxException
71
     */
72
    public function actionRead($id)
73
    {
74
        // find object in active record model
75
        $record = CommentPost::find($id);
76
        if ($record === null || $record === false) {
77
            throw new NotFoundException(__('Comment is not founded'));
78
        }
79
80
        // render response
81
        return App::$View->render('comment_read', [
82
            'record' => $record
83
        ]);
84
    }
85
86
    /**
87
     * Commentaries and answers edit action
88
     * @param string $type
89
     * @param int $id
90
     * @throws NotFoundException
91
     * @return string
92
     */
93
    public function actionEdit($type, $id)
94
    {
95
        // get active record by type and id from active records
96
        $record = null;
97
        switch ($type) {
98
            case static::TYPE_COMMENT:
99
                $record = CommentPost::find($id);
100
                break;
101
            case static::TYPE_ANSWER:
102
                $record = CommentAnswer::find($id);
103
                break;
104
        }
105
106
        // check if response is not empty
107
        if ($record === null || $record === false) {
108
            throw new NotFoundException(__('Comment is not founded'));
109
        }
110
        
111
        // init edit model
112
        $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...
113
        
114
        // check if data is submited and validated
115
        if ($model->send() && $model->validate()) {
116
            $model->make();
117
            App::$Session->getFlashBag()->add('success', __('Comment or answer is successful updated'));
118
        }
119
120
        // render view
121
        return App::$View->render('edit', [
122
            'model' => $model->export()
123
        ]);
124
    }
125
    
126
    /**
127
     * Delete comments and answers single or multiply items
128
     * @param string $type
129
     * @param int $id
130
     * @throws NotFoundException
131
     * @return string
132
     */
133
    public function actionDelete($type, $id = 0)
134
    {
135
        // sounds like a multiply delete definition
136
        if ($id === 0 || (int)$id < 1) {
137
            $ids = App::$Request->query->get('selectRemove');
138
            if (Obj::isArray($ids) && Arr::onlyNumericValues($ids)) {
0 ignored issues
show
Bug introduced by
The method onlyNumericValues() does not seem to exist on object<Ffcms\Core\Helper\Type\Arr>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
                $id = $ids;
140
            } else {
141
                throw new NotFoundException('Bad conditions');
142
            }
143
        } else {
144
            $id = [$id];
145
        }
146
        
147
        // prepare query to db
148
        $query = null;
149
        switch ($type) {
150
            case self::TYPE_COMMENT:
151
                $query = CommentPost::whereIn('id', $id);
152
                break;
153
            case self::TYPE_ANSWER:
154
                $query = CommentAnswer::whereIn('id', $id);
155
                break;
156
        }
157
        
158
        // check if result is not empty
159
        if ($query === null || $query->count() < 1) {
160
            throw new NotFoundException(__('No comments found for this condition'));
161
        }
162
        
163
        // initialize model
164
        $model = new FormCommentDelete($query, $type);
165
        
166
        // check if delete is submited
167
        if ($model->send() && $model->validate()) {
168
            $model->make();
169
            App::$Session->getFlashBag()->add('success', __('Comments or answers are successful deleted!'));
170
            App::$Response->redirect('comments/' . ($type === 'answer' ? 'answerlist' : 'index'));
171
        }
172
        
173
        // render view
174
        return App::$View->render('delete', [
175
            'model' => $model
176
        ]);
177
    }
178
179
    /**
180
     * List answers 
181
     * @return string
182
     */
183 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...
184
    {
185
        // set current page and offset
186
        $page = (int)App::$Request->query->get('page');
187
        $offset = $page * self::ITEM_PER_PAGE;
188
        
189
        // initialize ar answers model
190
        $query = new CommentAnswer();
191
        
192
        // build pagination list
193
        $pagination = new SimplePagination([
194
            'url' => ['comments/answerlist'],
195
            'page' => $page,
196
            'step' => self::ITEM_PER_PAGE,
197
            'total' => $query->count()
198
        ]);
199
        
200
        // get result as active records object with offset
201
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
202
        
203
        // render output view
204
        return App::$View->render('answer_list', [
205
            'records' => $records,
206
            'pagination' => $pagination
207
        ]);
208
    }
209
210
    /**
211
     * Comment widget global settings
212
     * @return string
213
     * @throws \Ffcms\Core\Exception\SyntaxException
214
     */
215 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...
216
    {
217
        // initialize settings model
218
        $model = new FormSettings($this->getConfigs());
219
220
        // check if form is send
221
        if ($model->send()) {
222
            if ($model->validate()) {
223
                $this->setConfigs($model->getAllProperties());
224
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
225
                App::$Response->redirect('comments/index');
226
            } else {
227
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
228
            }
229
        }
230
231
        // render view
232
        return App::$View->render('settings', [
233
            'model' => $model
234
        ]);
235
    }
236
237
238
239
240
}