Passed
Push — main ( 4cfd43...9ceccf )
by Nobufumi
09:41
created

DeleteTrait::handleDelete()   A

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
        $formHtml = $this->formService->formHtml(
30
            "/{$this->adminDirName}/delete/{$id}",
31
            $fields,
32
            $this->csrfManager->getToken(),
33
            __(
34
                "x_delete_confirm",
35
                "Are you sure you want to delete this :name?",
36
                ['name' => __($this->label)]
37
            ),
38
            __("delete", "Delete"),
39
        );
40
        $formHtml = $this->formService->addMessages(
41
            $formHtml,
42
            $this->flashManager->getData('errors', [])
43
        );
44
45
        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

45
        return $this->/** @scrutinizer ignore-call */ renderResponse(
Loading history...
46
            $response,
47
            __(
48
                "x_delete",
49
                "Delete :name",
50
                ['name' => __($this->label)]
51
            ),
52
            $formHtml
53
        );
54
    }
55
56
    public function handleDelete(
57
        Request $request,
58
        Response $response,
59
        array $args
60
    ): Response {
61
        $id = $args['id'];
62
        $data = $request->getParsedBody() ?? [];
63
64
        $results = $this->model->validate(
65
            $data,
66
            ['id' => $id, 'context' => 'delete']
67
        );
68
69
        if (!$results['valid']) {
70
            $this->flashManager->addErrors($results['errors']);
71
            return  $this->redirectResponse(
72
                $request,
73
                $response,
74
                "/{$this->adminDirName}/index"
75
            );
76
        }
77
78
        // validate csrf token
79
        $redirectTo = "/{$this->adminDirName}/delete/{$id}";
80
        $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

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