Passed
Push — master ( 68443e...939347 )
by Mihail
04:31
created

Feedback::actionRead()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 28
Code Lines 16

Duplication

Lines 3
Ratio 10.71 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 28
rs 8.439
cc 6
eloc 16
nc 5
nop 1
1
<?php
2
3
4
namespace Apps\Controller\Admin;
5
6
use Apps\ActiveRecord\FeedbackAnswer;
7
use Apps\Model\Admin\Feedback\FormAnswerAdd;
8
use Apps\Model\Admin\Feedback\FormSettings;
9
use Apps\Model\Admin\Feedback\FormUpdate;
10
use Extend\Core\Arch\AdminController as Controller;
11
use Ffcms\Core\App;
12
use Apps\ActiveRecord\FeedbackPost;
13
use Ffcms\Core\Exception\NotFoundException;
14
use Ffcms\Core\Helper\HTML\SimplePagination;
15
16
/**
17
 * Class Feedback. Control and manage feedback request and answers.
18
 * @package Apps\Controller\Admin
19
 */
20
class Feedback extends Controller
21
{
22
    const VERSION = '0.1';
23
    const ITEM_PER_PAGE = 10;
24
25
    public $type = 'app';
26
27
    /**
28
     * List feedback post messages with notifications
29
     * @return string
30
     * @throws \Ffcms\Core\Exception\SyntaxException
31
     */
32 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...
33
    {
34
        // set current page and offset
35
        $page = (int)App::$Request->query->get('page');
36
        $offset = $page * self::ITEM_PER_PAGE;
37
38
        // get feedback posts AR table
39
        $query = new FeedbackPost();
40
41
        // build pagination
42
        $pagination = new SimplePagination([
43
            'url' => ['feedback/index'],
44
            'page' => $page,
45
            'step' => self::ITEM_PER_PAGE,
46
            'total' => $query->count()
0 ignored issues
show
Documentation Bug introduced by
The method count does not exist on object<Apps\ActiveRecord\FeedbackPost>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
47
        ]);
48
49
        // build listing objects
50
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
0 ignored issues
show
Documentation Bug introduced by
The method orderBy does not exist on object<Apps\ActiveRecord\FeedbackPost>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
51
52
        // render output
53
        return App::$View->render('index', [
54
            'records' => $records,
55
            'pagination' => $pagination
56
        ]);
57
    }
58
59
    /**
60
     * Read feedback post and answer and add answer to thread post
61
     * @param int $id
62
     * @return string
63
     * @throws NotFoundException
64
     * @throws \Ffcms\Core\Exception\SyntaxException
65
     */
66
    public function actionRead($id)
67
    {
68
        // find feedback post by id
69
        $record = FeedbackPost::find($id);
70 View Code Duplication
        if ($record === null || $record === false) {
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...
71
            throw new NotFoundException(__('The feedback message is not founded'));
72
        }
73
74
        // initialize model with answer add if thread is not closed
75
        $model = null;
76
        if ((int)$record->closed !== 1) {
77
            $model = new FormAnswerAdd($record, App::$User->identity()->getId());
78
            if ($model->send()) {
79
                if ($model->validate()) {
80
                    $model->make();
81
                    App::$Session->getFlashBag()->add('success', __('New answer is successful add'));
82
                } else {
83
                    App::$Session->getFlashBag()->add('error', 'Validation failure');
84
                }
85
            }
86
        }
87
88
        // render view
89
        return App::$View->render('read', [
90
            'record' => $record,
91
            'model' => $model
92
        ]);
93
    }
94
95
    public function actionUpdate($type, $id)
96
    {
97
        // get active record based on type (post or answer for post)
98
        $record = null;
99
        $postId = $id;
100
        switch ($type) {
101
            case 'post':
102
                $record = FeedbackPost::find($id);
103
                break;
104
            case 'answer':
105
                $record = FeedbackAnswer::find($id);
106
                if ($record !== null && $record !== false) {
107
                    $postId = (int)$record->getFeedbackPost()->id;
108
                }
109
                break;
110
        }
111
112
        // try what we got
113 View Code Duplication
        if ($record === null || $record === false) {
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...
114
            throw new NotFoundException(__('Feedback item is not founded'));
115
        }
116
117
        // initialize model
118
        $model = new FormUpdate($record);
119
        if ($model->send()) {
120
            if ($model->validate()) {
121
                $model->make();
122
                App::$Session->getFlashBag()->add('success', __('Feedback item are successful changed'));
123
                App::$Response->redirect('feedback/read/' . $postId);
124
            } else {
125
                App::$Session->getFlashBag()->add('danger', __('Updating is failed'));
126
            }
127
        }
128
129
        // render output view
130
        return App::$View->render('update', [
131
            'model' => $model->export()
132
        ]);
133
    }
134
135
    /**
136
     * Turn feedback request post - close, open, readed
137
     * @param string $direction
138
     * @param int $id
139
     * @return null
140
     * @throws NotFoundException
141
     */
142
    public function actionTurn($direction, $id)
143
    {
144
        // try to find record
145
        $record = FeedbackPost::find($id);
146 View Code Duplication
        if ($record === null || $record === false) {
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...
147
            throw new NotFoundException(__('Feedback request with id %id% is not found', ['id' => $id]));
148
        }
149
150
        // switch operation direction to what we must change
151
        switch ($direction) {
152
            case 'open':
153
                $record->closed = 0;
154
                $record->save();
155
                break;
156
            case 'close':
157
                $record->closed = 1;
158
                $record->save();
159
                break;
160
            case 'read':
161
                $record->readed = 1;
162
                $record->save();
163
                break;
164
            default:
165
                throw new NotFoundException(__('Hack attention'));
166
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
167
        }
168
169
        // add notification of successful changes
170
        App::$Session->getFlashBag()->add('success', __('Feedback request is changed!'));
171
172
        // redirect to feedback post read
173
        App::$Response->redirect('feedback/read/' . $id);
174
        return null;
175
    }
176
177
    /**
178
     * Delete feedback post or answer
179
     * @param string $type
180
     * @param int $id
181
     * @return string
182
     * @throws NotFoundException
183
     * @throws \Exception
184
     * @throws \Ffcms\Core\Exception\SyntaxException
185
     */
186
    public function actionDelete($type, $id)
187
    {
188
        // try to get active record by type
189
        $record = null;
190
        switch ($type) {
191
            case 'post':
192
                $record = FeedbackPost::find($id);
193
                break;
194
            case 'answer':
195
                $record = FeedbackAnswer::find($id);
196
                break;
197
        }
198
199
        // check if we get the row
200 View Code Duplication
        if ($record === null || $record === false) {
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...
201
            throw new NotFoundException(__('Feedback item is not founded'));
202
        }
203
204
        // if delete is submited
205
        if (App::$Request->request->get('deleteFeedback')) {
206
            // remove all answers
207
            if ($type === 'post') {
208
                FeedbackAnswer::where('feedback_id', '=', $record->id)->delete();
209
                // remove item
210
                $record->delete();
211
                App::$Session->getFlashBag()->add('success', __('Feedback record is successful removed'));
212
                App::$Response->redirect('feedback/index');
213
            } else {
214
                // its a answer, lets remove it and redirect back in post
215
                $postId = $record->feedback_id;
216
                $record->delete();
217
                App::$Response->redirect('feedback/read/' . $postId);
218
            }
219
        }
220
221
        // render view
222
        return App::$View->render('delete', [
223
            'type' => $type,
224
            'record' => $record
225
        ]);
226
    }
227
228
    /**
229
     * Settings of feedback application
230
     * @return string
231
     * @throws \Ffcms\Core\Exception\SyntaxException
232
     */
233 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...
234
    {
235
        // initialize model and pass configs
236
        $model = new FormSettings($this->getConfigs());
237
238
        // check if form is submited to save data
239
        if ($model->send()) {
240
            // is validation passed?
241
            if ($model->validate()) {
242
                // save properties
243
                $this->setConfigs($model->getAllProperties());
244
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
245
                App::$Response->redirect('feedback/index');
246
            } else {
247
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
248
            }
249
        }
250
251
        // render view
252
        return App::$View->render('settings', [
253
            'model' => $model
254
        ]);
255
    }
256
257
}