Test Setup Failed
Push — master ( e2ccdd...da0315 )
by Gerrit
09:14
created

GenericEntityRemoveController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 34.94 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 29
loc 83
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

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
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 *
10
 * @author Gerrit Addiks <[email protected]>
11
 */
12
13
namespace Addiks\SymfonyGenerics\Controllers\API;
14
15
use Addiks\SymfonyGenerics\Controllers\ControllerHelperInterface;
16
use Webmozart\Assert\Assert;
17
use Symfony\Component\HttpFoundation\Response;
18
use InvalidArgumentException;
19
use Addiks\SymfonyGenerics\Events\EntityInteractionEvent;
20
use Symfony\Component\HttpFoundation\Request;
21
22
final class GenericEntityRemoveController
23
{
24
25
    private ControllerHelperInterface $controllerHelper;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
26
27
    /** @var class-string */
28
    private string $entityClass;
29
30
    private string $entityIdKey;
31
32
    private ?string $authorizationAttribute;
33
34
    public function __construct(
35
        ControllerHelperInterface $controllerHelper,
36
        array $options
37
    ) {
38
        Assert::keyExists($options, 'entity-class');
39
40
        $options = array_merge([
41
            'authorization-attribute' => null,
42
            'entity-id-key' => 'entityId',
43
        ], $options);
44
45 7
        $this->controllerHelper = $controllerHelper;
46
        $this->entityClass = $options['entity-class'];
47
        $this->entityIdKey = $options['entity-id-key'];
48
        $this->authorizationAttribute = $options['authorization-attribute'];
49 7
    }
50 7
51
    public function __invoke(): Response
52 7
    {
53 7
        /** @var Request $request */
54
        $request = $this->controllerHelper->getCurrentRequest();
55 7
56
        Assert::isInstanceOf($request, Request::class, "Cannot use controller outside of request-scope!");
57 7
58 7
        /** @var string $entityId */
59 7
        $entityId = $request->get($this->entityIdKey);
60 7
61 7
        return $this->removeEntity($entityId);
62
    }
63 2
64
    public function removeEntity(string $entityId): Response
65
    {
66 2
        /** @var object|null $entity */
67
        $entity = $this->controllerHelper->findEntity($this->entityClass, $entityId);
68 2
69
        if (is_null($entity)) {
70
            throw new InvalidArgumentException(sprintf(
71 1
                "Entity with id %s not found!",
72
                $entityId
73 1
            ));
74
        }
75
76 4
        if (!empty($this->authorizationAttribute)) {
77
            $this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $entity);
78
        }
79 4
80
        $this->controllerHelper->dispatchEvent("symfony_generics.entity_interaction", new EntityInteractionEvent(
81 4
            $this->entityClass,
82 1
            $entityId,
83 1
            $entity,
84 1
            "__destruct"
85
        ));
86
87
        $this->controllerHelper->removeEntity($entity);
88 3
89 1
        return new Response("Entity removed!");
90
    }
91
92
}
93