Passed
Pull Request — master (#371)
by Arnaud
33:38 queued 28:41
created

ResourceFactory::create()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 14
c 1
b 0
f 1
nc 4
nop 1
dl 0
loc 21
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Metadata\Factory;
6
7
use LAG\AdminBundle\Event\Events\ResourceEvent;
0 ignored issues
show
Bug introduced by
The type LAG\AdminBundle\Event\Events\ResourceEvent 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\ResourceEvents;
9
use LAG\AdminBundle\Exception\Exception;
10
use LAG\AdminBundle\Metadata\AdminResource;
11
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
12
13
class ResourceFactory implements ResourceFactoryInterface
14
{
15
    public function __construct(
16
        private EventDispatcherInterface $eventDispatcher,
17
        private OperationFactoryInterface $operationFactory,
18
    ) {
19
    }
20
21
    public function create(AdminResource $definition): AdminResource
22
    {
23
        $event = new ResourceEvent($definition);
24
        $this->eventDispatcher->dispatch($event, ResourceEvents::ADMIN_CREATE);
0 ignored issues
show
Bug introduced by
The constant LAG\AdminBundle\Event\ResourceEvents::ADMIN_CREATE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
25
        $this->eventDispatcher->dispatch($event, sprintf(ResourceEvents::ADMIN_CREATE_PATTERN, $definition->getName()));
0 ignored issues
show
Bug introduced by
The constant LAG\AdminBundle\Event\Re...s::ADMIN_CREATE_PATTERN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
26
        $resource = $event->getResource();
27
        $operations = [];
28
29
        foreach ($resource->getOperations() as $operationDefinition) {
30
            $operations[] = $this->operationFactory->create($resource, $operationDefinition->withResource($resource));
31
        }
32
        $resource = $resource->withOperations($operations);
33
        $event = new ResourceEvent($resource);
34
        $this->eventDispatcher->dispatch($event, ResourceEvents::ADMIN_CREATED);
0 ignored issues
show
Bug introduced by
The constant LAG\AdminBundle\Event\Re...ceEvents::ADMIN_CREATED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
35
        $this->eventDispatcher->dispatch($event, sprintf(ResourceEvents::ADMIN_CREATED_PATTERN, $resource->getName()));
0 ignored issues
show
Bug introduced by
The constant LAG\AdminBundle\Event\Re...::ADMIN_CREATED_PATTERN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
37
        if ($event->getResource()->getName() !== $definition->getName()) {
38
            throw new Exception(sprintf('The resource name "%s" to "%s" change is not allowed', $definition->getName(), $resource->getName()));
39
        }
40
41
        return $event->getResource();
42
    }
43
}
44