1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Controller; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
7
|
|
|
use ScayTrase\Api\Cruds\Event\CollectionCrudEvent; |
8
|
|
|
use ScayTrase\Api\Cruds\Event\CrudEvents; |
9
|
|
|
use ScayTrase\Api\Cruds\Exception\EntityNotFoundException; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
12
|
|
|
|
13
|
|
|
final class DeleteController |
14
|
|
|
{ |
15
|
|
|
const ACTION = 'deleteAction'; |
16
|
|
|
|
17
|
|
|
/** @var ObjectRepository */ |
18
|
|
|
private $repository; |
19
|
|
|
/** @var ObjectManager */ |
20
|
|
|
private $manager; |
21
|
|
|
/** @var EventDispatcherInterface */ |
22
|
|
|
private $evm; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* DeleteController constructor. |
26
|
|
|
* |
27
|
|
|
* @param ObjectRepository $repository |
28
|
|
|
* @param ObjectManager $manager |
29
|
|
|
* @param EventDispatcherInterface $evm |
30
|
|
|
*/ |
31
|
4 |
|
public function __construct( |
32
|
|
|
ObjectRepository $repository, |
33
|
|
|
ObjectManager $manager, |
34
|
|
|
EventDispatcherInterface $evm = null |
35
|
|
|
) { |
36
|
4 |
|
$this->repository = $repository; |
37
|
4 |
|
$this->manager = $manager; |
38
|
4 |
|
$this->evm = $evm ?: new EventDispatcher(); |
39
|
4 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Removes the entity by given identifiers |
43
|
|
|
* |
44
|
|
|
* @param mixed $identifier |
45
|
|
|
* @throws EntityNotFoundException |
46
|
|
|
*/ |
47
|
4 |
|
public function deleteAction($identifier) |
48
|
|
|
{ |
49
|
4 |
|
$entity = $this->repository->find($identifier); |
50
|
|
|
|
51
|
4 |
|
if (!$entity) { |
52
|
|
|
throw EntityNotFoundException::byIdentifier($identifier); |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
$this->manager->remove($entity); |
56
|
|
|
|
57
|
4 |
|
$this->evm->dispatch(CrudEvents::DELETE, new CollectionCrudEvent([$entity])); |
58
|
4 |
|
$this->manager->flush(); |
59
|
4 |
|
} |
60
|
|
|
} |
61
|
|
|
|