DeleteController::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
crap 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 6 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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