Completed
Push — master ( e8bfce...313e31 )
by Arnaud
13s queued 11s
created

ConfigurationFactory::createMenuConfiguration()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 12
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 18
ccs 0
cts 13
cp 0
crap 12
rs 9.8666
1
<?php
2
3
namespace LAG\AdminBundle\Factory;
4
5
use Exception;
6
use LAG\AdminBundle\Configuration\ActionConfiguration;
7
use LAG\AdminBundle\Configuration\AdminConfiguration;
8
use LAG\AdminBundle\Configuration\ApplicationConfiguration;
9
use LAG\AdminBundle\Configuration\MenuConfiguration;
10
use LAG\AdminBundle\Configuration\MenuItemConfiguration;
11
use LAG\AdminBundle\Event\Events;
12
use LAG\AdminBundle\Event\Events\ConfigurationEvent;
13
use LAG\AdminBundle\Event\Menu\MenuConfigurationEvent;
14
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface;
15
use Symfony\Component\OptionsResolver\OptionsResolver;
16
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
17
18
class ConfigurationFactory
19
{
20
    /**
21
     * @var EventDispatcherInterface
22
     */
23
    private $eventDispatcher;
24
25
    /**
26
     * @var ResourceRegistryInterface
27
     */
28
    private $registry;
29
30
    /**
31
     * ConfigurationFactory constructor.
32
     */
33
    public function __construct(EventDispatcherInterface $eventDispatcher, ResourceRegistryInterface $registry)
34
    {
35
        $this->eventDispatcher = $eventDispatcher;
36
        $this->registry = $registry;
37
    }
38
39
    public function createAdminConfiguration(
40
        string $adminName,
41
        array $configuration,
42
        ApplicationConfiguration $applicationConfiguration
43
    ): AdminConfiguration {
44
        $event = new ConfigurationEvent($adminName, $configuration, $adminName, $configuration['entity']);
45
        $this->eventDispatcher->dispatch(Events::CONFIGURATION_ADMIN, $event);
0 ignored issues
show
Bug introduced by
LAG\AdminBundle\Event\Events::CONFIGURATION_ADMIN of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

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

45
        $this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ Events::CONFIGURATION_ADMIN, $event);
Loading history...
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

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

45
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
46
                                dispatch(Events::CONFIGURATION_ADMIN, $event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
46
47
        try {
48
            $resolver = new OptionsResolver();
49
            $adminConfiguration = new AdminConfiguration($applicationConfiguration);
50
            $adminConfiguration->configureOptions($resolver);
51
            $adminConfiguration->setParameters($resolver->resolve($event->getConfiguration()));
52
        } catch (Exception $exception) {
53
            $message = 'The configuration of the admin "'.$adminName.'" is invalid: '.$exception->getMessage();
54
55
            throw new Exception($message, 500, $exception);
56
        }
57
58
        return $adminConfiguration;
59
    }
60
61
    public function createActionConfiguration(
62
        string $actionName,
63
        array $configuration,
64
        string $adminName,
65
        AdminConfiguration $adminConfiguration
66
    ): ActionConfiguration {
67
        $event = new ConfigurationEvent(
68
            $actionName,
69
            $adminConfiguration->getParameter('actions'),
70
            $adminName,
71
            $adminConfiguration->getParameter('entity')
72
        );
73
        $this->eventDispatcher->dispatch(Events::CONFIGURATION_ACTION, $event);
0 ignored issues
show
Bug introduced by
LAG\AdminBundle\Event\Events::CONFIGURATION_ACTION of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

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

73
        $this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ Events::CONFIGURATION_ACTION, $event);
Loading history...
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

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

73
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
74
                                dispatch(Events::CONFIGURATION_ACTION, $event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
74
75
        $resolver = new OptionsResolver();
76
        $actionConfiguration = new ActionConfiguration($actionName, $adminName, $adminConfiguration);
77
        $actionConfiguration->configureOptions($resolver);
78
        $actionConfiguration->setParameters($resolver->resolve($configuration));
79
80
        return $actionConfiguration;
81
    }
82
83
    public function createMenuConfiguration(string $menuName, array $configuration): MenuConfiguration
84
    {
85
        $event = new MenuConfigurationEvent($menuName, $configuration);
86
        $this->eventDispatcher->dispatch($event, Events::MENU_CONFIGURATION);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with LAG\AdminBundle\Event\Events::MENU_CONFIGURATION. ( Ignorable by Annotation )

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

86
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
87
                                dispatch($event, Events::MENU_CONFIGURATION);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
87
        $configuration = $event->getMenuConfiguration();
88
89
        foreach ($configuration['children'] as $itemName => $itemConfiguration) {
90
            if (null === $itemConfiguration) {
91
                $itemConfiguration = [];
92
            }
93
            $configuration['children'][$itemName] = $this->createMenuItemConfiguration($itemName, $itemConfiguration);
94
        }
95
        $resolver = new OptionsResolver();
96
        $menuConfiguration = new MenuConfiguration($menuName);
97
        $menuConfiguration->configureOptions($resolver);
98
        $menuConfiguration->setParameters($resolver->resolve($configuration));
99
100
        return $menuConfiguration;
101
    }
102
103
    public function createMenuItemConfiguration(string $itemName, array $configuration): MenuItemConfiguration
104
    {
105
        $resolver = new OptionsResolver();
106
        $itemConfiguration = new MenuItemConfiguration($itemName);
107
        $itemConfiguration->configureOptions($resolver);
108
        $itemConfiguration->setParameters($resolver->resolve($configuration));
109
110
        return $itemConfiguration;
111
    }
112
}
113