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

ActionDelete   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
D delete() 0 44 9
1
<?php
2
3
namespace Apps\Controller\Admin\Feedback;
4
5
use Apps\ActiveRecord\FeedbackAnswer;
6
use Apps\ActiveRecord\FeedbackPost;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\NotFoundException;
10
use Ffcms\Core\Helper\Type\Any;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
14
/**
15
 * Trait ActionDelete
16
 * @package Apps\Controller\Admin\Feedback
17
 * @property Request $request
18
 * @property Response $response
19
 * @property View $view
20
 */
21
trait ActionDelete
22
{
23
    /**
24
     * Delete feedback post or answer
25
     * @param string $type
26
     * @param string $id
27
     * @return string
28
     * @throws \Exception
29
     */
30
    public function delete(string $type, string $id): ?string
31
    {
32
        if (!Any::isInt($id) || $id < 1) {
33
            throw new NotFoundException('Bad id format');
34
        }
35
        // try to get active record by type
36
        $record = null;
37
        switch ($type) {
38
            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...
39
                $record = FeedbackPost::find($id);
40
                break;
41
            case 'answer':
42
                $record = FeedbackAnswer::find($id);
43
                break;
44
        }
45
46
        // check if we get the row
47
        if ($record === null || $record === false) {
48
            throw new NotFoundException(__('Feedback item is not founded'));
49
        }
50
51
        // if delete is submited
52
        if ($this->request->request->get('deleteFeedback')) {
53
            // remove all answers
54
            if ($type === 'post') {
55
                FeedbackAnswer::where('feedback_id', '=', $record->id)->delete();
56
                // remove item
57
                $record->delete();
58
                App::$Session->getFlashBag()->add('success', __('Feedback record is successful removed'));
59
                $this->response->redirect('feedback/index');
60
            } else {
61
                // its a answer, lets remove it and redirect back in post
62
                $postId = $record->feedback_id;
63
                $record->delete();
64
                $this->response->redirect('feedback/read/' . $postId);
65
            }
66
        }
67
68
        // render view
69
        return $this->view->render('delete', [
70
            'type' => $type,
71
            'record' => $record
72
        ]);
73
    }
74
}
75