DeleteController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 18.37 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 92.31%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 2
A deleteAction() 0 13 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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