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\API; |
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
|
|
|
/** |
34
|
|
|
* @var string|null |
35
|
|
|
*/ |
36
|
|
|
private $authorizationAttribute; |
37
|
|
|
|
38
|
5 |
|
public function __construct( |
39
|
|
|
ControllerHelperInterface $controllerHelper, |
40
|
|
|
array $options |
41
|
|
|
) { |
42
|
5 |
|
Assert::keyExists($options, 'entity-class'); |
43
|
5 |
|
Assert::null($this->controllerHelper); |
44
|
|
|
|
45
|
5 |
|
$options = array_merge([ |
46
|
5 |
|
'authorization-attribute' => null, |
47
|
5 |
|
], $options); |
48
|
|
|
|
49
|
5 |
|
$this->controllerHelper = $controllerHelper; |
50
|
5 |
|
$this->entityClass = $options['entity-class']; |
51
|
5 |
|
$this->authorizationAttribute = $options['authorization-attribute']; |
52
|
5 |
|
} |
53
|
|
|
|
54
|
3 |
|
public function removeEntity(string $id): Response |
55
|
|
|
{ |
56
|
|
|
/** @var object|null $entity */ |
57
|
3 |
|
$entity = $this->controllerHelper->findEntity($this->entityClass, $id); |
58
|
|
|
|
59
|
3 |
|
if (is_null($entity)) { |
60
|
1 |
|
throw new InvalidArgumentException(sprintf( |
61
|
1 |
|
"Entity with id %s not found!", |
62
|
1 |
|
$id |
63
|
|
|
)); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
if (!empty($this->authorizationAttribute)) { |
67
|
1 |
|
$this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $entity); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
$this->controllerHelper->removeEntity($entity); |
71
|
|
|
|
72
|
1 |
|
return new Response("Entity removed!"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|