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
|
6 |
View Code Duplication |
public function __construct( |
|
|
|
|
32
|
|
|
ObjectRepository $repository, |
33
|
|
|
ObjectManager $manager, |
34
|
|
|
EventDispatcherInterface $evm = null |
35
|
|
|
) { |
36
|
6 |
|
$this->repository = $repository; |
37
|
6 |
|
$this->manager = $manager; |
38
|
6 |
|
$this->evm = $evm ?: new EventDispatcher(); |
39
|
6 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Removes the entity by given identifiers |
43
|
|
|
* |
44
|
|
|
* @param mixed $identifier |
45
|
|
|
* |
46
|
|
|
* @throws EntityNotFoundException |
47
|
|
|
*/ |
48
|
6 |
|
public function deleteAction($identifier) |
49
|
|
|
{ |
50
|
6 |
|
$entity = $this->repository->find($identifier); |
51
|
|
|
|
52
|
6 |
|
if (!$entity) { |
53
|
|
|
throw EntityNotFoundException::byIdentifier($identifier); |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
$this->manager->remove($entity); |
57
|
|
|
|
58
|
6 |
|
$this->evm->dispatch(CrudEvents::DELETE, new CollectionCrudEvent([$entity])); |
59
|
6 |
|
$this->manager->flush(); |
60
|
6 |
|
} |
61
|
|
|
} |
62
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.