Completed
Pull Request — master (#90)
by Arnaud
21:22
created

ActionFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 10
dl 0
loc 149
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A injectConfiguration() 0 55 4
A getActions() 0 21 4
A getActionServiceId() 0 10 2
1
<?php
2
3
namespace LAG\AdminBundle\Action\Factory;
4
5
use LAG\AdminBundle\Action\ActionInterface;
6
use LAG\AdminBundle\Action\Event\ActionCreatedEvent;
7
use LAG\AdminBundle\Action\Event\ActionEvents;
8
use LAG\AdminBundle\Action\Event\BeforeConfigurationEvent;
9
use LAG\AdminBundle\Action\Registry\Registry;
10
use LAG\AdminBundle\Admin\AdminInterface;
11
use LAG\AdminBundle\Field\Factory\FieldFactory;
12
use LAG\AdminBundle\LAGAdminBundle;
13
use LogicException;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * This class allow to inject an Admin into a Controller.
19
 */
20
class ActionFactory
21
{
22
    /**
23
     * @var ConfigurationFactory
24
     */
25
    protected $configurationFactory;
26
27
    /**
28
     * @var EventDispatcherInterface
29
     */
30
    protected $eventDispatcher;
31
    
32
    /**
33
     * @var Registry
34
     */
35
    protected $registry;
36
    
37
    /**
38
     * @var FieldFactory
39
     */
40
    protected $fieldFactory;
41
    
42
    /**
43
     * ActionFactory constructor.
44
     *
45
     * @param ConfigurationFactory     $configurationFactory
46
     * @param FieldFactory             $fieldFactory
47
     * @param EventDispatcherInterface $eventDispatcher
48
     * @param Registry                 $registry
49
     */
50
    public function __construct(
51
        ConfigurationFactory $configurationFactory,
52
        FieldFactory $fieldFactory,
53
        EventDispatcherInterface $eventDispatcher,
54
        Registry $registry
55
    ) {
56
        $this->configurationFactory = $configurationFactory;
57
        $this->eventDispatcher = $eventDispatcher;
58
        $this->registry = $registry;
59
        $this->fieldFactory = $fieldFactory;
60
    }
61
    
62
    /**
63
     * Inject an ActionConfiguration into an Action controller.
64
     *
65
     * @param mixed $controller
66
     * @param Request $request
67
     */
68
    public function injectConfiguration($controller, Request $request)
69
    {
70
        if (!$controller instanceof ActionInterface) {
71
            return;
72
        }
73
        
74
        if (!$controller->getAdmin() instanceof AdminInterface) {
75
            return;
76
        }
77
        // retrieve actions configuration
78
        $actionsConfiguration = $controller
79
            ->getAdmin()
80
            ->getConfiguration()
81
            ->getParameter('actions')
82
        ;
83
        $actionName = $request->get('_route_params')[LAGAdminBundle::REQUEST_PARAMETER_ACTION];
84
        
85
        // if the current action name is not supported, we do nothing
86
        if (!array_key_exists($actionName, $actionsConfiguration)) {
87
            return;
88
        }
89
        // BeforeConfigurationEvent allows users to override action configuration
90
        $event = new BeforeConfigurationEvent($actionName, $actionsConfiguration[$actionName], $controller->getAdmin());
91
        $this
92
            ->eventDispatcher
93
            ->dispatch(ActionEvents::BEFORE_CONFIGURATION, $event)
94
        ;
95
        
96
        // retrieve the current Action configuration
97
        $configuration = $this
98
            ->configurationFactory
99
            ->create(
100
                $actionName,
101
                $controller->getAdmin()->getName(),
102
                $controller->getAdmin()->getConfiguration(), $event->getActionConfiguration()
103
            )
104
        ;
105
        
106
        // allow users to listen after action creation
107
        $event = new ActionCreatedEvent($controller, $controller->getAdmin());
108
        $this
109
            ->eventDispatcher
110
            ->dispatch(
111
                ActionEvents::ACTION_CREATED, $event)
112
        ;
113
    
114
//        $fields = $this
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
115
//            ->fieldFactory
116
//            ->getFields($configuration->getParameters())
117
//        ;
118
        
119
        // inject the Action to the controller
120
        $controller->setConfiguration($configuration);
121
        //$controller->setFields($fields);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
122
    }
123
    
124
    /**
125
     * @param $adminName
126
     * @param array $configurations
127
     *
128
     * @return array
129
     */
130
    public function getActions($adminName, array $configurations)
131
    {
132
        $actions = [];
133
        
134
        foreach ($configurations['actions'] as $name => $configuration) {
135
            if (null !== $configuration && key_exists('service', $configuration)) {
136
                // if a service key is defined, take it
137
                $serviceId = $configuration['service'];
138
            } else {
139
                // if no service key was provided, we take the default action service
140
                $serviceId = $this->getActionServiceId($name, $adminName);
141
            }
142
            $action = $this
143
                ->registry
144
                ->get($serviceId)
145
            ;
146
            $actions[$name] = $action;
147
        }
148
        
149
        return $actions;
150
    }
151
    
152
    /**
153
     * @param $name
154
     * @param $adminName
155
     *
156
     * @return string
157
     */
158
    protected function getActionServiceId($name, $adminName)
159
    {
160
        $mapping = LAGAdminBundle::getDefaultActionServiceMapping();
161
        
162
        if (!key_exists($name, $mapping)) {
163
            throw new LogicException('Action "'.$name.'" service id was not found for admin "'.$adminName.'"');
164
        }
165
        
166
        return $mapping[$name];
167
    }
168
}
169