Passed
Push — master ( 02a4a5...734a7e )
by Mihail
05:36
created

Feedback   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 251
Duplicated Lines 24.3 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 61
loc 251
rs 9.6
wmc 32
lcom 1
cbo 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
B actionRead() 3 28 6
B actionTurn() 3 34 6
B actionIndex() 26 26 1
D actionUpdate() 3 39 9
C actionDelete() 3 41 7
A actionSettings() 23 23 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
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\NativeException
31
     * @throws \Ffcms\Core\Exception\SyntaxException
32
     */
33 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...
34
    {
35
        // set current page and offset
36
        $page = (int)$this->request->query->get('page');
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Apps\Controller\Admin\Feedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
37
        $offset = $page * self::ITEM_PER_PAGE;
38
39
        // get feedback posts AR table
40
        $query = new FeedbackPost();
41
42
        // build pagination
43
        $pagination = new SimplePagination([
44
            'url' => ['feedback/index'],
45
            'page' => $page,
46
            'step' => self::ITEM_PER_PAGE,
47
            'total' => $query->count()
48
        ]);
49
50
        // build listing objects
51
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
52
53
        // render output
54
        return $this->view->render('index', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\Feedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
            'records' => $records,
56
            'pagination' => $pagination
57
        ]);
58
    }
59
60
    /**
61
     * Read feedback post and answer and add answer to thread post
62
     * @param int $id
63
     * @return string
64
     * @throws \Ffcms\Core\Exception\NativeException
65
     * @throws NotFoundException
66
     * @throws \Ffcms\Core\Exception\SyntaxException
67
     */
68
    public function actionRead($id)
69
    {
70
        // find feedback post by id
71
        $record = FeedbackPost::find($id);
72 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...
73
            throw new NotFoundException(__('The feedback message is not founded'));
74
        }
75
76
        // initialize model with answer add if thread is not closed
77
        $model = null;
78
        if ((int)$record->closed !== 1) {
79
            $model = new FormAnswerAdd($record, App::$User->identity()->getId());
80
            if ($model->send()) {
81
                if ($model->validate()) {
82
                    $model->make();
83
                    App::$Session->getFlashBag()->add('success', __('New answer is successful add'));
84
                } else {
85
                    App::$Session->getFlashBag()->add('error', 'Validation failure');
86
                }
87
            }
88
        }
89
90
        // render view
91
        return $this->view->render('read', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\Feedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92
            'record' => $record,
93
            'model' => $model
94
        ]);
95
    }
96
97
    /**
98
     * Edit feedback post or answer
99
     * @param string $type
100
     * @param int $id
101
     * @return string
102
     * @throws \Ffcms\Core\Exception\NativeException
103
     * @throws NotFoundException
104
     * @throws \Ffcms\Core\Exception\SyntaxException
105
     */
106
    public function actionUpdate($type, $id)
107
    {
108
        // get active record based on type (post or answer for post)
109
        $record = null;
110
        $postId = $id;
111
        switch ($type) {
112
            case 'post':
113
                $record = FeedbackPost::find($id);
114
                break;
115
            case 'answer':
116
                $record = FeedbackAnswer::find($id);
117
                if ($record !== null && $record !== false) {
118
                    $postId = (int)$record->getFeedbackPost()->id;
119
                }
120
                break;
121
        }
122
123
        // try what we got
124 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...
125
            throw new NotFoundException(__('Feedback item is not founded'));
126
        }
127
128
        // initialize model
129
        $model = new FormUpdate($record);
130
        if ($model->send()) {
131
            if ($model->validate()) {
132
                $model->make();
133
                App::$Session->getFlashBag()->add('success', __('Feedback item are successful changed'));
134
                $this->response->redirect('feedback/read/' . $postId);
0 ignored issues
show
Bug introduced by
The method redirect cannot be called on $this->response (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
135
            } else {
136
                App::$Session->getFlashBag()->add('danger', __('Updating is failed'));
137
            }
138
        }
139
140
        // render output view
141
        return $this->view->render('update', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\Feedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
142
            'model' => $model->filter()
143
        ]);
144
    }
145
146
    /**
147
     * Turn feedback request post - close, open, readed
148
     * @param string $direction
149
     * @param int $id
150
     * @return null
151
     * @throws NotFoundException
152
     */
153
    public function actionTurn($direction, $id)
154
    {
155
        // try to find record
156
        $record = FeedbackPost::find($id);
157 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...
158
            throw new NotFoundException(__('Feedback request with id %id% is not found', ['id' => $id]));
159
        }
160
161
        // switch operation direction to what we must change
162
        switch ($direction) {
163
            case 'open':
164
                $record->closed = 0;
165
                $record->save();
166
                break;
167
            case 'close':
168
                $record->closed = 1;
169
                $record->save();
170
                break;
171
            case 'read':
172
                $record->readed = 1;
173
                $record->save();
174
                break;
175
            default:
176
                throw new NotFoundException(__('Hack attention'));
177
                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...
178
        }
179
180
        // add notification of successful changes
181
        App::$Session->getFlashBag()->add('success', __('Feedback request is changed!'));
182
183
        // redirect to feedback post read
184
        $this->response->redirect('feedback/read/' . $id);
0 ignored issues
show
Bug introduced by
The method redirect cannot be called on $this->response (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
185
        return null;
186
    }
187
188
    /**
189
     * Delete feedback post or answer
190
     * @param string $type
191
     * @param int $id
192
     * @return string
193
     * @throws \Ffcms\Core\Exception\NativeException
194
     * @throws NotFoundException
195
     * @throws \Exception
196
     * @throws \Ffcms\Core\Exception\SyntaxException
197
     */
198
    public function actionDelete($type, $id)
199
    {
200
        // try to get active record by type
201
        $record = null;
202
        switch ($type) {
203
            case 'post':
204
                $record = FeedbackPost::find($id);
205
                break;
206
            case 'answer':
207
                $record = FeedbackAnswer::find($id);
208
                break;
209
        }
210
211
        // check if we get the row
212 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...
213
            throw new NotFoundException(__('Feedback item is not founded'));
214
        }
215
216
        // if delete is submited
217
        if ($this->request->request->get('deleteFeedback')) {
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Apps\Controller\Admin\Feedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
218
            // remove all answers
219
            if ($type === 'post') {
220
                FeedbackAnswer::where('feedback_id', '=', $record->id)->delete();
221
                // remove item
222
                $record->delete();
223
                App::$Session->getFlashBag()->add('success', __('Feedback record is successful removed'));
224
                $this->response->redirect('feedback/index');
0 ignored issues
show
Bug introduced by
The method redirect cannot be called on $this->response (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
225
            } else {
226
                // its a answer, lets remove it and redirect back in post
227
                $postId = $record->feedback_id;
228
                $record->delete();
229
                $this->response->redirect('feedback/read/' . $postId);
0 ignored issues
show
Bug introduced by
The method redirect cannot be called on $this->response (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
230
            }
231
        }
232
233
        // render view
234
        return $this->view->render('delete', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\Feedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
235
            'type' => $type,
236
            'record' => $record
237
        ]);
238
    }
239
240
    /**
241
     * Settings of feedback application
242
     * @return string
243
     * @throws \Ffcms\Core\Exception\NativeException
244
     * @throws \Ffcms\Core\Exception\SyntaxException
245
     */
246 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...
247
    {
248
        // initialize model and pass configs
249
        $model = new FormSettings($this->getConfigs());
250
251
        // check if form is submited to save data
252
        if ($model->send()) {
253
            // is validation passed?
254
            if ($model->validate()) {
255
                // save properties
256
                $this->setConfigs($model->getAllProperties());
257
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
258
                $this->response->redirect('feedback/index');
0 ignored issues
show
Bug introduced by
The method redirect cannot be called on $this->response (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
259
            } else {
260
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
261
            }
262
        }
263
264
        // render view
265
        return $this->view->render('settings', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\Feedback>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
266
            'model' => $model->filter()
267
        ]);
268
    }
269
270
}