Completed
Push — master ( 670f0c...07e238 )
by Pavel
12:30
created

DeleteController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 92.31%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 48
ccs 12
cts 13
cp 0.9231
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A deleteAction() 0 13 2
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