Passed
Pull Request — master (#175)
by Arnaud
08:30 queued 02:17
created

ConfigurationFactory::createMenuConfiguration()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 14
nop 2
dl 0
loc 34
ccs 0
cts 6
cp 0
crap 42
rs 8.9297
c 0
b 0
f 0
1
<?php
2
3
namespace LAG\AdminBundle\Factory;
4
5
use Exception;
6
use LAG\AdminBundle\Admin\Helper\AdminHelperInterface;
7
use LAG\AdminBundle\Configuration\ActionConfiguration;
8
use LAG\AdminBundle\Configuration\AdminConfiguration;
9
use LAG\AdminBundle\Configuration\ApplicationConfiguration;
10
use LAG\AdminBundle\Configuration\MenuConfiguration;
11
use LAG\AdminBundle\Event\Events;
12
use LAG\AdminBundle\Event\Events\ConfigurationEvent;
13
use LAG\AdminBundle\Event\Menu\MenuConfigurationEvent;
14
use LAG\AdminBundle\Exception\ConfigurationException;
15
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface;
16
use LAG\AdminBundle\Routing\Resolver\RoutingResolverInterface;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
19
20
class ConfigurationFactory
21
{
22
    /**
23
     * @var EventDispatcherInterface
24
     */
25
    private $eventDispatcher;
26
27
    /**
28
     * @var ResourceRegistryInterface
29
     */
30
    private $registry;
31
32
    /**
33
     * @var RoutingResolverInterface
34
     */
35
    private $routingResolver;
36
37
    /**
38
     * @var AdminHelperInterface
39
     */
40
    private $adminHelper;
41
42
    /**
43
     * ConfigurationFactory constructor.
44
     */
45
    public function __construct(
46
        EventDispatcherInterface $eventDispatcher,
47
        ResourceRegistryInterface $registry,
48
        RoutingResolverInterface $resolver,
49
        AdminHelperInterface $adminHelper
50
    ) {
51
        $this->eventDispatcher = $eventDispatcher;
52
        $this->registry = $registry;
53
        $this->routingResolver = $resolver;
54
        $this->adminHelper = $adminHelper;
55
    }
56
57
    public function createAdminConfiguration(
58
        string $adminName,
59
        array $configuration,
60
        ApplicationConfiguration $applicationConfiguration
61
    ): AdminConfiguration {
62
        $event = new ConfigurationEvent($adminName, $configuration, $adminName, $configuration['entity']);
63
        $this->eventDispatcher->dispatch($event, Events::CONFIGURATION_ADMIN);
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::CONFIGURATION_ADMIN. ( Ignorable by Annotation )

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

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

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...
64
65
        try {
66
            $resolver = new OptionsResolver();
67
            $adminConfiguration = new AdminConfiguration($adminName, $applicationConfiguration);
68
            $adminConfiguration->configureOptions($resolver);
69
            $adminConfiguration->setParameters($resolver->resolve($event->getConfiguration()));
70
        } catch (Exception $exception) {
71
            $message = 'The configuration of the admin "'.$adminName.'" is invalid: '.$exception->getMessage();
72
73
            throw new Exception($message, 500, $exception);
74
        }
75
76
        return $adminConfiguration;
77
    }
78
79
    public function createActionConfiguration(
80
        string $actionName,
81
        array $configuration,
82
        string $adminName,
83
        AdminConfiguration $adminConfiguration
84
    ): ActionConfiguration {
85
        $event = new ConfigurationEvent(
86
            $actionName,
87
            $adminConfiguration->getParameter('actions'),
88
            $adminName,
89
            $adminConfiguration->getParameter('entity')
90
        );
91
        $this->eventDispatcher->dispatch($event, Events::CONFIGURATION_ACTION);
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::CONFIGURATION_ACTION. ( Ignorable by Annotation )

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

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

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...
92
93
        $resolver = new OptionsResolver();
94
        $actionConfiguration = new ActionConfiguration($actionName, $adminName, $adminConfiguration);
95
        $actionConfiguration->configureOptions($resolver);
96
        $actionConfiguration->setParameters($resolver->resolve($configuration));
97
98
        return $actionConfiguration;
99
    }
100
101
    public function createMenuConfiguration(string $menuName, array $configuration): MenuConfiguration
102
    {
103
        $event = new MenuConfigurationEvent($menuName, $configuration);
104
        $this->eventDispatcher->dispatch($event, Events::PRE_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\Ev...:PRE_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

104
        $this->eventDispatcher->/** @scrutinizer ignore-call */ 
105
                                dispatch($event, Events::PRE_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...
105
        $configuration = $event->getMenuConfiguration();
106
        $adminName = null;
107
108
        if (null !== $this->adminHelper->getCurrent()) {
109
            $adminName = $this->adminHelper->getCurrent()->getName();
110
            $actionMenus = $this->adminHelper->getCurrent()->getAction()->getConfiguration()->get('menus');
111
            $inherits = empty($configuration['inherits']) || false === $configuration['inherits'];
112
113
            if (!empty($actionMenus[$menuName])) {
114
                if ($inherits) {
115
                    $event->setMenuConfiguration(array_merge_recursive($configuration, $actionMenus[$menuName]));
116
                } else {
117
                    $event->setMenuConfiguration($actionMenus[$menuName]);
118
                }
119
            }
120
        }
121
        $this->eventDispatcher->dispatch($event, Events::MENU_CONFIGURATION);
122
        $configuration = $event->getMenuConfiguration();
123
124
        $resolver = new OptionsResolver();
125
        $menuConfiguration = new MenuConfiguration($menuName, $this->routingResolver, $adminName);
126
        $menuConfiguration->configureOptions($resolver);
127
128
        try {
129
            $menuConfiguration->setParameters($resolver->resolve($configuration));
130
        } catch (Exception $exception) {
131
            throw new ConfigurationException('menu', $menuName, $exception->getCode(), $exception);
132
        }
133
134
        return $menuConfiguration;
135
    }
136
}
137