OperationFactory::create()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 27
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 41
rs 9.488
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Metadata\Factory;
6
7
use LAG\AdminBundle\Event\Events\OperationEvent;
0 ignored issues
show
Bug introduced by
The type LAG\AdminBundle\Event\Events\OperationEvent 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...
8
use LAG\AdminBundle\Event\OperationEvents;
9
use LAG\AdminBundle\Filter\Factory\FilterFactoryInterface;
10
use LAG\AdminBundle\Metadata\AdminResource;
11
use LAG\AdminBundle\Metadata\CollectionOperationInterface;
12
use LAG\AdminBundle\Metadata\OperationInterface;
13
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
14
15
class OperationFactory implements OperationFactoryInterface
16
{
17
    public function __construct(
18
        private EventDispatcherInterface $eventDispatcher,
19
        private PropertyFactoryInterface $propertyFactory,
20
        private FilterFactoryInterface $filterFactory,
21
    ) {
22
    }
23
24
    public function create(AdminResource $resource, OperationInterface $operationDefinition): OperationInterface
25
    {
26
        $operationDefinition = $operationDefinition
27
            ->withResource($resource)
28
        ;
29
        $event = new OperationEvent($operationDefinition);
30
        $this->eventDispatcher->dispatch($event, OperationEvents::OPERATION_CREATE);
0 ignored issues
show
Bug introduced by
The constant LAG\AdminBundle\Event\Op...vents::OPERATION_CREATE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
31
        $this->eventDispatcher->dispatch($event, sprintf(
32
            OperationEvents::OPERATION_CREATE_PATTERN,
0 ignored issues
show
Bug introduced by
The constant LAG\AdminBundle\Event\Op...PERATION_CREATE_PATTERN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
33
            $resource->getName(),
34
            $operationDefinition->getName(),
35
        ));
36
        $properties = [];
37
        $operation = $event->getOperation();
38
39
        foreach ($operation->getProperties() as $property) {
40
            $properties[] = $this->propertyFactory->create($property);
41
        }
42
        $operation = $operation->withProperties($properties);
43
44
        if ($operation instanceof CollectionOperationInterface) {
45
            $filters = [];
46
47
            foreach ($operation->getFilters() ?? [] as $filter) {
48
                $filters[] = $this->filterFactory->create($filter);
49
            }
50
            $operation = $operation->withFilters($filters);
51
        }
52
53
        $event = new OperationEvent($operation);
54
        $this->eventDispatcher->dispatch($event, OperationEvents::OPERATION_CREATED);
0 ignored issues
show
Bug introduced by
The constant LAG\AdminBundle\Event\Op...ents::OPERATION_CREATED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
55
        $this->eventDispatcher->dispatch($event, sprintf(
56
            OperationEvents::OPERATION_CREATED_PATTERN,
0 ignored issues
show
Bug introduced by
The constant LAG\AdminBundle\Event\Op...ERATION_CREATED_PATTERN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
57
            $resource->getName(),
58
            $operation->getName(),
59
        ));
60
61
        // Ensure the operation belongs to the right resource
62
        return $event
63
            ->getOperation()
64
            ->withResource($resource)
65
        ;
66
    }
67
}
68