Completed
Pull Request — master (#90)
by Arnaud
16:42
created

ActionFactory   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 67.39%

Importance

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

4 Methods

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