Passed
Pull Request — master (#116)
by Arnaud
05:44 queued 02:35
created

FormSubscriber::createEntityForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace LAG\AdminBundle\Event\Subscriber;
4
5
use LAG\AdminBundle\Admin\AdminInterface;
6
use LAG\AdminBundle\Event\Events;
7
use LAG\AdminBundle\Event\Events\FormEvent;
8
use LAG\AdminBundle\Factory\DataProviderFactory;
9
use LAG\AdminBundle\Factory\FormFactoryInterface;
10
use LAG\AdminBundle\LAGAdminBundle;
11
use LAG\AdminBundle\Utils\StringUtils;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
use Symfony\Component\Form\FormInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Session\Session;
16
use Symfony\Component\HttpFoundation\Session\SessionInterface;
17
use Symfony\Contracts\Translation\TranslatorInterface;
18
19
class FormSubscriber implements EventSubscriberInterface
20
{
21
    /**
22
     * @var DataProviderFactory
23
     */
24
    private $dataProviderFactory;
25
26
    /**
27
     * @var Session|SessionInterface
28
     */
29
    private $session;
30
31
    /**
32
     * @var TranslatorInterface
33
     */
34
    private $translator;
35
36
    /**
37
     * @var FormFactoryInterface
38
     */
39
    private $adminFormFactory;
40
41
    /**
42
     * @return array
43
     */
44
    public static function getSubscribedEvents()
45
    {
46
        return [
47
            Events::ADMIN_CREATE_FORM => 'createForm',
48
            Events::ADMIN_HANDLE_FORM => 'handleForm',
49
        ];
50
    }
51
52
    /**
53
     * FormSubscriber constructor.
54
     *
55
     * @param DataProviderFactory  $dataProviderFactory
56
     * @param FormFactoryInterface $adminFormFactory
57
     * @param SessionInterface     $session
58
     * @param TranslatorInterface  $translator
59
     */
60
    public function __construct(
61
        DataProviderFactory $dataProviderFactory,
62
        FormFactoryInterface $adminFormFactory,
63
        SessionInterface $session,
64
        TranslatorInterface $translator
65
    ) {
66
        $this->dataProviderFactory = $dataProviderFactory;
67
        $this->session = $session;
68
        $this->translator = $translator;
69
        $this->adminFormFactory = $adminFormFactory;
70
    }
71
72
    /**
73
     * Create a form for the loaded entity.
74
     *
75
     * @param FormEvent $event
76
     */
77
    public function createForm(FormEvent $event): void
78
    {
79
        $admin = $event->getAdmin();
80
        $action = $admin->getAction();
81
        $configuration = $action->getConfiguration();
82
83
        if (!$configuration->get('use_form')) {
84
            return;
85
        }
86
        $entity = null;
87
88
        if (LAGAdminBundle::LOAD_STRATEGY_UNIQUE === $configuration->get('load_strategy')) {
89
            if (!$admin->getEntities()->isEmpty()) {
90
                $entity = $admin->getEntities()->first();
91
            }
92
        }
93
94
        if ('create' === $action->getName() || 'edit' === $action->getName()) {
95
            $form = $this->adminFormFactory->createEntityForm($admin, $event->getRequest(), $entity);
96
            $event->addForm($form, 'entity');
97
        }
98
99
        if ('delete' === $action->getName()) {
100
            $form = $this->adminFormFactory->createDeleteForm($action, $event->getRequest(), $entity);
101
            $event->addForm($form, 'delete');
102
        }
103
    }
104
105
    /**
106
     * When the HANDLE_FORM event is dispatched, we handle the form according to the current action.
107
     *
108
     * @param FormEvent $event
109
     */
110
    public function handleForm(FormEvent $event): void
111
    {
112
        $admin = $event->getAdmin();
113
        $action = $admin->getAction();
114
115
        if ('delete' === $action->getName()) {
116
            if (!$admin->hasForm('delete')) {
117
                return;
118
            }
119
            $form = $admin->getForm('delete');
120
            $this->handleDeleteForm($event->getRequest(), $form, $admin);
121
        }
122
    }
123
124
    private function handleDeleteForm(Request $request, FormInterface $form, AdminInterface $admin): void
125
    {
126
        $form->handleRequest($request);
127
128
        if ($form->isSubmitted() && $form->isValid()) {
129
            $dataProvider = $this
130
                ->dataProviderFactory
131
                ->get($admin->getConfiguration()->get('data_provider'))
132
            ;
133
            $dataProvider->delete($admin);
134
135
            $message = StringUtils::getTranslationKey(
136
                $admin->getConfiguration()->get('translation_pattern'),
137
                $admin->getName(),
138
                'delete_success'
139
            );
140
            $this->session->getFlashBag()->add('success', $this->translator->trans($message));
141
        }
142
    }
143
}
144