CreateListener::addOperationDefault()   B
last analyzed

Complexity

Conditions 8
Paths 48

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 22
c 1
b 0
f 1
nc 48
nop 2
dl 0
loc 37
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\EventListener\Admin;
6
7
use LAG\AdminBundle\Event\Events\ResourceCreateEvent;
8
use LAG\AdminBundle\Metadata\AdminResource;
9
use LAG\AdminBundle\Metadata\CollectionOperationInterface;
10
use LAG\AdminBundle\Metadata\Create;
11
use LAG\AdminBundle\Metadata\OperationInterface;
12
use LAG\AdminBundle\Routing\Route\RouteNameGeneratorInterface;
13
use Symfony\Component\String\Inflector\EnglishInflector;
14
15
use function Symfony\Component\String\u;
16
17
class CreateListener
18
{
19
    public function __construct(
20
        private RouteNameGeneratorInterface $routeNameGenerator,
21
    ) {
22
    }
23
24
    public function __invoke(ResourceCreateEvent $event): void
25
    {
26
        $resource = $event->getResource();
27
        $operations = [];
28
        $resource = $this->addResourceDefault($resource);
29
30
        foreach ($resource->getOperations() as $operation) {
31
            $operation = $this->addOperationDefault($resource, $operation)->withResource($resource);
32
            $operations[$operation->getName()] = $operation;
33
        }
34
        $resource = $resource->withOperations($operations);
35
        $event->setResource($resource);
36
    }
37
38
    private function addResourceDefault(AdminResource $resource): AdminResource
39
    {
40
        if (!$resource->getTitle()) {
41
            $inflector = new EnglishInflector();
42
            $resource = $resource->withTitle($inflector->pluralize($resource->getName())[0]);
0 ignored issues
show
Bug introduced by
It seems like $resource->getName() can also be of type null; however, parameter $singular of Symfony\Component\String...hInflector::pluralize() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
            $resource = $resource->withTitle($inflector->pluralize(/** @scrutinizer ignore-type */ $resource->getName())[0]);
Loading history...
43
        }
44
45
        if ($resource->getPrefix() === null) {
0 ignored issues
show
Bug introduced by
The method getPrefix() does not exist on LAG\AdminBundle\Metadata\AdminResource. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
        if ($resource->/** @scrutinizer ignore-call */ getPrefix() === null) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
            $resource = $resource->withPrefix($resource->getName());
0 ignored issues
show
Bug introduced by
The method withPrefix() does not exist on LAG\AdminBundle\Metadata\AdminResource. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
            /** @scrutinizer ignore-call */ 
47
            $resource = $resource->withPrefix($resource->getName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        }
48
49
        return $resource;
50
    }
51
52
    private function addOperationDefault(AdminResource $resource, OperationInterface $operation): OperationInterface
53
    {
54
        if (!$operation->getName()) {
55
            $operation = $operation->withName(
56
                u(\get_class($operation))
57
                    ->afterLast('\\')
58
                    ->snake()
59
                    ->lower()
60
                    ->toString()
61
            );
62
        }
63
64
        if (!$operation->getResourceName()) {
0 ignored issues
show
Bug introduced by
The method getResourceName() does not exist on LAG\AdminBundle\Metadata\OperationInterface. Did you maybe mean getResource()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        if (!$operation->/** @scrutinizer ignore-call */ getResourceName()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
            $operation = $operation->withResourceName($resource->getName());
0 ignored issues
show
Bug introduced by
The method withResourceName() does not exist on LAG\AdminBundle\Metadata\OperationInterface. Did you maybe mean withResource()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            /** @scrutinizer ignore-call */ 
66
            $operation = $operation->withResourceName($resource->getName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        }
67
68
        if (!$operation->getTitle()) {
69
            $operation = $operation->withTitle(match ($operation->getName()) {
70
                'index' => 'List',
71
                default => u($operation->getName())->title(true)->toString(),
72
            });
73
        }
74
75
        if (!$operation->getRoute()) {
76
            $route = $this->routeNameGenerator->generateRouteName($resource, $operation);
77
            $operation = $operation->withRoute($route);
78
        }
79
80
        if (!$operation->getRouteParameters()) {
81
            if (!$operation instanceof CollectionOperationInterface && !$operation instanceof Create) {
82
                $operation = $operation->withRouteParameters(array_keys($operation->getIdentifiers()));
83
            } else {
84
                $operation = $operation->withRouteParameters([]);
85
            }
86
        }
87
88
        return $operation;
89
    }
90
}
91