These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\FormBundle\Entity\PageParts; |
||
4 | |||
5 | use ArrayObject; |
||
6 | use Doctrine\ORM\Mapping as ORM; |
||
7 | use Kunstmaan\FormBundle\Entity\FormSubmissionFieldTypes\BooleanFormSubmissionField; |
||
8 | use Kunstmaan\FormBundle\Form\BooleanFormSubmissionType; |
||
9 | use Kunstmaan\FormBundle\Form\CheckboxPagePartAdminType; |
||
10 | use Symfony\Component\Form\FormBuilderInterface; |
||
11 | use Symfony\Component\Validator\Constraints\NotBlank; |
||
12 | |||
13 | /** |
||
14 | * The checkbox page part can be used to create forms with checkbox input fields |
||
15 | * |
||
16 | * @ORM\Entity |
||
17 | * @ORM\Table(name="kuma_checkbox_page_parts") |
||
18 | */ |
||
19 | View Code Duplication | class CheckboxPagePart extends AbstractFormPagePart |
|
0 ignored issues
–
show
|
|||
20 | { |
||
21 | /** |
||
22 | * If set to true, you are obligated to fill in this page part |
||
23 | * |
||
24 | * @ORM\Column(type="boolean", nullable=true) |
||
25 | */ |
||
26 | protected $required = false; |
||
27 | |||
28 | /** |
||
29 | * Error message shows when the page part is required and nothing is filled in |
||
30 | * |
||
31 | * @ORM\Column(type="string", name="error_message_required", nullable=true) |
||
32 | */ |
||
33 | protected $errorMessageRequired; |
||
34 | |||
35 | /** |
||
36 | * Sets the required valud of this page part |
||
37 | * |
||
38 | * @param bool $required |
||
39 | * |
||
40 | * @return CheckboxPagePart |
||
41 | */ |
||
42 | 2 | public function setRequired($required) |
|
43 | { |
||
44 | 2 | $this->required = $required; |
|
45 | |||
46 | 2 | return $this; |
|
47 | } |
||
48 | |||
49 | /** |
||
50 | * Check if the page part is required |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | 2 | public function getRequired() |
|
55 | { |
||
56 | 2 | return $this->required; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Sets the message shown when the page part is required and no value was entered |
||
61 | * |
||
62 | * @param string $errorMessageRequired |
||
63 | * |
||
64 | * @return CheckboxPagePart |
||
65 | */ |
||
66 | 3 | public function setErrorMessageRequired($errorMessageRequired) |
|
67 | { |
||
68 | 3 | $this->errorMessageRequired = $errorMessageRequired; |
|
69 | |||
70 | 3 | return $this; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Get the error message that will be shown when the page part is required and no value was entered |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 2 | public function getErrorMessageRequired() |
|
79 | { |
||
80 | 2 | return $this->errorMessageRequired; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Returns the frontend view |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 1 | public function getDefaultView() |
|
89 | { |
||
90 | 1 | return '@KunstmaanForm/CheckboxPagePart/view.html.twig'; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Modify the form with the fields of the current page part |
||
95 | * |
||
96 | * @param FormBuilderInterface $formBuilder The form builder |
||
97 | * @param ArrayObject $fields The fields |
||
98 | * @param int $sequence The sequence of the form field |
||
99 | */ |
||
100 | 1 | public function adaptForm(FormBuilderInterface $formBuilder, ArrayObject $fields, $sequence) |
|
101 | { |
||
102 | 1 | $bfsf = new BooleanFormSubmissionField(); |
|
103 | 1 | $bfsf->setFieldName('field_'.$this->getUniqueId()); |
|
104 | 1 | $bfsf->setLabel($this->getLabel()); |
|
105 | 1 | $bfsf->setSequence($sequence); |
|
106 | |||
107 | 1 | $data = $formBuilder->getData(); |
|
108 | 1 | $data['formwidget_'.$this->getUniqueId()] = $bfsf; |
|
109 | 1 | $constraints = []; |
|
110 | 1 | if ($this->getRequired()) { |
|
111 | 1 | $options = []; |
|
112 | 1 | if (!empty($this->errorMessageRequired)) { |
|
113 | 1 | $options['message'] = $this->errorMessageRequired; |
|
114 | } |
||
115 | 1 | $constraints[] = new NotBlank($options); |
|
116 | } |
||
117 | 1 | $formBuilder->add( |
|
118 | 1 | 'formwidget_'.$this->getUniqueId(), |
|
119 | 1 | BooleanFormSubmissionType::class, |
|
120 | [ |
||
121 | 1 | 'label' => $this->getLabel(), |
|
122 | 1 | 'value_constraints' => $constraints, |
|
123 | 1 | 'required' => $this->getRequired(), |
|
124 | ] |
||
125 | ); |
||
126 | 1 | $formBuilder->setData($data); |
|
127 | |||
128 | 1 | $fields->append($bfsf); |
|
129 | 1 | } |
|
130 | |||
131 | /** |
||
132 | * Returns the default backend form type for this page part |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | 1 | public function getDefaultAdminType() |
|
137 | { |
||
138 | 1 | return CheckboxPagePartAdminType::class; |
|
139 | } |
||
140 | } |
||
141 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.