TrashRestoreTrait::restore()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 3
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 TrashRestoreTrait
10
{
11
    public function trashIndex(Request $request, Response $response): Response
12
    {
13
        return $this->index($request, $response, 'trash');
0 ignored issues
show
Bug introduced by
It seems like index() 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

13
        return $this->/** @scrutinizer ignore-call */ index($request, $response, 'trash');
Loading history...
14
    }
15
16
    public function trash(Request $request, Response $response, array $args): Response
17
    {
18
        $id = $args['id'];
19
        return static::confirmTrashRestore($request, $response, $id, 'trash');
0 ignored issues
show
Bug Best Practice introduced by
The method Jidaikobo\Kontiki\Contro...::confirmTrashRestore() is not static, but was called statically. ( Ignorable by Annotation )

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

19
        return static::/** @scrutinizer ignore-call */ confirmTrashRestore($request, $response, $id, 'trash');
Loading history...
20
    }
21
22
    public function restore(Request $request, Response $response, array $args): Response
23
    {
24
        $id = $args['id'];
25
        return static::confirmTrashRestore($request, $response, $id, 'restore');
0 ignored issues
show
Bug Best Practice introduced by
The method Jidaikobo\Kontiki\Contro...::confirmTrashRestore() is not static, but was called statically. ( Ignorable by Annotation )

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

25
        return static::/** @scrutinizer ignore-call */ confirmTrashRestore($request, $response, $id, 'restore');
Loading history...
26
    }
27
28
    public function confirmTrashRestore(
29
        Request $request,
30
        Response $response,
31
        int $id,
32
        string $actionType
33
    ): Response {
34
        $data = $this->model->getById($id);
35
36
        if (!$data) {
37
            return $this->redirectResponse($request, $response, "{$this->label}_index");
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

37
            return $this->/** @scrutinizer ignore-call */ redirectResponse($request, $response, "{$this->label}_index");
Loading history...
38
        }
39
40
        $fields = $this->model->getFields($actionType, $data);
41
42
        $buttonText = $actionType == 'trash' ? 'to_trash' : $actionType;
43
        $buttonClass = $actionType == 'trash' ? 'btn-danger' : 'btn-success';
44
45
        $formVars = [
46
            'description' => __(
47
                "x_{$actionType}_confirm",
48
                "Are you sure you want to {$actionType} this :name?",
49
                ['name' => __($this->label)]
50
            ),
51
            'buttonID' => "main{$actionType}Btn",
52
            'buttonClass' => $buttonClass,
53
            'buttonText' => __($buttonText),
54
            'data' => $data
55
        ];
56
57
        $formHtml = $this->formService->formHtml(
58
            "/{$this->adminDirName}/{$actionType}/{$id}",
59
            $fields,
60
            $this->csrfManager->getToken(),
61
            $formVars
62
        );
63
        $formHtml = $this->formService->addMessages(
64
            $formHtml,
65
            $this->flashManager->getData('errors', [])
66
        );
67
68
        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

68
        return $this->/** @scrutinizer ignore-call */ renderResponse(
Loading history...
69
            $response,
70
            __(
71
                "x_{$actionType}",
72
                "{$actionType} :name",
73
                ['name' => __($this->label)]
74
            ),
75
            $formHtml
76
        );
77
    }
78
79
    public function handleTrash(Request $request, Response $response, array $args): Response
80
    {
81
        $id = $args['id'];
82
        return static::executeTrashRestore($request, $response, $id, 'trash');
0 ignored issues
show
Bug Best Practice introduced by
The method Jidaikobo\Kontiki\Contro...::executeTrashRestore() is not static, but was called statically. ( Ignorable by Annotation )

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

82
        return static::/** @scrutinizer ignore-call */ executeTrashRestore($request, $response, $id, 'trash');
Loading history...
83
    }
84
85
    public function handleRestore(Request $request, Response $response, array $args): Response
86
    {
87
        $id = $args['id'];
88
        return static::executeTrashRestore($request, $response, $id, 'restore');
0 ignored issues
show
Bug Best Practice introduced by
The method Jidaikobo\Kontiki\Contro...::executeTrashRestore() is not static, but was called statically. ( Ignorable by Annotation )

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

88
        return static::/** @scrutinizer ignore-call */ executeTrashRestore($request, $response, $id, 'restore');
Loading history...
89
    }
90
91
    public function executeTrashRestore(Request $request, Response $response, int $id, string $actionType): Response
92
    {
93
        $data = $request->getParsedBody() ?? [];
94
95
        // validate csrf token
96
        $redirectTo = "/{$this->adminDirName}/{$actionType}/{$id}";
97
        $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

97
        /** @scrutinizer ignore-call */ 
98
        $redirectResponse = $this->validateCsrfToken($data, $request, $response, $redirectTo);
Loading history...
98
        if ($redirectResponse) {
99
            return $redirectResponse;
100
        }
101
102
        // execute data
103
        try {
104
            if ($this->model->$actionType($id)) {
105
                $this->flashManager->addMessage(
106
                    'success',
107
                    __(
108
                        "x_{$actionType}_success",
109
                        ":name {$actionType} successfully.",
110
                        ['name' => __($this->label)]
111
                    )
112
                );
113
                return $this->redirectResponse(
114
                    $request,
115
                    $response,
116
                    "/{$this->adminDirName}/index"
117
                );
118
            }
119
        } catch (\Exception $e) {
120
            $this->flashManager->addErrors([
121
                __(
122
                    "x_{$actionType}_failed",
123
                    "Failed to {$actionType} :name",
124
                    ['name' => __($this->label)]
125
                )
126
              ]);
127
        }
128
129
        $redirectTo = "/{$this->adminDirName}/index";
130
        return $this->redirectResponse($request, $response, $redirectTo);
131
    }
132
}
133