Completed
Pull Request — master (#90)
by Arnaud
17:45
created

ActionFactory::getActions()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 10
cp 0
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 3
nop 2
crap 20
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\Configuration\Factory\ConfigurationFactory;
12
use LAG\AdminBundle\Field\Factory\FieldFactory;
13
use LAG\AdminBundle\LAGAdminBundle;
14
use LogicException;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * This class allow to inject an Admin into a Controller.
20
 */
21
class ActionFactory
22
{
23
    /**
24
     * @var ConfigurationFactory
25
     */
26
    protected $configurationFactory;
27
28
    /**
29
     * @var EventDispatcherInterface
30
     */
31
    protected $eventDispatcher;
32
    
33
    /**
34
     * @var Registry
35
     */
36
    protected $registry;
37
    
38
    /**
39
     * @var FieldFactory
40
     */
41
    protected $fieldFactory;
42
    
43
    /**
44
     * ActionFactory constructor.
45
     *
46
     * @param ConfigurationFactory     $configurationFactory
47
     * @param FieldFactory             $fieldFactory
48
     * @param EventDispatcherInterface $eventDispatcher
49
     * @param Registry                 $registry
50
     */
51
    public function __construct(
52
        ConfigurationFactory $configurationFactory,
53
        FieldFactory $fieldFactory,
54
        EventDispatcherInterface $eventDispatcher,
55
        Registry $registry
56
    ) {
57
        $this->configurationFactory = $configurationFactory;
58
        $this->eventDispatcher = $eventDispatcher;
59
        $this->registry = $registry;
60
        $this->fieldFactory = $fieldFactory;
61
    }
62
    
63
    /**
64
     * Inject an ActionConfiguration into an Action controller.
65
     *
66
     * @param mixed $controller
67
     * @param Request $request
68
     */
69
    public function injectConfiguration($controller, Request $request)
70
    {
71
        if (!$controller instanceof ActionInterface) {
72
            return;
73
        }
74
        
75
        if (!$controller->getAdmin() instanceof AdminInterface) {
76
            return;
77
        }
78
        // retrieve actions configuration
79
        $actionsConfiguration = $controller
80
            ->getAdmin()
81
            ->getConfiguration()
82
            ->getParameter('actions')
83
        ;
84
        $actionName = $request->get('_route_params')[LAGAdminBundle::REQUEST_PARAMETER_ACTION];
85
        
86
        // if the current action name is not supported, we do nothing
87
        if (!array_key_exists($actionName, $actionsConfiguration)) {
88
            return;
89
        }
90
        // BeforeConfigurationEvent allows users to override action configuration
91
        $event = new BeforeConfigurationEvent($actionName, $actionsConfiguration[$actionName], $controller->getAdmin());
92
        $this
93
            ->eventDispatcher
94
            ->dispatch(ActionEvents::BEFORE_CONFIGURATION, $event)
95
        ;
96
        
97
        // retrieve the current Action configuration
98
        $configuration = $this
99
            ->configurationFactory
100
            ->createActionConfiguration($actionName, $controller->getAdmin(), $event->getActionConfiguration())
101
        ;
102
        
103
        // allow users to listen after action creation
104
        $event = new ActionCreatedEvent($controller, $controller->getAdmin());
105
        $this
106
            ->eventDispatcher
107
            ->dispatch(
108
                ActionEvents::ACTION_CREATED, $event)
109
        ;
110
    
111
        $fields = $this
112
            ->fieldFactory
113
            ->getFields($configuration->getParameters())
114
        ;
115
        
116
        // inject the Action to the controller
117
        $controller->setConfiguration($configuration);
118
        $controller->setFields($fields);
119
    }
120
    
121
    /**
122
     * @param $adminName
123
     * @param array $configuration
124
     *
125
     * @return array
126
     */
127
    public function getActions($adminName, array $configuration)
128
    {
129
        $actions = [];
130
        
131
        foreach ($configuration['actions'] as $name => $actionConfiguration) {
132
            if (null !== $actionConfiguration && key_exists('service', $actionConfiguration)) {
133
                // if a service key is defined, take it
134
                $serviceId = $actionConfiguration['service'];
135
            } else {
136
                // if no service key was provided, we take the default action service
137
                $serviceId = $this->getActionServiceId($name, $adminName);
138
            }
139
            $action = $this
140
                ->registry
141
                ->get($serviceId)
142
            ;
143
            $actions[$name] = $action;
144
        }
145
        
146
        return $actions;
147
    }
148
    
149
    /**
150
     * @param $name
151
     * @param $adminName
152
     *
153
     * @return string
154
     */
155
    protected function getActionServiceId($name, $adminName)
156
    {
157
        $mapping = LAGAdminBundle::getDefaultActionServiceMapping();
158
        
159
        if (!key_exists($name, $mapping)) {
160
            throw new LogicException('Action "'.$name.'" service id was not found for admin "'.$adminName.'"');
161
        }
162
        
163
        return $mapping[$name];
164
    }
165
}
166