Completed
Push — master ( b86c40...af862a )
by Beñat
02:08
created

TranslateActionType::checkRequired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
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\Action\Type\OptionResolver;
17
use LIN3S\AdminBundle\Configuration\EntityConfiguration;
18
use LIN3S\AdminBundle\Form\FormHandler;
19
use LIN3S\CMSKernel\Domain\Model\Translation\TranslationDoesNotExistException;
20
use LIN3S\SharedKernel\Application\CommandBus;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
24
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
25
26
/**
27
 * @author Beñat Espiña <[email protected]>
28
 */
29
class TranslateActionType implements ActionType
30
{
31
    use EntityId;
32
    use OptionResolver;
33
34
    private $flashBag;
35
    private $twig;
36
    private $formHandler;
37
    private $commandBus;
38
39 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
        FormHandler $formHandler,
41
        CommandBus $commandBus,
42
        \Twig_Environment $twig,
43
        FlashBagInterface $flashBag
44
    ) {
45
        $this->twig = $twig;
46
        $this->flashBag = $flashBag;
47
        $this->formHandler = $formHandler;
48
        $this->commandBus = $commandBus;
49
    }
50
51
    public function execute($entity, EntityConfiguration $config, Request $request, $options = null)
52
    {
53
        $this->checkRequired($options, 'form');
54
55
        $id = (string) $this->getEntityId($entity, $config);
56
        if (!$entity) {
57
            throw new NotFoundHttpException(
58
                sprintf('The translatable with %s id does not exist', $id)
59
            );
60
        }
61
62
        $locale = $request->query->get('locale');
63
64
//        TODO: Check if locale is defined in the bundle configuration
65
//        if (whatever) {
66
//            throw new NotFoundHttpException(
67
//                sprintf('%s locale is not supported from the admin', $locale)
68
//            );
69
//        }
70
71
        try {
72
            $translation = $entity->{$locale}();
73
        } catch (TranslationDoesNotExistException $exception) {
74
            $translation = null;
75
        }
76
77
        $form = $this->formHandler->createForm(
78
            $options['form'],
79
            $translation, [
80
            'locale'  => $locale,
81
            'page_id' => $id,
82
        ]);
83
        if ($request->isMethod('POST') || $request->isMethod('PUT') || $request->isMethod('PATCH')) {
84
            $form->handleRequest($request);
85
            if ($form->isValid() && $form->isSubmitted()) {
86
                $this->commandBus->handle(
87
                    $form->getData()
88
                );
89
                $this->flashBag->add(
90
                    'lin3s_admin_success',
91
                    sprintf(
92
                        'The %s translation is successfully saved',
93
                        $config->name()
94
                    )
95
                );
96
            } else {
97
                $this->flashBag->add(
98
                    'lin3s_admin_error',
99
                    sprintf(
100
                        'Errors while saving %s translation. Please check all fields and try again',
101
                        $config->name()
102
                    )
103
                );
104
            }
105
        }
106
107
        return new Response(
108
            $this->twig->render('@Lin3sCmsAdminBridge/Admin/edit_translation.html.twig', [
109
                'entity'       => $entity,
110
                'entityConfig' => $config,
111
                'locale'       => $locale,
112
                'form'         => $form->createView(),
113
            ])
114
        );
115
    }
116
}
117