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

TranslateActionType::execute()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 64
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 6.8232
c 0
b 0
f 0
cc 8
eloc 38
nc 7
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\CMSKernel\Domain\Model\Translation\TranslationDoesNotExistException;
19
use LIN3S\SharedKernel\Application\CommandBus;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpFoundation\Session\Session;
23
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
24
25
/**
26
 * @author Beñat Espiña <[email protected]>
27
 */
28
class TranslateActionType implements ActionType
29
{
30
    use EntityId;
31
32
    private $session;
33
    private $twig;
34
    private $formHandler;
35
    private $commandBus;
36
37
    public function __construct(
38
        FormHandler $formHandler,
39
        CommandBus $commandBus,
40
        \Twig_Environment $twig,
41
        Session $session
42
    ) {
43
        $this->twig = $twig;
44
        $this->session = $session;
45
        $this->formHandler = $formHandler;
46
        $this->commandBus = $commandBus;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function execute($entity, EntityConfiguration $config, Request $request, $options = null)
53
    {
54
        $this->checkRequired($options, 'form');
55
56
        $id = (string) $this->getEntityId($entity, $config);
57
        if (!$entity) {
58
            throw new NotFoundHttpException(
59
                sprintf('The translatable with %s id does not exist', $id)
60
            );
61
        }
62
63
        $locale = $request->query->get('locale');
64
//        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...
65
//            // TODO: HACER EL IF DE ARRIBA CUANDO EL LOCALE QUE NOS LLEGA NO ESTÁ DEFINIDO EN LA CONFIG
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->session->getFlashBag()->add(
90
                    'lin3s_admin_success',
91
                    sprintf(
92
                        'The %s translation is successfully saved',
93
                        $config->name()
94
                    )
95
                );
96
            } else {
97
                $this->session->getFlashBag()->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
    private function checkRequired($options, $field)
118
    {
119
        if (!isset($options[$field])) {
120
            throw new \InvalidArgumentException(
121
                sprintf(
122
                    '%s option is required so, you must declare inside action in the admin.yml', $field
123
                )
124
            );
125
        }
126
    }
127
}
128