ContentItemRegionType   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 75.51%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 106
ccs 37
cts 49
cp 0.7551
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A __construct() 0 5 1
A setDefaultOptions() 0 11 1
A finishView() 0 16 3
A buildForm() 0 22 5
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\PageBundle\Type;
7
8
use Symfony\Component\Form\FormInterface;
9
use Symfony\Component\Form\FormView;
10
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
11
use Symfony\Component\Form\FormBuilderInterface;
12
use Symfony\Component\Form\AbstractType;
13
use Symfony\Component\Translation\TranslatorInterface;
14
use Zicht\Bundle\PageBundle\Model\ContentItemContainer;
15
use Zicht\Util\Str;
16
17
/**
18
 * Provides a type for selecting the region of the content item.
19
 */
20
class ContentItemRegionType extends AbstractType
21
{
22
    /**
23
     * @var string $contentItemClassName
24
     */
25
    protected $contentItemClassName;
26
27
    /**
28
     * @var array $defaultRegions
29
     */
30
    protected $defaultRegions = array();
31
32
    /**
33
     * @var TranslatorInterface $translator
34
     */
35
    private $translator;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param string $contentItemClassName
41
     * @param array $defaultRegions
42
     * @param TranslatorInterface $translator
43
     */
44 1
    public function __construct($contentItemClassName, array $defaultRegions, TranslatorInterface $translator)
45
    {
46 1
        $this->contentItemClassName = $contentItemClassName;
47 1
        $this->defaultRegions = $defaultRegions;
48 1
        $this->translator = $translator;
49 1
    }
50
51
    /**
52
     * @{inheritDoc}
53
     */
54 1
    public function setDefaultOptions(OptionsResolverInterface $resolver)
55
    {
56 1
        parent::setDefaultOptions($resolver);
57
58 1
        $resolver->setDefaults(
59
            array(
60 1
                'inherit_data'    => true,
61 1
                'data_class' => $this->contentItemClassName,
62 1
                'container'  => '',
63 1
                'default_regions' => $this->defaultRegions,
64 1
                'translation_domain' => 'admin',
65
            )
66 1
        );
67 1
    }
68
69
    /**
70
     * @{inheritDoc}
71
     */
72 2
    public function buildForm(FormBuilderInterface $builder, array $options)
73
    {
74 2
        $container = $options['container'];
75
        if ($container
76 2
            && $container instanceof ContentItemContainer
77 2
            && (null !== ($matrix = $container->getContentItemMatrix()))
78 2
        ) {
79 1
            $choices = array();
80 1
            foreach ($matrix->getRegions() as $c) {
81 1
                $choices[$c] = $c;
82 1
            }
83 1
            $builder->add(
84 1
                'region',
85 1
                'choice',
86
                array(
87 1
                    'choices' => $choices,
88 1
                    'translation_domain' => 'admin',
89 1
                    'empty_value' => $this->translator->trans('content_item_region.empty_value', array(), 'admin')
90 1
                )
91 1
            );
92 1
        } else {
93 1
            $builder->add('region', 'choice', array('choices' => $options['default_regions'], 'translation_domain' => 'admin'));
94
        }
95 2
    }
96
97
    /**
98
     * @{inheritDoc}
99
     */
100
    public function finishView(FormView $view, FormInterface $form, array $options)
101
    {
102
        $matrix = array();
103
        $fullmatrix = $options['container']->getContentItemMatrix()->getMatrix();
104
105
        foreach ($fullmatrix as $region => $classNames) {
106
            foreach ($classNames as $className) {
107
                $snakeCasedClassName = strtolower(str_replace(' ', '_', Str::humanize($className)));
108
                $placeholder = sprintf('content_item.type.%s', $snakeCasedClassName);
109
                $matrix[$region][$className] = $this->translator->trans($placeholder, array(), 'admin', 'nl');
110
            }
111
        }
112
113
        // todo: the matrix says something over both the DiscriminatorMapType and the ContentItemRegionType,
114
        // hence it should be moved to the ContentItemTypeType
115
        $view->vars['matrix'] = json_encode($matrix);
116
    }
117
118
    /**
119
     * Returns the name of this type.
120
     *
121
     * @return string The name of this type
122
     */
123 1
    public function getName()
124
    {
125 1
        return 'zicht_content_item_region';
126
    }
127
}
128