Completed
Push — master ( 27d676...dee7dc )
by Gerrit
03:35
created

GenericEntityRemoveController::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 5
cts 5
cp 1
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
    /**
26
     * @var ControllerHelperInterface
27
     */
28
    private $controllerHelper;
29
30
    /**
31
     * @var string
32
     */
33
    private $entityClass;
34
35
    /**
36
     * @var string
37
     */
38
    private $entityIdKey;
39
40
    /**
41
     * @var string|null
42
     */
43
    private $authorizationAttribute;
44
45 7 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...
46
        ControllerHelperInterface $controllerHelper,
47
        array $options
48
    ) {
49 7
        Assert::keyExists($options, 'entity-class');
50 7
        Assert::null($this->controllerHelper);
51
52 7
        $options = array_merge([
53 7
            'authorization-attribute' => null,
54
            'entity-id-key' => 'entityId',
55 7
        ], $options);
56
57 7
        $this->controllerHelper = $controllerHelper;
58 7
        $this->entityClass = $options['entity-class'];
59 7
        $this->entityIdKey = $options['entity-id-key'];
60 7
        $this->authorizationAttribute = $options['authorization-attribute'];
61 7
    }
62
63 2 View Code Duplication
    public function __invoke(): Response
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...
64
    {
65
        /** @var Request $request */
66 2
        $request = $this->controllerHelper->getCurrentRequest();
67
68 2
        Assert::isInstanceOf($request, Request::class, "Cannot use controller outside of request-scope!");
69
70
        /** @var string $entityId */
71 1
        $entityId = (string)$request->get($this->entityIdKey);
72
73 1
        return $this->removeEntity($entityId);
74
    }
75
76 4
    public function removeEntity(string $entityId): Response
77
    {
78
        /** @var object|null $entity */
79 4
        $entity = $this->controllerHelper->findEntity($this->entityClass, $entityId);
80
81 4
        if (is_null($entity)) {
82 1
            throw new InvalidArgumentException(sprintf(
83 1
                "Entity with id %s not found!",
84 1
                $entityId
85
            ));
86
        }
87
88 3
        if (!empty($this->authorizationAttribute)) {
89 1
            $this->controllerHelper->denyAccessUnlessGranted($this->authorizationAttribute, $entity);
90
        }
91
92 2
        $this->controllerHelper->dispatchEvent("symfony_generics.entity_interaction", new EntityInteractionEvent(
93 2
            $this->entityClass,
94 2
            $entityId,
95 2
            $entity,
96 2
            "__destruct"
97
        ));
98
99 2
        $this->controllerHelper->removeEntity($entity);
100
101 2
        return new Response("Entity removed!");
102
    }
103
104
}
105