AddMenuType::buildView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Php DDD Standard project.
5
 *
6
 * Copyright (c) 2017 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\Symfony\Form\Type\Menu;
13
14
use LIN3S\CMSKernel\Application\Command\Menu\ManageMenuCommand;
15
use LIN3S\CMSKernel\Domain\Model\Menu\EmptyMenuCodeException;
16
use LIN3S\CMSKernel\Domain\Model\Menu\EmptyMenuNameException;
17
use Symfony\Component\Form\AbstractType;
18
use Symfony\Component\Form\DataMapperInterface;
19
use Symfony\Component\Form\Extension\Core\Type\TextType;
20
use Symfony\Component\Form\FormBuilderInterface;
21
use Symfony\Component\Form\FormError;
22
use Symfony\Component\Form\FormInterface;
23
use Symfony\Component\Form\FormView;
24
use Symfony\Component\OptionsResolver\OptionsResolver;
25
use Symfony\Component\Translation\TranslatorInterface;
26
27
/**
28
 * @author Beñat Espiña <[email protected]>
29
 */
30
class AddMenuType extends AbstractType implements DataMapperInterface
31
{
32
    private $locale;
33
    private $translator;
34
35
    public function __construct(TranslatorInterface $translator)
36
    {
37
        $this->translator = $translator;
38
    }
39
40
    public function buildForm(FormBuilderInterface $builder, array $options)
41
    {
42
        $this->locale = $options['locale'];
43
44
        $builder
45
            ->add('code', TextType::class)
46
            ->add('translation', MenuTranslationType::class, [
47
                'locale' => $this->locale,
48
            ])
49
            ->setDataMapper($this);
50
    }
51
52
    public function configureOptions(OptionsResolver $resolver)
53
    {
54
        $resolver->setRequired('locale');
55
    }
56
57 View Code Duplication
    public function buildView(FormView $view, FormInterface $form, array $options)
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...
58
    {
59
        $view->vars = array_merge($view->vars, [
60
            'groups' => [
61
                [
62
                    'name'   => 'Menu',
63
                    'fields' => [
64
                        'code',
65
                        'translation.name',
66
                        'translation.items',
67
                    ],
68
                ],
69
            ],
70
        ]);
71
    }
72
73
    public function mapDataToForms($data, $forms)
74
    {
75
        if (null === $data) {
76
            return;
77
        }
78
        $forms = iterator_to_array($forms);
79
        $translation = $data->${$this->locale}();
80
81
        $forms['code']->setData($data->code());
82
83
        $forms['translation']->setData([
84
            'name'  => $translation->name(),
85
            'items' => $translation->items(),
86
        ]);
87
    }
88
89
    public function mapFormsToData($forms, &$data)
90
    {
91
        $forms = iterator_to_array($forms);
92
        $translation = $forms['translation']->getData();
93
94
        try {
95
            $data = new ManageMenuCommand(
96
                $this->locale,
97
                $translation['name'],
98
                $forms['code']->getData(),
99
                $translation['items']
100
            );
101
        } catch (EmptyMenuCodeException $exception) {
102
            $forms['translation']['code']->addError(
103
                new FormError(
104
                    $this->translator->trans(
105
                        'lin3s_cms_kernel.form.type.menu.error.empty_menu_code',
106
                        [],
107
                        'Lin3sCmsKernel'
108
                    )
109
                )
110
            );
111
        } catch (EmptyMenuNameException $exception) {
112
            $forms['translation']['name']->addError(
113
                new FormError(
114
                    $this->translator->trans(
115
                        'lin3s_cms_kernel.form.type.menu.error.empty_menu_name',
116
                        [],
117
                        'Lin3sCmsKernel'
118
                    )
119
                )
120
            );
121
        }
122
    }
123
}
124