Completed
Pull Request — master (#90)
by Arnaud
02:11
created

ActionFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 2
cts 2
cp 1
rs 9.6666
cc 1
eloc 7
nc 1
nop 3
crap 1
1
<?php
2
3
namespace LAG\AdminBundle\Action\Factory;
4
5
use Exception;
6
use LAG\AdminBundle\Action\ActionInterface;
7
use LAG\AdminBundle\Action\Event\ActionCreatedEvent;
8
use LAG\AdminBundle\Action\Event\ActionEvents;
9
use LAG\AdminBundle\Action\Event\BeforeConfigurationEvent;
10
use LAG\AdminBundle\Action\Registry\Registry;
11
use LAG\AdminBundle\Admin\AdminInterface;
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 5
    
37
    /**
38
     * ActionFactory constructor.
39
     *
40 5
     * @param ConfigurationFactory     $configurationFactory
41 5
     * @param EventDispatcherInterface $eventDispatcher
42 5
     * @param Registry                 $registry
43
     */
44
    public function __construct(
45
        ConfigurationFactory $configurationFactory,
46
        EventDispatcherInterface $eventDispatcher,
47
        Registry $registry
48
    ) {
49
        $this->configurationFactory = $configurationFactory;
50 4
        $this->eventDispatcher = $eventDispatcher;
51
        $this->registry = $registry;
52 4
    }
53 1
    
54
    /**
55
     * Inject an ActionConfiguration into an Action controller.
56 3
     *
57 1
     * @param mixed $controller
58
     * @param Request $request
59
     */
60
    public function injectConfiguration($controller, Request $request)
61 2
    {
62 2
        if (!$controller instanceof ActionInterface) {
63 2
            return;
64
        }
65 2
        
66
        if (!$controller->getAdmin() instanceof AdminInterface) {
67
            return;
68 2
        }
69 1
        // retrieve actions configuration
70
        $actionsConfiguration = $controller
71
            ->getAdmin()
72 1
            ->getConfiguration()
73
            ->getParameter('actions')
74 1
        ;
75 1
        $actionName = $request->get('_route_params')[LAGAdminBundle::REQUEST_PARAMETER_ACTION];
76
        
77
        // if the current action name is not supported, we do nothing
78
        if (!array_key_exists($actionName, $actionsConfiguration)) {
79
            return;
80 1
        }
81 1
        // BeforeConfigurationEvent allows users to override action configuration
82
        $event = new BeforeConfigurationEvent($actionName, $actionsConfiguration[$actionName], $controller->getAdmin());
83
        $this
84 1
            ->eventDispatcher
85
            ->dispatch(ActionEvents::BEFORE_CONFIGURATION, $event)
86 1
        ;
87 1
        
88 1
        // retrieve the current Action configuration
89
        $configuration = $this
90
            ->configurationFactory
91 1
            ->create(
92 1
                $actionName,
93
                $controller->getAdmin()->getName(),
94
                $controller->getAdmin()->getConfiguration(),
95
                $event->getActionConfiguration()
96
            )
97
        ;
98
        
99
        // allow users to listen after action creation
100
        $event = new ActionCreatedEvent($controller, $controller->getAdmin());
101
        $this
102
            ->eventDispatcher
103
            ->dispatch(
104
                ActionEvents::ACTION_CREATED, $event)
105
        ;
106
        
107
        // inject the Action to the controller
108
        $controller->setConfiguration($configuration);
109
    }
110
    
111
    /**
112
     * Return the Actions of an Admin configuration. Each Action will be retrieved from the Action registry. The key
113
     * will be retrieved either in the service configuration key if provided, either from the default service mapping
114
     * configuration.
115
     *
116
     * @param string $adminName
117
     * @param array  $configuration
118
     *
119
     * @return ActionInterface[]
120
     *
121
     * @throws Exception
122
     */
123
    public function getActions($adminName, array $configuration)
124
    {
125
        $actions = [];
126
    
127
        if (!key_exists('actions', $configuration)) {
128
            throw new Exception('Invalid configuration for admin "'.$adminName.'"');
129
        }
130
        
131
        foreach ($configuration['actions'] as $name => $actionConfiguration) {
132
            
133
            if (null !== $actionConfiguration && key_exists('service', $actionConfiguration)) {
134
                // if a service key is defined, take it
135
                $serviceId = $actionConfiguration['service'];
136
            } else {
137
                // if no service key was provided, we take the default action service
138
                $serviceId = $this->getDefaultActionServiceId($name, $adminName);
139
            }
140
            $action = $this
141
                ->registry
142
                ->get($serviceId)
143
            ;
144
            $actions[$name] = $action;
145
        }
146
        
147
        return $actions;
148
    }
149
    
150
    /**
151
     * Return the default action service id, according to the Action and Admin names.
152
     *
153
     * @param string $name
154
     * @param string $adminName
155
     *
156
     * @return string
157
     */
158
    protected function getDefaultActionServiceId($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