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