Completed
Pull Request — dev (#50)
by Arnaud
03:27
created

ActionFactory::dispatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
namespace LAG\AdminBundle\Action\Factory;
4
5
use LAG\AdminBundle\Action\Action;
6
use LAG\AdminBundle\Action\Event\ActionCreatedEvent;
7
use LAG\AdminBundle\Action\Event\ActionCreateEvent;
8
use LAG\AdminBundle\Action\Event\ActionEvents;
9
use LAG\AdminBundle\Action\Event\BeforeConfigurationEvent;
10
use LAG\AdminBundle\Admin\AdminInterface;
11
use LAG\AdminBundle\Admin\Factory\FilterFactory;
12
use LAG\AdminBundle\Configuration\Factory\ConfigurationFactory;
13
use LAG\AdminBundle\Field\Factory\FieldFactory;
14
use Symfony\Component\EventDispatcher\Event;
15
use Symfony\Component\EventDispatcher\EventDispatcher;
16
17
class ActionFactory
18
{
19
    /**
20
     * @var FieldFactory
21
     */
22
    protected $fieldFactory;
23
24
    /**
25
     * @var FilterFactory
26
     */
27
    protected $filterFactory;
28
29
    /**
30
     * @var ConfigurationFactory
31
     */
32
    protected $configurationFactory;
33
34
    /**
35 1
     * @var EventDispatcher
36
     */
37
    protected $eventDispatcher;
38
39
    /**
40 1
     * ActionFactory constructor.
41 1
     *
42 1
     * @param FieldFactory $fieldFactory
43 1
     * @param FilterFactory $filterFactory
44
     * @param ConfigurationFactory $configurationFactory
45
     * @param EventDispatcher $eventDispatcher
46
     */
47
    public function __construct(
48
        FieldFactory $fieldFactory,
49
        FilterFactory $filterFactory,
50
        ConfigurationFactory $configurationFactory,
51
        EventDispatcher $eventDispatcher
52
    ) {
53
        $this->fieldFactory = $fieldFactory;
54 1
        $this->filterFactory = $filterFactory;
55
        $this->configurationFactory = $configurationFactory;
56
        $this->eventDispatcher = $eventDispatcher;
57 1
    }
58
59 1
    /**
60
     * Create an Action from configuration values.
61
     *
62 1
     * @param string $actionName
63
     * @param array $configuration
64
     * @param AdminInterface $admin
65 1
     *
66 1
     * @return Action
67
     */
68 1
    public function create($actionName, array $configuration, AdminInterface $admin)
69 1
    {
70 1
        // before configuration event allows users to override action configuration
71
        $this->dispatch(
72
            ActionEvents::BEFORE_CONFIGURATION,
73 1
            $event = new BeforeConfigurationEvent($actionName, $configuration, $admin)
74
        );
75
76
        // create action configuration object
77
        $actionConfiguration = $this
78 1
            ->configurationFactory
79
            ->createActionConfiguration($actionName, $admin, $event->getActionConfiguration());
80 1
81
        // action create event allows users to know what configuration will be passed to the action
82
        $this->dispatch(
83
            ActionEvents::ACTION_CREATE,
84
            new ActionCreateEvent($actionName, $configuration, $admin)
85
        );
86
87
        // create action
88
        $action = new Action($actionName, $actionConfiguration);
89
90
        // adding fields items to actions
91
        foreach ($actionConfiguration->getParameter('fields') as $fieldName => $fieldConfiguration) {
92
            $field = $this
93
                ->fieldFactory
94
                ->create($fieldName, $fieldConfiguration);
95
            $action->addField($field);
96
        }
97
98
        // adding filters to the action
99
        foreach ($actionConfiguration->getParameter('filters') as $fieldName => $filterConfiguration) {
100
            $filter = $this
101
                ->filterFactory
102
                ->create($fieldName, $filterConfiguration);
103
            $action->addFilter($filter);
104
        }
105
        // add the action to the admin
106
        $admin->addAction($action);
107
108
        // allow users to listen after action creation
109
        $this->dispatch(
110
            ActionEvents::ACTION_CREATED,
111
            new ActionCreatedEvent($action, $admin)
112
        );
113
114
        return $action;
115
    }
116
117
    /**
118
     * Dispatch an event using the main event dispatcher.
119
     *
120
     * @param string $name
121
     * @param Event $event
122
     */
123
    protected function dispatch($name, Event $event)
124
    {
125
        $this
126
            ->eventDispatcher
127
            ->dispatch($name, $event);
128
    }
129
}
130