Test Failed
Push — release/2.5.x ( f4336c...2f2c8d )
by Muhammed
21:26 queued 14:46
created

ContentItemTypeType::buildView()   C

Complexity

Conditions 11
Paths 13

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 19
nc 13
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Sonata\AdminBundle\Admin\Pool;
9
use Symfony\Component\Form\AbstractType;
10
use Symfony\Component\Form\FormBuilderInterface;
11
use Symfony\Component\Form\FormView;
12
use Symfony\Component\Form\FormInterface;
13
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
14
use Symfony\Component\Routing\Exception\InvalidParameterException;
15
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
16
use Zicht\Bundle\PageBundle\Model\ContentItemContainer;
17
use Zicht\Util\Str;
18
19
/**
20
 * Provides a "type" dropdown for creating content items, and a "edit link" for editing content items
21
 * that have their own admin.
22
 */
23
class ContentItemTypeType extends AbstractType
24
{
25
    /**
26
     * Constructor
27
     *
28
     * @param \Sonata\AdminBundle\Admin\Pool $sonata
29
     * @param string $contentItemClass
30
     */
31
    public function __construct($contentItemClass, Pool $sonata = null)
32
    {
33
        $this->contentItemClass = $contentItemClass;
0 ignored issues
show
Bug introduced by
The property contentItemClass does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
        $this->sonata = $sonata;
0 ignored issues
show
Bug introduced by
The property sonata does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
    }
36
37
38
    /**
39
     * @{inheritDoc}
40
     */
41
    public function setDefaultOptions(OptionsResolverInterface $resolver)
42
    {
43
        $resolver->setDefaults(
44
            array(
45
                'inherit_data' => true,
46
                'data_class' => $this->contentItemClass,
47
                'container' => '',
48
                'translation_domain' => 'admin'
49
            )
50
        );
51
    }
52
53
54
    /**
55
     * @{inheritDoc}
56
     */
57
    public function buildForm(FormBuilderInterface $builder, array $options)
58
    {
59
        if ($options['container']) {
60
            $page = $options['container'];
61
            $choiceFilter = function ($choices) use ($page) {
62
                $ret = array();
63
                if ($page instanceof ContentItemContainer && null !== $page->getContentItemMatrix()) {
64
                    $types = $page->getContentItemMatrix()->getTypes();
65
66
                    foreach ($choices as $className => $name) {
67
                        if (in_array($className, $types)) {
68
                            $ret[$className] = $name;
69
                        }
70
                    }
71
                } else {
72
                    return $choices;
73
                }
74
                return $ret;
75
            };
76
        } else {
77
            $choiceFilter = null;
78
        }
79
        $builder
80
            ->add(
81
                'convertToType',
82
                'zicht_discriminator_map',
83
                array(
84
                    'entity' => $this->contentItemClass,
85
                    'choice_filter' => $choiceFilter
86
                )
87
            );
88
    }
89
90
91
    /**
92
     * @{inheritDoc}
93
     */
94
    public function buildView(FormView $view, FormInterface $form, array $options)
95
    {
96
        if (isset($view->vars['sonata_admin']['admin'])) {
97
            $genericAdmin = $view->vars['sonata_admin']['admin'];
98
99
            $parentAdmin = $genericAdmin->getParent();
100
101
            $subject = $form->getParent()->getData();
102
103
            $view->vars['type'] = null;
104
            $view->vars['edit_url'] = null;
105
106
107
            try {
108
                if ($subject->getRegion() !== null && $typeAdmin = $this->sonata->getAdminByClass(get_class($subject))) {
109
                    $view->vars['type']= Str::humanize(Str::classname($subject->getConvertToType()));
110
                    $childAdmin = $this->sonata->getAdminByAdminCode($parentAdmin->getCode() . '|' . $typeAdmin->getCode());
111
                    $childAdmin->setRequest($genericAdmin->getRequest());
112
113
                    if ($subject && $subject->getPage() && $subject->getPage()->getId()) {
114
                        try {
115
                            $view->vars['edit_url'] = $childAdmin->generateObjectUrl('edit', $subject);
116
                        } catch (InvalidParameterException $e) {
117
                            //2.2 edit url not needed when generating other admins (this is done in the POST of the sonata_collection_type)
118
                        } catch (MissingMandatoryParametersException $e) {
119
                            //>= 2.3
120
                        } catch (\Exception $e) {
121
                        }
122
                    }
123
                }
124
            } catch (\RuntimeException $e) {
125
            }
126
        }
127
    }
128
129
130
    /**
131
     * Returns the name of this type.
132
     *
133
     * @return string The name of this type
134
     */
135
    public function getName()
136
    {
137
        return 'zicht_content_item_type';
138
    }
139
}
140