1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2018 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* |
8
|
|
|
* @license GPL-3.0 |
9
|
|
|
* |
10
|
|
|
* @author Gerrit Addiks <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Addiks\SymfonyGenerics\Controllers; |
14
|
|
|
|
15
|
|
|
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface; |
16
|
|
|
use Webmozart\Assert\Assert; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
final class GenericEntityRemoveController |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var ControllerHelperInterface |
25
|
|
|
*/ |
26
|
|
|
private $controllerHelper; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $entityClass; |
32
|
|
|
|
33
|
4 |
|
public function __construct( |
34
|
|
|
ControllerHelperInterface $controllerHelper, |
35
|
|
|
array $options |
36
|
|
|
) { |
37
|
4 |
|
Assert::keyExists($options, 'entity-class'); |
38
|
4 |
|
Assert::null($this->controllerHelper); |
39
|
|
|
|
40
|
4 |
|
$this->controllerHelper = $controllerHelper; |
41
|
4 |
|
$this->entityClass = $options['entity-class']; |
42
|
4 |
|
} |
43
|
|
|
|
44
|
2 |
|
public function removeEntity(string $id): Response |
45
|
|
|
{ |
46
|
|
|
/** @var object|null $entity */ |
47
|
2 |
|
$entity = $this->controllerHelper->findEntity($this->entityClass, $id); |
48
|
|
|
|
49
|
2 |
|
if (is_null($entity)) { |
50
|
1 |
|
throw new InvalidArgumentException(sprintf( |
51
|
1 |
|
"Entity with id %s not found!", |
52
|
1 |
|
$id |
53
|
|
|
)); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$this->controllerHelper->removeEntity($entity); |
57
|
|
|
|
58
|
1 |
|
return new Response("Entity removed!"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|