SlugType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 62
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A buildForm() 0 10 1
A mapDataToForms() 0 19 3
A mapFormsToData() 0 7 1
A buildView() 0 10 1
A configureOptions() 0 4 1
A getBlockPrefix() 0 4 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\SharedKernel\Domain\Model\Slug\Slug;
15
use LIN3S\SharedKernel\Exception\InvalidArgumentException;
16
use Symfony\Component\Form\AbstractType;
17
use Symfony\Component\Form\DataMapperInterface;
18
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
19
use Symfony\Component\Form\FormBuilderInterface;
20
use Symfony\Component\Form\FormInterface;
21
use Symfony\Component\Form\FormView;
22
use Symfony\Component\OptionsResolver\OptionsResolver;
23
24
/**
25
 * @author Beñat Espiña <[email protected]>
26
 */
27
class SlugType extends AbstractType implements DataMapperInterface
28
{
29
    public function buildForm(FormBuilderInterface $builder, array $options)
30
    {
31
        $builder
32
            ->add('slug', HiddenType::class, [
33
                'label'              => 'lin3s_cms_kernel.form.type.slug.slug',
34
                'required'           => true,
35
                'translation_domain' => 'Lin3sCmsKernel',
36
            ])
37
            ->setDataMapper($this);
38
    }
39
40
    public function mapDataToForms($data, $forms)
41
    {
42
        if (null === $data) {
43
            return;
44
        }
45
        if (!$data instanceof Slug) {
46
            throw new InvalidArgumentException(
47
                sprintf(
48
                    'Given data must be %s instance, %s given',
49
                    Slug::class,
50
                    get_class($data)
51
                )
52
            );
53
        }
54
55
        $forms = iterator_to_array($forms);
56
57
        $forms['slug']->setData($data->slug());
58
    }
59
60
    public function mapFormsToData($forms, &$data)
61
    {
62
        $forms = iterator_to_array($forms);
63
        $data = [
64
            'slug' => $forms['slug']->getData(),
65
        ];
66
    }
67
68
    public function buildView(FormView $view, FormInterface $form, array $options)
69
    {
70
        $view->vars = array_merge(
71
            $view->vars, array_merge(
72
                $options, [
73
                    'from' => $options['from'],
74
                ]
75
            )
76
        );
77
    }
78
79
    public function configureOptions(OptionsResolver $resolver)
80
    {
81
        $resolver->setRequired(['from']);
82
    }
83
84
    public function getBlockPrefix()
85
    {
86
        return 'lin3s_cms_slug';
87
    }
88
}
89