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

ActionFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 35.56%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 10
dl 0
loc 135
ccs 16
cts 45
cp 0.3556
rs 10
c 0
b 0
f 0

4 Methods

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