Passed
Push — master ( 81f334...c86e8c )
by Mihail
03:35
created

ActionUpdate::update()   C

Complexity

Conditions 11
Paths 17

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 26
nc 17
nop 2
dl 0
loc 42
rs 5.2653
c 0
b 0
f 0

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\Feedback;
4
5
use Apps\ActiveRecord\FeedbackAnswer;
6
use Apps\ActiveRecord\FeedbackPost;
7
use Apps\Model\Admin\Feedback\FormUpdate;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\View;
10
use Ffcms\Core\Exception\NotFoundException;
11
use Ffcms\Core\Helper\Type\Any;
12
use Ffcms\Core\Network\Request;
13
use Ffcms\Core\Network\Response;
14
15
/**
16
 * Trait ActionUpdate
17
 * @package Apps\Controller\Admin\Feedback
18
 * @property Request $request
19
 * @property Response $response
20
 * @property View $view
21
 */
22
trait ActionUpdate
23
{
24
    /**
25
     * Edit feedback post or answer
26
     * @param string $type
27
     * @param string $id
28
     * @return string
29
     * @throws NotFoundException
30
     * @throws \Ffcms\Core\Exception\SyntaxException
31
     */
32
    public function update(string $type, string $id): ?string
33
    {
34
        if (!Any::isInt($id) || $id < 1) {
35
            throw new NotFoundException('Bad id format');
36
        }
37
        // get active record based on type (post or answer for post)
38
        $record = null;
39
        $postId = $id;
40
        switch ($type) {
41
            case 'post':
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
42
                $record = FeedbackPost::find($id);
43
                break;
44
            case 'answer':
45
                $record = FeedbackAnswer::find($id);
46
                if ($record !== null && $record !== false) {
47
                    $postId = (int)$record->getFeedbackPost()->id;
48
                }
49
                break;
50
        }
51
52
        // try what we got
53
        if ($record === null || $record === false) {
54
            throw new NotFoundException(__('Feedback item is not founded'));
55
        }
56
57
        // initialize model
58
        $model = new FormUpdate($record);
59
        if ($model->send()) {
60
            if ($model->validate()) {
61
                $model->make();
62
                App::$Session->getFlashBag()->add('success', __('Feedback item are successful changed'));
63
                $this->response->redirect('feedback/read/' . $postId);
64
            } else {
65
                App::$Session->getFlashBag()->add('danger', __('Updating is failed'));
66
            }
67
        }
68
69
        // render output view
70
        return $this->view->render('update', [
71
            'model' => $model
72
        ]);
73
    }
74
}
75