CreateListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\EventListener\Operation;
6
7
use LAG\AdminBundle\Event\Events\OperationCreateEvent;
8
use LAG\AdminBundle\Form\Type\OperationDataType;
9
use LAG\AdminBundle\Form\Type\ResourceFilterType;
10
use LAG\AdminBundle\Metadata\Action;
0 ignored issues
show
Bug introduced by
The type LAG\AdminBundle\Metadata\Action was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use LAG\AdminBundle\Metadata\CollectionOperationInterface;
12
use LAG\AdminBundle\Metadata\Create;
13
use LAG\AdminBundle\Metadata\Index;
14
use LAG\AdminBundle\Metadata\Update;
15
use LAG\AdminBundle\Routing\Route\RouteNameGeneratorInterface;
16
17
class CreateListener
18
{
19
    public function __construct(
20
        private RouteNameGeneratorInterface $routeNameGenerator,
21
    ) {
22
    }
23
24
    public function __invoke(OperationCreateEvent $event): void
25
    {
26
        $operation = $event->getOperation();
27
        $resource = $operation->getResource();
28
29
        if (!$operation->getItemActions()) {
30
            $actions = [];
31
32
            if ($operation instanceof CollectionOperationInterface) {
33
                if ($resource->hasOperation('update')) {
34
                    $actions[] = new Action(
35
                        resourceName: $resource->getName(),
36
                        operationName: 'update',
37
                        label: 'lag_admin.resource.update',
38
                        type: 'secondary'
39
                    );
40
                }
41
42
                if ($resource->hasOperation('delete')) {
43
                    $actions[] = new Action(
44
                        resourceName: $resource->getName(),
45
                        operationName: 'delete',
46
                        label: 'lag_admin.resource.delete',
47
                        type: 'danger'
48
                    );
49
                }
50
            } else {
51
                if ($resource->hasOperation('index')) {
52
                    $actions[] = new Action(
53
                        resourceName: $resource->getName(),
54
                        operationName: 'index',
55
                        label: 'lag_admin.ui.cancel',
56
                        type: 'light',
57
                    );
58
                }
59
            }
60
            $operation = $operation->withItemActions($actions);
61
        }
62
63
        if ($operation instanceof CollectionOperationInterface && !$operation->getListActions()) {
0 ignored issues
show
Bug introduced by
The method getListActions() does not exist on LAG\AdminBundle\Metadata...ctionOperationInterface. ( Ignorable by Annotation )

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

63
        if ($operation instanceof CollectionOperationInterface && !$operation->/** @scrutinizer ignore-call */ getListActions()) {

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...
64
            if ($resource->hasOperation('create')) {
65
                $operation = $operation->withListActions([new Action(
0 ignored issues
show
Bug introduced by
The method withListActions() does not exist on LAG\AdminBundle\Metadata...ctionOperationInterface. ( 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->withListActions([new Action(

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
                    resourceName: $resource->getName(),
67
                    operationName: 'create',
68
                    label: 'lag_admin.ui.create',
69
                    type: 'primary',
70
                )]);
71
            }
72
        }
73
74
        if (!$operation->getTargetRoute()) {
75
            if ($resource->hasOperation('index')) {
76
                $operation = $operation->withTargetRoute(
77
                    $this->routeNameGenerator->generateRouteName($resource, $resource->getOperation('index')),
78
                );
79
            }
80
        }
81
82
        if (!$operation->getFormType()) {
83
            if ($operation instanceof Create || $operation instanceof Update) {
84
                $operation = $operation
85
                    ->withFormType(OperationDataType::class)
86
                    ->withFormOptions(['exclude' => $resource->getIdentifiers()])
87
                ;
88
            }
89
90
            if ($operation instanceof Index && \count($operation->getFilters() ?? []) > 0) {
91
                $operation = $operation
92
                    ->withFormType(ResourceFilterType::class)
93
                ;
94
            }
95
        }
96
97
        if (is_a($operation->getFormType(), ResourceFilterType::class, true) && !\array_key_exists('operation', $operation->getFormOptions())) {
98
            $operation = $operation->withFormOptions([
99
                'operation' => $operation,
100
            ]);
101
        }
102
103
        if ($operation instanceof Index) {
104
            if ($operation->getListActions() === null) {
0 ignored issues
show
Bug introduced by
The method getListActions() does not exist on LAG\AdminBundle\Metadata\Index. ( Ignorable by Annotation )

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

104
            if ($operation->/** @scrutinizer ignore-call */ getListActions() === 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...
105
                $operation = $operation->withListActions([]);
0 ignored issues
show
Bug introduced by
The method withListActions() does not exist on LAG\AdminBundle\Metadata\Index. ( Ignorable by Annotation )

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

105
                /** @scrutinizer ignore-call */ 
106
                $operation = $operation->withListActions([]);

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...
106
            }
107
        }
108
109
        $event->setOperation($operation);
110
    }
111
}
112