AbstractFormAction::getTemplateVars()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Pim\Bundle\CustomEntityBundle\Action;
4
5
use Pim\Bundle\CustomEntityBundle\Event\ActionEventManager;
6
use Pim\Bundle\CustomEntityBundle\Manager\Registry as ManagerRegistry;
7
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
8
use Symfony\Component\Form\Form;
9
use Symfony\Component\Form\FormFactoryInterface;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\HttpFoundation\RedirectResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
use Symfony\Component\Routing\RouterInterface;
15
use Symfony\Component\Translation\TranslatorInterface;
16
17
/**
18
 * @author    Antoine Guigan <[email protected]>
19
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
20
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
21
 */
22
abstract class AbstractFormAction extends AbstractViewableAction
23
{
24
    /**
25
     * @var FormFactoryInterface
26
     */
27
    protected $formFactory;
28
29
    /**
30
     * @param ActionFactory        $actionFactory
31
     * @param ActionEventManager   $eventManager
32
     * @param ManagerRegistry      $managerRegistry
33
     * @param RouterInterface      $router
34
     * @param TranslatorInterface  $translator
35
     * @param EngineInterface      $templating
36
     * @param FormFactoryInterface $formFactory
37
     */
38
    public function __construct(
39
        ActionFactory $actionFactory,
40
        ActionEventManager $eventManager,
41
        ManagerRegistry $managerRegistry,
42
        RouterInterface $router,
43
        TranslatorInterface $translator,
44
        EngineInterface $templating,
45
        FormFactoryInterface $formFactory
46
    ) {
47
        parent::__construct($actionFactory, $eventManager, $managerRegistry, $router, $translator, $templating);
48
49
        $this->formFactory = $formFactory;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    protected function setDefaultOptions(OptionsResolver $resolver)
56
    {
57
        parent::setDefaultOptions($resolver);
58
59
        $resolver->setRequired(['form_type', 'success_message']);
60
        $action = $this->actionFactory->getAction($this->configuration->getName(), 'index');
61
        $resolver->setDefaults(
62
            [
63
                'form_options'              => [],
64
                'template'                  => 'PimCustomEntityBundle:CustomEntity:form.html.twig',
65
                'redirect_route'            => $action->getRoute(),
66
                'redirect_route_parameters' => $action->getRouteParameters(),
67
                'save_options'              => []
68
            ]
69
        );
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function doExecute(Request $request)
76
    {
77
        $object = $this->getObject($request);
78
        $form = $this->createForm($request, $object);
79
        if ($request->isMethod('post')) {
80
            $form->submit($request);
81
            if ($form->isValid()) {
82
                $this->saveForm($request, $form);
83
                $this->addFlash($request, 'success', $this->options['success_message']);
84
85
                return $this->getRedirectResponse($object);
86
            }
87
        }
88
89
        return $this->renderResponse(
90
            $this->getTemplateVars($request, $form)
91
        );
92
    }
93
94
    /**
95
     * Saves the object
96
     *
97
     * @param Request       $request
98
     * @param FormInterface $form
99
     */
100
    protected function saveForm(Request $request, FormInterface $form)
101
    {
102
        $this->getManager()->save($form->getData(), $this->options['save_options']);
103
    }
104
105
    /**
106
     * Gets the variables that should be present on the template
107
     *
108
     * @param Request $request
109
     * @param Form    $form
110
     */
111
    protected function getTemplateVars(Request $request, FormInterface $form)
112
    {
113
        return [
114
            'form'       => $form->createView(),
115
            'formAction' => $this->getActionUrl($this->getType(), $form->getData())
116
        ];
117
    }
118
119
    /**
120
     * Returns the redirect response in case of success
121
     *
122
     * @param object $object
123
     *
124
     * @return RedirectResponse
125
     */
126
    protected function getRedirectResponse($object)
127
    {
128
        return new RedirectResponse($this->getRedirectPath($object));
129
    }
130
131
    /**
132
     * Returns the path to be redirected to in case of success
133
     *
134
     * @param object $object
135
     *
136
     * @return string
137
     */
138
    protected function getRedirectPath($object)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
        return $this->router->generate(
141
            $this->options['redirect_route'],
142
            $this->options['redirect_route_parameters']
143
        );
144
    }
145
146
    /**
147
     * Creates the form
148
     *
149
     * @param Request $request
150
     * @param object  $object
151
     *
152
     * @return Form
153
     */
154
    protected function createForm(Request $request, $object)
155
    {
156
        return $this->formFactory->create(
157
            $this->options['form_type'],
158
            $object,
159
            $this->getFormOptions($request, $object)
160
        );
161
    }
162
163
    /**
164
     * Returns the options of the form
165
     *
166
     * @param Request $request
167
     * @param object  $object
168
     *
169
     * @return array
170
     */
171
    protected function getFormOptions(Request $request, $object)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $object is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
172
    {
173
        return ['data_class' => $this->configuration->getEntityClass()] + $this->options['form_options'];
174
    }
175
176
    /**
177
     * Returns the object to use in the form
178
     *
179
     * @param Request $request
180
     *
181
     * @return object
182
     */
183
    abstract protected function getObject(Request $request);
184
}
185