DeleteTrait::handleDelete()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 58
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 35
c 0
b 0
f 0
nc 7
nop 3
dl 0
loc 58
rs 9.0488

How to fix   Long Method   

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 Jidaikobo\Kontiki\Controllers\Traits;
4
5
use Jidaikobo\Kontiki\Services\FormService;
6
use Psr\Http\Message\ResponseInterface as Response;
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
9
trait DeleteTrait
10
{
11
    public function delete(
12
        Request $request,
13
        Response $response,
14
        array $args
15
    ): Response {
16
        $id = $args['id'];
17
        $data = $this->model->getById($id);
18
19
        if (!$data) {
20
            return $this->redirectResponse(
0 ignored issues
show
Bug introduced by
It seems like redirectResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
            return $this->/** @scrutinizer ignore-call */ redirectResponse(
Loading history...
21
                $request,
22
                $response,
23
                "{$this->label}_index"
24
            );
25
        }
26
27
        $fields = $this->model->getFields('delete', $data);
28
29
        $formVars = [
30
            'description' => __(
31
                "x_delete_confirm",
32
                "Are you sure you want to delete this :name?",
33
                ['name' => __($this->label)]
34
            ),
35
            'buttonClass' => 'btn-danger',
36
            'buttonID' => 'mainDeleteBtn',
37
            'buttonText' => __("delete", "Delete"),
38
            'data' => $data
39
        ];
40
41
        $formHtml = $this->formService->formHtml(
42
            "/{$this->adminDirName}/delete/{$id}",
43
            $fields,
44
            $this->csrfManager->getToken(),
45
            $formVars
46
        );
47
        $formHtml = $this->formService->addMessages(
48
            $formHtml,
49
            $this->flashManager->getData('errors', [])
50
        );
51
52
        return $this->renderResponse(
0 ignored issues
show
Bug introduced by
It seems like renderResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        return $this->/** @scrutinizer ignore-call */ renderResponse(
Loading history...
53
            $response,
54
            __(
55
                "x_delete",
56
                "Delete :name",
57
                ['name' => __($this->label)]
58
            ),
59
            $formHtml
60
        );
61
    }
62
63
    public function handleDelete(
64
        Request $request,
65
        Response $response,
66
        array $args
67
    ): Response {
68
        $id = $args['id'];
69
        $data = $request->getParsedBody() ?? [];
70
71
        $results = $this->model->validate(
72
            $data,
73
            ['id' => $id, 'context' => 'delete']
74
        );
75
76
        if (!$results['valid']) {
77
            $this->flashManager->addErrors($results['errors']);
78
            return  $this->redirectResponse(
79
                $request,
80
                $response,
81
                "/{$this->adminDirName}/index"
82
            );
83
        }
84
85
        // validate csrf token
86
        $redirectTo = "/{$this->adminDirName}/delete/{$id}";
87
        $redirectResponse = $this->validateCsrfToken($data, $request, $response, $redirectTo);
0 ignored issues
show
Bug introduced by
It seems like validateCsrfToken() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        /** @scrutinizer ignore-call */ 
88
        $redirectResponse = $this->validateCsrfToken($data, $request, $response, $redirectTo);
Loading history...
88
        if ($redirectResponse) {
89
            return $redirectResponse;
90
        }
91
92
        // delete data
93
        try {
94
            if ($this->model->delete($id)) {
95
                $this->flashManager->addMessage(
96
                    'success',
97
                    __(
98
                        "x_delete_success",
99
                        ":name deleted successfully.",
100
                        ['name' => __($this->label)]
101
                    )
102
                );
103
                return $this->redirectResponse(
104
                    $request,
105
                    $response,
106
                    "/{$this->adminDirName}/index"
107
                );
108
            }
109
        } catch (\Exception $e) {
110
            $this->flashManager->addErrors([
111
                __(
112
                    "x_delete_failed",
113
                    "Failed to delete :name",
114
                    ['name' => __($this->label)]
115
                )
116
              ]);
117
        }
118
119
        $redirectTo = "/{$this->adminDirName}/edit/{$id}";
120
        return $this->redirectResponse($request, $response, $redirectTo);
121
    }
122
}
123