Completed
Pull Request — 2.x (#521)
by Maximilian
01:52
created

ContentAdmin::configureFormFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 8.7418
c 0
b 0
f 0
cc 1
nc 1
nop 1

How to fix   Long Method   

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\DoctrinePHPCRAdminBundle\Tests\Fixtures\App\Admin;
15
16
use Doctrine\Bundle\PHPCRBundle\Form\DataTransformer\DocumentToPathTransformer;
17
use Doctrine\Common\Persistence\ManagerRegistry;
18
use Doctrine\ODM\PHPCR\Mapping\Annotations\String;
19
use Sonata\AdminBundle\Datagrid\DatagridMapper;
20
use Sonata\AdminBundle\Datagrid\ListMapper;
21
use Sonata\AdminBundle\Form\FormMapper;
22
use Sonata\AdminBundle\Form\Type\ModelType;
23
use Sonata\AdminBundle\Show\ShowMapper;
24
use Sonata\DoctrinePHPCRAdminBundle\Filter\NodeNameFilter;
25
use Sonata\DoctrinePHPCRAdminBundle\Filter\StringFilter;
26
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
27
use Sonata\CoreBundle\Form\Type\CollectionType;
28
use Sonata\DoctrinePHPCRAdminBundle\Admin\Admin;
29
use Sonata\DoctrinePHPCRAdminBundle\Tests\Fixtures\App\Document\Content;
30
use Symfony\Component\Form\Extension\Core\Type\TextType;
31
use Symfony\Cmf\Bundle\TreeBrowserBundle\Form\Type\TreeSelectType;
32
33
/**
34
 * @author Maximilian Berghoff <[email protected]>
35
 */
36
class ContentAdmin extends Admin
37
{
38
    /**
39
     * @var ManagerRegistry
40
     */
41
    private $managerRegistry;
42
43
    public function setManagerRegistry(ManagerRegistry $managerRegistry)
44
    {
45
        $this->managerRegistry = $managerRegistry;
46
    }
47
48
    public function getExportFormats()
49
    {
50
        return [];
51
    }
52
53
    public function toString($object)
54
    {
55
        return $object instanceof Content && $object->getTitle()
56
            ? $object->getTitle()
57
            : $this->trans('link_add', [], 'SonataAdminBundle');
58
    }
59
60
    public function configureShowFields(ShowMapper $showMapper)
61
    {
62
        $showMapper
63
            ->tab('General')// the tab call is optional
64
            ->with(
65
                'Content',
66
                [
67
                    'class' => 'col-md-8',
68
                    'box_class' => 'box box-solid box-danger',
69
                    'description' => 'Main Content',
70
                ]
71
            )
72
            ->add('title')
73
            ->add('name')
74
            ->end()
75
            ->with('References')
76
            ->add(
77
                'children',
78
                null,
79
                [
80
                    'route' => ['name' => 'edit', 'parameters' => []],
81
                    'associated_property' => 'id',
82
                    'admin_code' => 'sonata_admin_doctrine_phpcr.test.admin',
83
                ]
84
            )
85
            ->add(
86
                'child',
87
                null,
88
                [
89
                    'route' => ['name' => 'edit', 'parameters' => []],
90
                    'associated_property' => 'id',
91
                    'admin_code' => 'sonata_admin_doctrine_phpcr.test.admin',
92
                ]
93
            )
94
            ->add(
95
                'singleRoute',
96
                null,
97
                ['route' => ['name' => 'edit', 'parameters' => []], 'associated_property' => 'id']
98
            )
99
            ->add(
100
                'routes',
101
                null,
102
                ['route' => ['name' => 'edit', 'parameters' => []], 'associated_property' => 'id']
103
            )
104
            ->end()
105
            ->end();
106
    }
107
108
    protected function configureListFields(ListMapper $listMapper)
109
    {
110
        $listMapper
111
            ->addIdentifier('id')
112
            ->add('title');
113
    }
114
115
    protected function configureFormFields(FormMapper $formMapper)
116
    {
117
        $formMapper
118
            ->with('form.group_general')
119
            ->add('name', TextType::class)
120
            ->add('title', TextType::class)
121
            ->add(
122
                'children',
123
                CollectionType::class,
124
                [
125
                    'label' => false, 'type_options' => [
126
                    'delete' => true,
127
                    'delete_options' => [
128
                        'type' => CheckboxType::class,
129
                        'type_options' => ['required' => false, 'mapped' => false],
130
                    ],
131
                ],
132
                ],
133
                ['edit' => 'inline', 'inline' => 'table', 'admin_code' => 'sonata_admin_doctrine_phpcr.test.admin']
134
            )
135
            ->add(
136
                'routes',
137
                ModelType::class,
138
                ['property' => 'title', 'multiple' => true, 'expanded' => false]
139
            )
140
            ->add(
141
                'parentDocument',
142
                TreeSelectType::class,
143
                [
144
                    'widget' => 'browser',
145
                    'root_node' => $this->getRootPath(),
146
                ]
147
            )
148
            ->add(
149
                'child',
150
                ModelType::class,
151
                [
152
                    'property' => 'title',
153
                    'class' => Content::class,
154
                    'btn_catalogue' => 'List',
155
                    'required' => false,
156
                ],
157
                ['admin_code' => 'sonata_admin_doctrine_phpcr.test.admin']
158
            )
159
            ->add(
160
                'singleRoute',
161
                TreeSelectType::class,
162
                [
163
                    'widget' => 'browser',
164
                    'root_node' => $this->getRootPath(),
165
                ]
166
            )
167
            ->end();
168
169
        $formMapper->getFormBuilder()->get('parentDocument')->addModelTransformer(
170
            new DocumentToPathTransformer(
171
                $this->managerRegistry->getManagerForClass($this->getClass())
172
            )
173
        );
174
175
        $formMapper->getFormBuilder()->get('singleRoute')->addModelTransformer(
176
            new DocumentToPathTransformer(
177
                $this->managerRegistry->getManagerForClass($this->getClass())
178
            )
179
        );
180
    }
181
182
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
183
    {
184
        $datagridMapper
185
            ->add('title', StringFilter::class)
186
            ->add('name', NodeNameFilter::class);
187
    }
188
}
189