Completed
Push — master ( f73844...02bccb )
by Beñat
03:38
created

TranslateActionType   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 13.25 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 11
loc 83
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 11 11 1
B execute() 0 54 8
A checkFormIsPassed() 0 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the CMS Kernel package.
5
 *
6
 * Copyright (c) 2016-present 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\Configuration\Model\Entity;
15
use LIN3S\AdminBundle\Configuration\Type\ActionType;
16
use LIN3S\AdminBundle\Form\FormHandler;
17
use LIN3S\CMSKernel\Domain\Model\Translation\TranslationDoesNotExistException;
18
use LIN3S\SharedKernel\Application\CommandBus;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
22
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23
24
/**
25
 * @author Beñat Espiña <[email protected]>
26
 */
27
class TranslateActionType implements ActionType
28
{
29
    private $flashBag;
30
    private $twig;
31
    private $formHandler;
32
    private $commandBus;
33
34 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...
35
        FormHandler $formHandler,
36
        CommandBus $commandBus,
37
        \Twig_Environment $twig,
38
        FlashBagInterface $flashBag
39
    ) {
40
        $this->twig = $twig;
41
        $this->flashBag = $flashBag;
42
        $this->formHandler = $formHandler;
43
        $this->commandBus = $commandBus;
44
    }
45
46
    public function execute($entity, Entity $config, Request $request, $options = null)
47
    {
48
        if (!$entity) {
49
            throw new NotFoundHttpException('The translatable does not exist');
50
        }
51
        $entityName = $config->name();
52
        $this->checkFormIsPassed($options);
53
        $id = $config->id($entity);
54
        $locale = $request->query->get('locale');
55
56
//        TODO: Check if locale is defined in the bundle configuration
57
//        if (whatever) {
58
//            throw new NotFoundHttpException(
59
//                sprintf('%s locale is not supported from the admin', $locale)
60
//            );
61
//        }
62
63
        try {
64
            $translation = $entity->{$locale}();
65
        } catch (TranslationDoesNotExistException $exception) {
66
            $translation = null;
67
        }
68
69
        $form = $this->formHandler->createForm($options['form'], $translation, [
70
            'locale'  => $locale,
71
            'page_id' => $id,
72
        ]);
73
        if ($request->isMethod('POST') || $request->isMethod('PUT') || $request->isMethod('PATCH')) {
74
            $form->handleRequest($request);
75
            if ($form->isValid() && $form->isSubmitted()) {
76
                $command = $form->getData();
77
78
                $this->commandBus->handle($command);
79
                $this->flashBag->add(
80
                    'lin3s_admin_success',
81
                    sprintf('The %s translation is successfully saved', $entityName)
82
                );
83
            } else {
84
                $this->flashBag->add(
85
                    'lin3s_admin_error',
86
                    sprintf('Errors while saving %s translation. Please check all fields and try again', $entityName)
87
                );
88
            }
89
        }
90
91
        return new Response(
92
            $this->twig->render('@CmsKernelAdminBridge/Admin/edit_translation.html.twig', [
93
                'entity'       => $entity,
94
                'entityConfig' => $config,
95
                'locale'       => $locale,
96
                'form'         => $form->createView(),
97
            ])
98
        );
99
    }
100
101
    private function checkFormIsPassed($options)
102
    {
103
        if (!isset($options['form'])) {
104
            throw new \InvalidArgumentException(
105
                '"form" option is required so, you must declare inside action in the admin.yml'
106
            );
107
        }
108
    }
109
}
110