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

ActionEdit::edit()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 10
nop 2
dl 0
loc 36
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace Apps\Controller\Admin\Comments;
4
5
use Apps\ActiveRecord\CommentAnswer;
6
use Apps\ActiveRecord\CommentPost;
7
use Apps\Model\Admin\Comments\FormCommentUpdate;
8
use Ffcms\Core\App;
9
use Ffcms\Core\Arch\View;
10
use Ffcms\Core\Exception\NotFoundException;
11
use Ffcms\Core\Exception\SyntaxException;
12
use Ffcms\Core\Helper\Type\Any;
13
use Ffcms\Core\Network\Request;
14
use Ffcms\Core\Network\Response;
15
16
/**
17
 * Trait ActionEdit
18
 * @package Apps\Controller\Admin\Comments
19
 * @property Request $request
20
 * @property Response $response
21
 * @property View $view
22
 */
23
trait ActionEdit
24
{
25
    /**
26
     * Commentaries and answers edit action
27
     * @param string $type
28
     * @param string $id
29
     * @throws NotFoundException
30
     * @return string
31
     * @throws \Ffcms\Core\Exception\SyntaxException
32
     */
33
    public function edit(string $type, string $id): ?string
34
    {
35
        if (!Any::isInt($id) || $id < 1) {
36
            throw new SyntaxException('Bad arguments');
37
        }
38
39
        // get active record by type and id from active records
40
        $record = null;
41
        switch ($type) {
42
            case static::TYPE_COMMENT:
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...
43
                $record = CommentPost::find($id);
44
                break;
45
            case static::TYPE_ANSWER:
46
                $record = CommentAnswer::find($id);
47
                break;
48
        }
49
50
        // check if response is not empty
51
        if (!$record) {
52
            throw new NotFoundException(__('Comment is not founded'));
53
        }
54
55
        // init edit model
56
        $model = new FormCommentUpdate($record, $type);
57
58
        // check if data is submited and validated
59
        if ($model->send() && $model->validate()) {
60
            $model->make();
61
            App::$Session->getFlashBag()->add('success', __('Comment or answer is successful updated'));
62
        }
63
64
        // render view
65
        return $this->view->render('edit', [
66
            'model' => $model
67
        ]);
68
    }
69
}
70