Completed
Push — master ( bd4cbc...df0c76 )
by Arnaud
9s
created

ActionFactory::dispatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
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\EventDispatcherInterface;
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
     * @var EventDispatcherInterface
36
     */
37
    protected $eventDispatcher;
38
39
    /**
40
     * ActionFactory constructor.
41
     *
42
     * @param FieldFactory $fieldFactory
43
     * @param FilterFactory $filterFactory
44
     * @param ConfigurationFactory $configurationFactory
45
     * @param EventDispatcherInterface $eventDispatcher
46
     */
47 3
    public function __construct(
48
        FieldFactory $fieldFactory,
49
        FilterFactory $filterFactory,
50
        ConfigurationFactory $configurationFactory,
51
        EventDispatcherInterface $eventDispatcher
52
    ) {
53 3
        $this->fieldFactory = $fieldFactory;
54 3
        $this->filterFactory = $filterFactory;
55 3
        $this->configurationFactory = $configurationFactory;
56 3
        $this->eventDispatcher = $eventDispatcher;
57 3
    }
58
59
    /**
60
     * Create an Action from configuration values.
61
     *
62
     * @param string $actionName
63
     * @param array $configuration
64
     * @param AdminInterface $admin
65
     *
66
     * @return Action
67
     */
68 3
    public function create($actionName, array $configuration, AdminInterface $admin)
69
    {
70
        // before configuration event allows users to override action configuration
71 3
        $this->dispatch(
72 3
            ActionEvents::BEFORE_CONFIGURATION,
73 3
            $event = new BeforeConfigurationEvent($actionName, $configuration, $admin)
74 3
        );
75
76
        // create action configuration object
77 3
        $actionConfiguration = $this
78
            ->configurationFactory
79 3
            ->createActionConfiguration($actionName, $admin, $event->getActionConfiguration());
80
81
        // action create event allows users to know what configuration will be passed to the action
82 2
        $this->dispatch(
83 2
            ActionEvents::ACTION_CREATE,
84 2
            new ActionCreateEvent($actionName, $configuration, $admin)
85 2
        );
86
87
        // create action
88 2
        $action = new Action($actionName, $actionConfiguration);
89
90
        // adding fields items to actions
91 2
        foreach ($actionConfiguration->getParameter('fields') as $fieldName => $fieldConfiguration) {
92 2
            $field = $this
93
                ->fieldFactory
94 2
                ->create($fieldName, $fieldConfiguration);
95 2
            $action->addField($field);
96 2
        }
97
98
        // adding filters to the action
99 2
        foreach ($actionConfiguration->getParameter('filters') as $fieldName => $filterConfiguration) {
100
            $filter = $this
101
                ->filterFactory
102
                ->create($fieldName, $filterConfiguration);
103
            $action->addFilter($filter);
104 2
        }
105
        // add the action to the admin
106 2
        $admin->addAction($action);
107
108
        // allow users to listen after action creation
109 2
        $this->dispatch(
110 2
            ActionEvents::ACTION_CREATED,
111 2
            new ActionCreatedEvent($action, $admin)
112 2
        );
113
114 2
        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 3
    protected function dispatch($name, Event $event)
124
    {
125 3
        $this
126
            ->eventDispatcher
127 3
            ->dispatch($name, $event);
128 3
    }
129
}
130