SeoType   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 68
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B buildForm() 0 31 1
A mapDataToForms() 0 22 3
A mapFormsToData() 0 10 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\Domain\Model\Seo\Metadata;
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\ChoiceType;
19
use Symfony\Component\Form\Extension\Core\Type\TextType;
20
use Symfony\Component\Form\FormBuilderInterface;
21
22
/**
23
 * @author Beñat Espiña <[email protected]>
24
 */
25
class SeoType extends AbstractType implements DataMapperInterface
26
{
27
    public function buildForm(FormBuilderInterface $builder, array $options)
28
    {
29
        $builder
30
            ->add('metaTitle', TextType::class, [
31
                'label'              => 'lin3s_cms_kernel.form.type.seo.meta_title',
32
                'required'           => false,
33
                'translation_domain' => 'Lin3sCmsKernel',
34
            ])
35
            ->add('metaDescription', TextType::class, [
36
                'label'              => 'lin3s_cms_kernel.form.type.seo.meta_description',
37
                'required'           => false,
38
                'translation_domain' => 'Lin3sCmsKernel',
39
            ])
40
            ->add('robotsIndex', ChoiceType::class, [
41
                'choices'            => [
42
                    'lin3s_cms_kernel.form.type.seo.robots_yes' => true,
43
                    'lin3s_cms_kernel.form.type.seo.robots_no'  => false,
44
                ],
45
                'label'              => 'lin3s_cms_kernel.form.type.seo.robots_index',
46
                'translation_domain' => 'Lin3sCmsKernel',
47
            ])
48
            ->add('robotsFollow', ChoiceType::class, [
49
                'choices'            => [
50
                    'lin3s_cms_kernel.form.type.seo.robots_yes' => true,
51
                    'lin3s_cms_kernel.form.type.seo.robots_no'  => false,
52
                ],
53
                'label'              => 'lin3s_cms_kernel.form.type.seo.robots_follow',
54
                'translation_domain' => 'Lin3sCmsKernel',
55
            ])
56
            ->setDataMapper($this);
57
    }
58
59
    public function mapDataToForms($data, $forms)
60
    {
61
        if (null === $data) {
62
            return;
63
        }
64
        if (!$data instanceof Metadata) {
65
            throw new InvalidArgumentException(
66
                sprintf(
67
                    'Given data must be %s instance, %s given',
68
                    Metadata::class,
69
                    get_class($data)
70
                )
71
            );
72
        }
73
74
        $forms = iterator_to_array($forms);
75
76
        $forms['metaTitle']->setData($data->title()->title());
77
        $forms['metaDescription']->setData($data->description()->description());
78
        $forms['robotsIndex']->setData($data->robots()->index());
79
        $forms['robotsFollow']->setData($data->robots()->follow());
80
    }
81
82
    public function mapFormsToData($forms, &$data)
83
    {
84
        $forms = iterator_to_array($forms);
85
        $data = [
86
            'metaTitle'       => $forms['metaTitle']->getData(),
87
            'metaDescription' => $forms['metaDescription']->getData(),
88
            'robotsIndex'     => $forms['robotsIndex']->getData(),
89
            'robotsFollow'    => $forms['robotsFollow']->getData(),
90
        ];
91
    }
92
}
93