MenuTreeType   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B buildForm() 0 32 5
A configureOptions() 0 6 1
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\Symfony\Form\Type;
13
14
use LIN3S\CMSKernel\Application\Query\Menu\MenuOfIdHandler;
15
use LIN3S\CMSKernel\Application\Query\Menu\MenuOfIdQuery;
16
use LIN3S\CMSKernel\Domain\Model\Translation\TranslationDoesNotExistException;
17
use Symfony\Component\Form\AbstractType;
18
use Symfony\Component\Form\CallbackTransformer;
19
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
20
use Symfony\Component\Form\FormBuilderInterface;
21
use Symfony\Component\OptionsResolver\OptionsResolver;
22
23
/**
24
 * @author Beñat Espiña <[email protected]>
25
 */
26
class MenuTreeType extends AbstractType
27
{
28
    private $menuOfIdHandler;
29
30
    public function __construct(MenuOfIdHandler $menuOfIdHandler)
31
    {
32
        $this->menuOfIdHandler = $menuOfIdHandler;
33
    }
34
35
    public function buildForm(FormBuilderInterface $builder, array $options)
36
    {
37
        $locale = $options['locale'];
38
        $menuId = isset($options['menu_id']) ? $options['menu_id'] : null;
39
40
        $builder
41
            ->add('tree', HiddenType::class)
42
            ->addModelTransformer(new CallbackTransformer(
43
                function ($value) use ($locale, $menuId) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
                    if (null === $menuId) {
45
                        return ['tree' => []];
46
                    }
47
48
                    try {
49
                        $menu = $this->menuOfIdHandler->__invoke(
50
                            new MenuOfIdQuery(
51
                                $menuId,
52
                                $locale
53
                            )
54
                        );
55
56
                    } catch (TranslationDoesNotExistException $exception) {
57
                        return ['tree' => []];
58
                    }
59
60
                    return ['tree' => json_encode($menu['tree'])];
61
                },
62
                function ($value) {
63
                    return $value['tree'] ? json_decode($value['tree'], true) : [];
64
                }
65
            ));
66
    }
67
68
    public function configureOptions(OptionsResolver $resolver)
69
    {
70
        $resolver
71
            ->setRequired('locale')
72
            ->setDefined('menu_id');
73
    }
74
}
75