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

ActionUpdate   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 8

1 Method

Rating   Name   Duplication   Size   Complexity  
C update() 0 42 11
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