Completed
Push — master ( 1f36be...d8b059 )
by Beñat
03:59
created

NewTranslatableActionType::execute()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 8.551
c 0
b 0
f 0
cc 6
eloc 27
nc 3
nop 4
1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel library.
5
 *
6
 * Copyright (c) 2016 LIN3S <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LIN3S\CMSKernel\Infrastructure\LIN3SAdminBundle\Action\Type;
13
14
use LIN3S\AdminBundle\Action\ActionType;
15
use LIN3S\AdminBundle\Action\Type\EntityId;
16
use LIN3S\AdminBundle\Configuration\EntityConfiguration;
17
use LIN3S\AdminBundle\Form\FormHandler;
18
use LIN3S\SharedKernel\Application\CommandBus;
19
use Symfony\Component\HttpFoundation\RedirectResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
23
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
24
25
/**
26
 * @author Beñat Espiña <[email protected]>
27
 */
28
class NewTranslatableActionType implements ActionType
29
{
30
    use EntityId;
31
32
    private $flashBag;
33
    private $twig;
34
    private $formHandler;
35
    private $commandBus;
36
    private $urlGenerator;
37
38
    public function __construct(
39
        FormHandler $formHandler,
40
        CommandBus $commandBus,
41
        \Twig_Environment $twig,
42
        FlashBagInterface $flashBag,
43
        UrlGeneratorInterface $urlGenerator
44
    ) {
45
        $this->twig = $twig;
46
        $this->flashBag = $flashBag;
47
        $this->formHandler = $formHandler;
48
        $this->commandBus = $commandBus;
49
        $this->urlGenerator = $urlGenerator;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function execute($entity, EntityConfiguration $config, Request $request, $options = null)
56
    {
57
        $this->checkRequired($options, 'form');
58
59
        $locale = $request->query->get('locale');
60
//        if (loquesea) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
//            // TODO: HACER EL IF DE ARRIBA CUANDO EL LOCALE QUE NOS LLEGA NO ESTÁ DEFINIDO EN LA CONFIG
62
//            throw new NotFoundHttpException(
63
//                sprintf('%s locale is not supported from the admin', $locale)
64
//            );
65
//        }
66
67
        $form = $this->formHandler->createForm($options['form'], null, ['locale' => $locale]);
68
        if ($request->isMethod('POST') || $request->isMethod('PUT') || $request->isMethod('PATCH')) {
69
            $form->handleRequest($request);
70
            if ($form->isValid() && $form->isSubmitted()) {
71
                $this->commandBus->handle(
72
                    $form->getData()
73
                );
74
                $this->flashBag->add(
75
                    'lin3s_admin_success',
76
                    sprintf(
77
                        'The %s translation is successfully saved',
78
                        $config->name()
79
                    )
80
                );
81
82
                return $this->redirect($options, $config->name(), $form->getData());
83
            } else {
84
                $this->flashBag->add(
85
                    'lin3s_admin_error',
86
                    sprintf(
87
                        'Errors while saving %s translation. Please check all fields and try again',
88
                        $config->name()
89
                    )
90
                );
91
            }
92
        }
93
94
        return new Response(
95
            $this->twig->render('@LIN3SAdmin/Admin/new.html.twig', [
96
                'entity'       => $entity,
97
                'entityConfig' => $config,
98
                'locale'       => $locale,
99
                'form'         => $form->createView(),
100
            ])
101
        );
102
    }
103
104
    private function checkRequired($options, $field)
105
    {
106
        if (!isset($options[$field])) {
107
            throw new \InvalidArgumentException(
108
                sprintf(
109
                    '%s option is required so, you must declare inside action in the admin.yml', $field
110
                )
111
            );
112
        }
113
    }
114
115
    public function redirect($options, $entity, $command)
116
    {
117
        if (!isset($options['redirectAction'])) {
118
            return new RedirectResponse(
119
                $this->urlGenerator->generate('lin3s_admin_list', [
120
                    'entity' => $entity,
121
                ])
122
            );
123
        }
124
125
        return new RedirectResponse(
126
            $this->urlGenerator->generate('lin3s_admin_custom', [
127
                'action' => $options['redirectAction'],
128
                'entity' => $entity,
129
                'id'     => $command->id(),
130
            ])
131
        );
132
    }
133
}
134