Passed
Push — master ( 0c21d6...074f02 )
by Jan
04:57
created

BaseEntityAdminForm::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
7
 *
8
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
23
 */
24
25
namespace App\Form\AdminPages;
26
27
use App\Entity\Attachments\Attachment;
28
use App\Entity\Base\NamedDBElement;
29
use App\Entity\Base\StructuralDBElement;
30
use App\Form\AttachmentFormType;
31
use App\Form\Type\MasterPictureAttachmentType;
32
use App\Form\Type\StructuralEntityType;
33
use FOS\CKEditorBundle\Form\Type\CKEditorType;
34
use function get_class;
35
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
36
use Symfony\Component\Form\AbstractType;
37
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
38
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
39
use Symfony\Component\Form\Extension\Core\Type\ResetType;
40
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
41
use Symfony\Component\Form\Extension\Core\Type\TextType;
42
use Symfony\Component\Form\FormBuilderInterface;
43
use Symfony\Component\OptionsResolver\OptionsResolver;
44
use Symfony\Component\Security\Core\Security;
45
46
class BaseEntityAdminForm extends AbstractType
47
{
48
    protected $security;
49
    protected $params;
50
51
    public function __construct(Security $security, ParameterBagInterface $params)
52
    {
53
        $this->security = $security;
54
        $this->params = $params;
55
    }
56
57
    public function configureOptions(OptionsResolver $resolver): void
58
    {
59
        parent::configureOptions($resolver); // TODO: Change the autogenerated stub
60
        $resolver->setRequired('attachment_class');
61
    }
62
63
    public function buildForm(FormBuilderInterface $builder, array $options): void
64
    {
65
        /** @var StructuralDBElement $entity */
66
        $entity = $options['data'];
67
        $is_new = null === $entity->getID();
68
69
        $builder
70
            ->add('name', TextType::class, [
71
                'empty_data' => '',
72
                'label' => 'name.label',
73
                'attr' => [
74
                    'placeholder' => 'part.name.placeholder',
75
                ],
76
                'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
0 ignored issues
show
introduced by
The condition $is_new is always false.
Loading history...
77
            ])
78
79
            ->add('parent', StructuralEntityType::class, [
80
                'class' => get_class($entity),
81
                'required' => false,
82
                'label' => 'parent.label',
83
                'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'move', $entity),
0 ignored issues
show
introduced by
The condition $is_new is always false.
Loading history...
84
            ])
85
86
            ->add('not_selectable', CheckboxType::class, [
87
                'required' => false,
88
                'label' => 'entity.edit.not_selectable',
89
                'help' => 'entity.edit.not_selectable.help',
90
                'label_attr' => [
91
                    'class' => 'checkbox-custom',
92
                ],
93
                'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
0 ignored issues
show
introduced by
The condition $is_new is always false.
Loading history...
94
            ])
95
96
            ->add('comment', CKEditorType::class, [
97
                'required' => false,
98
                'empty_data' => '',
99
                'label' => 'comment.label',
100
                'attr' => [
101
                    'rows' => 4,
102
                ],
103
                'help' => 'bbcode.hint',
104
                'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
0 ignored issues
show
introduced by
The condition $is_new is always false.
Loading history...
105
            ]);
106
107
        $this->additionalFormElements($builder, $options, $entity);
108
109
        //Attachment section
110
        $builder->add('attachments', CollectionType::class, [
111
            'entry_type' => AttachmentFormType::class,
112
            'allow_add' => true,
113
            'allow_delete' => true,
114
            'label' => false,
115
            'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
0 ignored issues
show
introduced by
The condition $is_new is always false.
Loading history...
116
            'entry_options' => [
117
                'data_class' => $options['attachment_class'],
118
            ],
119
            'by_reference' => false,
120
        ]);
121
122
        $builder->add('master_picture_attachment', MasterPictureAttachmentType::class, [
123
            'required' => false,
124
            'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
0 ignored issues
show
introduced by
The condition $is_new is always false.
Loading history...
125
            'label' => 'part.edit.master_attachment',
126
            'entity' => $entity,
127
        ]);
128
129
        //Buttons
130
        $builder->add('save', SubmitType::class, [
131
            'label' => $is_new ? 'entity.create' : 'entity.edit.save',
0 ignored issues
show
introduced by
The condition $is_new is always false.
Loading history...
132
            'attr' => [
133
                'class' => $is_new ? 'btn-success' : '',
0 ignored issues
show
introduced by
The condition $is_new is always false.
Loading history...
134
            ],
135
            'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
0 ignored issues
show
introduced by
The condition $is_new is always false.
Loading history...
136
        ])
137
            ->add('reset', ResetType::class, [
138
                'label' => 'entity.edit.reset',
139
                'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
0 ignored issues
show
introduced by
The condition $is_new is always false.
Loading history...
140
            ]);
141
    }
142
143
    protected function additionalFormElements(FormBuilderInterface $builder, array $options, NamedDBElement $entity): void
0 ignored issues
show
Unused Code introduced by
The parameter $entity is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

143
    protected function additionalFormElements(FormBuilderInterface $builder, array $options, /** @scrutinizer ignore-unused */ NamedDBElement $entity): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
    {
145
        //Empty for Base
146
    }
147
}
148