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\FormSubmissionFieldTypes; |
||
4 | |||
5 | use Doctrine\ORM\Mapping as ORM; |
||
6 | use Gedmo\Sluggable\Util\Urlizer; |
||
7 | use Kunstmaan\FormBundle\Entity\FormSubmissionField; |
||
8 | use Kunstmaan\FormBundle\Form\FileFormSubmissionType; |
||
9 | use Symfony\Component\DependencyInjection\ContainerInterface; |
||
10 | use Symfony\Component\Form\Form; |
||
11 | use Symfony\Component\Form\FormBuilderInterface; |
||
12 | use Symfony\Component\HttpFoundation\File\UploadedFile; |
||
13 | use Symfony\Component\HttpFoundation\Request; |
||
14 | use Symfony\Component\Validator\Constraints as Assert; |
||
15 | |||
16 | /** |
||
17 | * The ChoiceFormSubmissionField can be used to store files to a FormSubmission |
||
18 | * |
||
19 | * @ORM\Entity |
||
20 | * @ORM\HasLifecycleCallbacks |
||
21 | * @ORM\Table(name="kuma_file_form_submission_fields") |
||
22 | */ |
||
23 | class FileFormSubmissionField extends FormSubmissionField |
||
24 | { |
||
25 | /** |
||
26 | * The file name |
||
27 | * |
||
28 | * @ORM\Column(name="ffsf_value", type="string") |
||
29 | */ |
||
30 | protected $fileName; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | * |
||
35 | * @ORM\Column(type="string", unique=true, length=255) |
||
36 | * @ORM\GeneratedValue(strategy="AUTO") |
||
37 | */ |
||
38 | protected $uuid; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | * |
||
43 | * @ORM\Column(type="string", nullable=true) |
||
44 | */ |
||
45 | protected $url; |
||
46 | |||
47 | /** |
||
48 | * Non-persistent storage of upload file |
||
49 | * |
||
50 | * @Assert\File(maxSize="6000000") |
||
51 | * |
||
52 | * @var UploadedFile |
||
53 | */ |
||
54 | public $file; |
||
55 | |||
56 | /** |
||
57 | * A string representation of the current value |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | public function __toString() |
||
62 | { |
||
63 | if (!empty($this->url)) { |
||
64 | return $this->url; |
||
65 | } |
||
66 | |||
67 | return !empty($this->fileName) ? $this->fileName : ''; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Checks if a file has been uploaded |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | public function isNull() |
||
76 | { |
||
77 | return null === $this->file && empty($this->fileName); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Move the file to the given uploadDir and save the filename |
||
82 | * |
||
83 | * @param string $uploadDir |
||
84 | */ |
||
85 | public function upload($uploadDir, $webDir) |
||
86 | { |
||
87 | // the file property can be empty if the field is not required |
||
88 | if (null === $this->file) { |
||
89 | return; |
||
90 | } |
||
91 | |||
92 | // sanitize filename for security |
||
93 | $safeFileName = $this->getSafeFileName($this->file); |
||
0 ignored issues
–
show
|
|||
94 | |||
95 | $uuid = uniqid(); |
||
96 | $this->setUuid($uuid); |
||
97 | |||
98 | // move takes the target directory and then the target filename to move to |
||
99 | $this->file->move(sprintf('%s/%s', $uploadDir, $uuid), $safeFileName); |
||
100 | |||
101 | // set the path property to the filename where you'ved saved the file |
||
102 | $this->fileName = $safeFileName; |
||
103 | |||
104 | // set the url to the uuid directory inside the web dir |
||
105 | $this->setUrl(sprintf('%s%s/', $webDir, $uuid) . $safeFileName); |
||
106 | |||
107 | // clean up the file property as you won't need it anymore |
||
108 | $this->file = null; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * This function will be triggered if the form was successfully posted. |
||
113 | * |
||
114 | * @param Form $form the Form |
||
115 | * @param FormBuilderInterface $formBuilder the FormBuilder |
||
116 | * @param Request $request the Request |
||
117 | * @param ContainerInterface $container the Container |
||
118 | */ |
||
119 | public function onValidPost(Form $form, FormBuilderInterface $formBuilder, Request $request, ContainerInterface $container) |
||
120 | { |
||
121 | $uploadDir = $container->getParameter('form_submission_rootdir'); |
||
122 | $webDir = $container->getParameter('form_submission_webdir'); |
||
123 | $this->upload($uploadDir, $webDir); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Create a safe file name for the uploaded file, so that it can be saved safely on the disk. |
||
128 | * |
||
129 | * @return string |
||
130 | */ |
||
131 | public function getSafeFileName() |
||
132 | { |
||
133 | $fileExtension = pathinfo($this->file->getClientOriginalName(), PATHINFO_EXTENSION); |
||
134 | $mimeTypeExtension = $this->file->guessExtension(); |
||
135 | $newExtension = !empty($mimeTypeExtension) ? $mimeTypeExtension : $fileExtension; |
||
136 | |||
137 | $baseName = !empty($fileExtension) ? basename($this->file->getClientOriginalName(), $fileExtension) : $this->file->getClientOriginalName(); |
||
138 | $safeBaseName = Urlizer::urlize($baseName); |
||
139 | |||
140 | return $safeBaseName . (!empty($newExtension) ? '.' . $newExtension : ''); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Set the filename for the uploaded file |
||
145 | * |
||
146 | * @param string $fileName |
||
147 | * |
||
148 | * @return FileFormSubmissionField |
||
149 | */ |
||
150 | public function setFileName($fileName) |
||
151 | { |
||
152 | $this->fileName = $fileName; |
||
153 | |||
154 | return $this; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Returns the filename of the uploaded file |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | public function getFileName() |
||
163 | { |
||
164 | return $this->fileName; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Set uuid |
||
169 | * |
||
170 | * @param string $uuid |
||
171 | * |
||
172 | * @return FileFormSubmissionField |
||
173 | */ |
||
174 | public function setUuid($uuid) |
||
175 | { |
||
176 | $this->uuid = $uuid; |
||
177 | |||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Get uuid |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | public function getUuid() |
||
187 | { |
||
188 | return $this->uuid; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Set url |
||
193 | * |
||
194 | * @param string $url |
||
195 | * |
||
196 | * @return FileFormSubmissionField |
||
197 | */ |
||
198 | public function setUrl($url) |
||
199 | { |
||
200 | $this->url = $url; |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Get url |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getUrl() |
||
211 | { |
||
212 | return $this->url; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Return the template for this field |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | public function getSubmissionTemplate() |
||
221 | { |
||
222 | return 'KunstmaanFormBundle:FileUploadPagePart:submission.html.twig'; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Returns the default form type for this FormSubmissionField |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | public function getDefaultAdminType() |
||
231 | { |
||
232 | return FileFormSubmissionType::class; |
||
233 | } |
||
234 | } |
||
235 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.