Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

ActionTurn::turn()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 35
rs 8.6186
c 0
b 0
f 0
cc 7
nc 6
nop 2
1
<?php
2
3
namespace Apps\Controller\Admin\Feedback;
4
5
use Apps\ActiveRecord\FeedbackPost;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Exception\NotFoundException;
8
use Ffcms\Core\Helper\Type\Any;
9
use Ffcms\Core\Network\Request;
10
use Ffcms\Core\Network\Response;
11
12
/**
13
 * Trait ActionTurn
14
 * @package Apps\Controller\Admin\Feedback
15
 * @property Request $request
16
 * @property Response $response
17
 */
18
trait ActionTurn
19
{
20
    /**
21
     * Turn feedback request post - close, open, readed
22
     * @param string $direction
23
     * @param string $id
24
     * @return void
25
     * @throws NotFoundException
26
     */
27
    public function turn(string $direction, string $id): void
28
    {
29
        if (!Any::isInt($id) || $id < 1) {
30
            throw new NotFoundException('Bad is format');
31
        }
32
        // try to find record
33
        $record = FeedbackPost::find($id);
34
        if (!$record) {
35
            throw new NotFoundException(__('Feedback request with id %id% is not found', ['id' => $id]));
36
        }
37
38
        // switch operation direction to what we must change
39
        switch ($direction) {
40
            case 'open':
41
                $record->closed = 0;
0 ignored issues
show
Documentation Bug introduced by zenn
The property $closed was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
42
                $record->save();
43
                break;
44
            case 'close':
45
                $record->closed = 1;
46
                $record->save();
47
                break;
48
            case 'read':
49
                $record->readed = 1;
0 ignored issues
show
Documentation Bug introduced by zenn
The property $readed was declared of type boolean, but 1 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
50
                $record->save();
51
                break;
52
            default:
53
                throw new NotFoundException(__('Hack attention'));
54
                break;
55
        }
56
57
        // add notification of successful changes
58
        App::$Session->getFlashBag()->add('success', __('Feedback request is changed!'));
59
60
        // redirect to feedback post read
61
        $this->response->redirect('feedback/read/' . $id);
62
    }
63
}
64