Test Failed
Push — develop ( 8b5bdd...c753fd )
by nguereza
02:34
created

BatchAction::enableHandle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Framework
5
 *
6
 * Platine Framework is a lightweight, high-performance, simple and elegant
7
 * PHP Web framework
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Framework
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file BatchAction.php
34
 *
35
 *  The Batch (delete, etc.) action class
36
 *
37
 *  @package    Platine\Framework\Demo\Action\User
38
 *  @author Platine Developers team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   http://www.iacademy.cf
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Framework\Demo\Action\User;
49
50
use Platine\Framework\Auth\Repository\UserRepository;
51
use Platine\Framework\Http\RequestData;
52
use Platine\Framework\Http\Response\RedirectResponse;
53
use Platine\Framework\Http\RouteHelper;
54
use Platine\Http\Handler\RequestHandlerInterface;
55
use Platine\Http\ResponseInterface;
56
use Platine\Http\ServerRequestInterface;
57
use Platine\Lang\Lang;
58
use Platine\Logger\LoggerInterface;
59
use Platine\Session\Session;
60
use Platine\Template\Template;
61
62
/**
63
 * @class BatchAction
64
 * @package Platine\Framework\Demo\Action\User
65
 * @template T
66
 */
67
class BatchAction implements RequestHandlerInterface
68
{
69
70
    /**
71
     * Logger instance
72
     * @var LoggerInterface
73
     */
74
    protected LoggerInterface $logger;
75
76
    /**
77
     * The user repository
78
     * @var UserRepository
79
     */
80
    protected UserRepository $userRepository;
81
82
    /**
83
     * The template instance
84
     * @var Template
85
     */
86
    protected Template $template;
87
88
    /**
89
     * The route helper instance
90
     * @var RouteHelper
91
     */
92
    protected RouteHelper $routeHelper;
93
94
    /**
95
     * The session instance
96
     * @var Session
97
     */
98
    protected Session $session;
99
100
    /**
101
     * The translator instance
102
     * @var Lang
103
     */
104
    protected Lang $lang;
105
106
    /**
107
     * The server request instance
108
     * @var ServerRequestInterface
109
     */
110
    protected ServerRequestInterface $request;
111
112
    /**
113
     * The items to handle batch actions
114
     * @var array<mixed>
115
     */
116
    protected array $items = [];
117
118
    /**
119
     * Create new instance
120
     * @param Lang $lang
121
     * @param Session $session
122
     * @param LoggerInterface $logger
123
     * @param Template $template
124
     * @param UserRepository $userRepository
125
     * @param RouteHelper $routeHelper
126
     */
127
    public function __construct(
128
        Lang $lang,
129
        Session $session,
130
        LoggerInterface $logger,
131
        Template $template,
132
        UserRepository $userRepository,
133
        RouteHelper $routeHelper
134
    ) {
135
        $this->lang = $lang;
136
        $this->logger = $logger;
137
        $this->session = $session;
138
        $this->userRepository = $userRepository;
139
        $this->template = $template;
140
        $this->routeHelper = $routeHelper;
141
    }
142
143
    /**
144
     * {@inheritodc}
145
     */
146
    public function handle(ServerRequestInterface $request): ResponseInterface
147
    {
148
        $param = new RequestData($request);
149
150
        $items =  $param->post('items', []);
151
        if (empty($items)) {
152
            return new RedirectResponse(
153
                $this->routeHelper->generateUrl('user_list')
154
            );
155
        }
156
157
        $this->request = $request;
158
        $this->items = $items;
159
160
        $actions = [
161
            'delete',
162
            'disable',
163
            'enable',
164
        ];
165
166
        foreach ($actions as $action) {
167
            if ($param->post($action)) {
168
                $method = $action . 'Handle';
169
170
                $this->{$method}();
171
                break;
172
            }
173
        }
174
175
        return new RedirectResponse(
176
            $this->routeHelper->generateUrl('user_list')
177
        );
178
    }
179
180
    /**
181
     * Handle delete action
182
     * @return void
183
     */
184
    protected function deleteHandle(): void
185
    {
186
        $items = $this->items;
187
        $this->logger->info('Deleted of user #{items}', ['items' => $items]);
188
189
        $this->userRepository->query()
190
                            ->where('id')
191
                            ->in($items)
192
                            ->delete();
0 ignored issues
show
Bug introduced by
The method delete() does not exist on Platine\Database\Query\WhereStatement. It seems like you code against a sub-type of Platine\Database\Query\WhereStatement such as Platine\Database\Query\DeleteStatement or Platine\Database\Query\Query or Platine\Orm\Query\EntityQuery. ( Ignorable by Annotation )

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

192
                            ->/** @scrutinizer ignore-call */ delete();
Loading history...
193
194
        $this->session->setFlash('success', 'The selected users are deleted successfully');
195
    }
196
197
    /**
198
     * Handle disable action
199
     * @return void
200
     */
201
    protected function disableHandle(): void
202
    {
203
        $items = $this->items;
204
        $this->logger->info('Disable of user #{items}', ['items' => $items]);
205
206
        $this->userRepository->query()
207
                            ->where('id')
208
                            ->in($items)
209
                            ->update(['status' => false]);
0 ignored issues
show
Bug introduced by
The method update() does not exist on Platine\Database\Query\WhereStatement. It seems like you code against a sub-type of Platine\Database\Query\WhereStatement such as Platine\Orm\Query\EntityQuery. ( Ignorable by Annotation )

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

209
                            ->/** @scrutinizer ignore-call */ update(['status' => false]);
Loading history...
210
211
        $this->session->setFlash('success', 'The selected users are disabled successfully');
212
    }
213
214
    /**
215
     * Handle enable action
216
     * @return void
217
     */
218
    protected function enableHandle(): void
219
    {
220
        $items = $this->items;
221
        $this->logger->info('Enable of user #{items}', ['items' => $items]);
222
223
        $this->userRepository->query()
224
                            ->where('id')
225
                            ->in($items)
226
                            ->update(['status' => true]);
227
228
        $this->session->setFlash('success', 'The selected users are enabled successfully');
229
    }
230
}
231