Passed
Pull Request — master (#207)
by
unknown
12:49
created

FormDataReader::getDocumentForm()   F

Complexity

Conditions 26
Paths 1596

Size

Total Lines 166
Code Lines 110

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 110
dl 0
loc 166
rs 0
c 0
b 0
f 0
cc 26
nc 1596
nop 0

How to fix   Long Method    Complexity   

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
namespace EWW\Dpf\Helper;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use EWW\Dpf\Domain\Model\DocumentForm;
18
use EWW\Dpf\Domain\Model\File;
19
use EWW\Dpf\Domain\Model\MetadataObject;
20
use TYPO3\CMS\Core\Core\Environment;
21
22
class FormDataReader
23
{
24
25
    /**
26
     * documentTypeRepository
27
     *
28
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
29
     * @TYPO3\CMS\Extbase\Annotation\Inject
30
     */
31
    protected $documentTypeRepository = null;
32
33
    /**
34
     * metadataPageRepository
35
     *
36
     * @var \EWW\Dpf\Domain\Repository\MetadataPageRepository
37
     * @TYPO3\CMS\Extbase\Annotation\Inject
38
     */
39
    protected $metadataPageRepository = null;
40
41
    /**
42
     * metadataGroupRepository
43
     *
44
     * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository
45
     * @TYPO3\CMS\Extbase\Annotation\Inject
46
     */
47
    protected $metadataGroupRepository = null;
48
49
    /**
50
     * metadataObjectRepository
51
     *
52
     * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository
53
     * @TYPO3\CMS\Extbase\Annotation\Inject
54
     */
55
    protected $metadataObjectRepository = null;
56
57
    /**
58
     * fileRepository
59
     *
60
     * @var \EWW\Dpf\Domain\Repository\FileRepository
61
     * @TYPO3\CMS\Extbase\Annotation\Inject
62
     */
63
    protected $fileRepository = null;
64
65
    /**
66
     * documentRepository
67
     *
68
     * @var \EWW\Dpf\Domain\Repository\DocumentRepository
69
     * @TYPO3\CMS\Extbase\Annotation\Inject
70
     */
71
    protected $documentRepository = null;
72
73
    /**
74
     * objectManager
75
     *
76
     * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
77
     * @TYPO3\CMS\Extbase\Annotation\Inject
78
     */
79
    protected $objectManager;
80
81
    /**
82
     *
83
     * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
84
     * @TYPO3\CMS\Extbase\Annotation\Inject
85
     */
86
    protected $configurationManager;
87
88
    /**
89
     * formData
90
     *
91
     * @var array
92
     */
93
    protected $formData;
94
95
    /**
96
     * documentType
97
     *
98
     * @var
99
     */
100
    protected $documentType;
101
102
    /**
103
     * uploadPath
104
     *
105
     * @var
106
     */
107
    protected $uploadPath;
108
109
    /**
110
     * basePath
111
     *
112
     * @var
113
     */
114
    protected $uploadBaseUrl;
115
116
    public function __construct()
117
    {
118
119
        $uploadFileUrl = new \EWW\Dpf\Helper\UploadFileUrl;
120
121
        $this->uploadBaseUrl = $uploadFileUrl->getUploadUrl() . "/";
122
123
        $this->uploadPath = Environment::getPublicPath() . "/" . $uploadFileUrl->getDirectory() . "/";
124
125
    }
126
127
    /**
128
     *
129
     * @param array $formData
130
     */
131
    public function setFormData($formData)
132
    {
133
        $this->formData     = $formData;
134
        $this->documentType = $this->documentTypeRepository->findByUid($formData['type']);
135
    }
136
137
    protected function getFields()
138
    {
139
140
        $fields = array();
141
142
        if (is_array($this->formData['metadata'])) {
143
            foreach ($this->formData['metadata'] as $key => $value) {
144
                $formField = new \EWW\Dpf\Helper\FormField($key, $value);
145
                $fields[]  = $formField;
146
            }
147
        }
148
149
        return $fields;
150
    }
151
152
    protected function getDeletedFiles()
153
    {
154
        $deletedFiles = array();
155
156
        if (is_array($this->formData['deleteFile'])) {
157
            foreach ($this->formData['deleteFile'] as $key => $value) {
158
159
                $file = $this->fileRepository->findByUid($value);
160
161
                // Deleting the primary file is not allowed.
162
                // if (!$file->isPrimaryFile()) {
163
                //    $deletedFiles[] = $file;
164
                // }
165
166
                $deletedFiles[] = $file;
167
            }
168
        }
169
170
        return $deletedFiles;
171
    }
172
173
    public function uploadError()
174
    {
175
        // todo: To be updated or implemented elsewhere
176
        return false;
177
178
        if (
0 ignored issues
show
Unused Code introduced by
IfNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
179
            $this->formData['primaryFile'] &&
180
            $this->formData['primaryFile']['error'] != UPLOAD_ERR_OK &&
181
            $this->formData['primaryFile']['error'] != UPLOAD_ERR_NO_FILE
182
        ) {
183
            return true;
184
        }
185
186
        if (is_array($this->formData['secondaryFiles'])) {
187
            foreach ($this->formData['secondaryFiles'] as $tmpFile) {
188
                if ($tmpFile['error'] != UPLOAD_ERR_OK && $tmpFile['error'] != UPLOAD_ERR_NO_FILE) {
189
                    return true;
190
                }
191
            }
192
        }
193
194
        return false;
195
    }
196
197
    protected function getUploadedFile($tmpFile, $primary = false, \EWW\Dpf\Domain\Model\File $file = null)
198
    {
199
200
        if (empty($file)) {
201
            $file = $this->objectManager->get(File::class);
202
        }
203
204
        $fileName = uniqid(time(), true);
205
206
        \TYPO3\CMS\Core\Utility\GeneralUtility::upload_copy_move($tmpFile['tmp_name'], $this->uploadPath . $fileName);
207
208
        $finfo       = finfo_open(FILEINFO_MIME_TYPE);
209
        $contentType = finfo_file($finfo, $this->uploadPath . $fileName);
210
        finfo_close($finfo);
211
212
        $file->setContentType($contentType);
213
214
        $file->setTitle($tmpFile['name']);
215
        $file->setLink($fileName);
216
        $file->setPrimaryFile($primary);
217
        $file->setFileIdentifier(uniqid(time(), true));
218
219
        if ($primary) {
220
            if ($file->getDatastreamIdentifier()) {
221
                $file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_CHANGED);
222
            } else {
223
                $file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_ADDED);
224
            }
225
        } else {
226
            $file->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_ADDED);
227
        }
228
229
        return $file;
230
    }
231
232
    public function getDocumentForm()
233
    {
234
        $fields = $this->getFields();
235
236
        $documentForm = new \EWW\Dpf\Domain\Model\DocumentForm();
237
        $documentForm->setCsrfToken($this->formData['csrfToken']);
238
        $documentForm->setUid($this->documentType->getUid());
239
        $documentForm->setDisplayName($this->documentType->getDisplayName());
240
        $documentForm->setName($this->documentType->getName());
241
        $documentForm->setDocumentUid($this->formData['documentUid']);
242
        $documentForm->setFedoraPid($this->formData['fedoraPid']);
243
        $documentForm->setValid(!empty($this->formData['validDocument']));
244
        if ($this->formData['comment']) {
245
            $documentForm->setComment($this->formData['comment']);
246
        }
247
248
        $documentData = array();
249
250
        foreach ($fields as $field) {
251
            $pageUid    = $field->getPageUid();
252
            $groupUid   = $field->getGroupUid();
253
            $groupIndex = $field->getGroupIndex();
254
            $fieldUid   = $field->getFieldUid();
255
            $fieldIndex = $field->getFieldIndex();
256
            $value      = $field->getValue();
257
258
            $documentData[$pageUid][$groupUid][$groupIndex][$fieldUid][$fieldIndex] = $value;
259
        }
260
261
        foreach ($documentData as $pageUid => $page) {
262
            $metadataPage     = $this->metadataPageRepository->findByUid($pageUid);
263
            $documentFormPage = new \EWW\Dpf\Domain\Model\DocumentFormPage();
264
            $documentFormPage->setUid($metadataPage->getUid());
265
            $documentFormPage->setDisplayName($metadataPage->getDisplayName());
266
            $documentFormPage->setName($metadataPage->getName());
267
268
            $documentFormPage->setAccessRestrictionRoles($metadataPage->getAccessRestrictionRoles());
269
270
            foreach ($page as $groupUid => $groupItem) {
271
                foreach ($groupItem as $group) {
272
                    $metadataGroup     = $this->metadataGroupRepository->findByUid($groupUid);
273
                    $documentFormGroup = new \EWW\Dpf\Domain\Model\DocumentFormGroup();
274
                    $documentFormGroup->setUid($metadataGroup->getUid());
275
                    $documentFormGroup->setDisplayName($metadataGroup->getDisplayName());
276
                    $documentFormGroup->setName($metadataGroup->getName());
277
                    $documentFormGroup->setMandatory($metadataGroup->getMandatory());
278
279
                    $documentFormGroup->setAccessRestrictionRoles($metadataGroup->getAccessRestrictionRoles());
280
281
                    $documentFormGroup->setMaxIteration($metadataGroup->getMaxIteration());
282
283
                    $fileLabel = "";
284
                    $fileDownload = "";
285
                    $fileArchive = "";
286
287
                    $fileIdentifier = '';
288
                    if (array_key_exists('fileIdentifier', $group)) {
289
                        $fileIdentifier = array_shift($group['fileIdentifier']);
290
                        unset($group['fileIdentifier']);
291
                    }
292
293
                    foreach ($group as $objectUid => $objectItem) {
294
295
                        foreach ($objectItem as $objectItem => $object) {
0 ignored issues
show
Comprehensibility Bug introduced by
$objectItem is overwriting a variable from outer foreach loop.
Loading history...
296
                            /** @var MetadataObject $metadataObject */
297
                            $metadataObject    = $this->metadataObjectRepository->findByUid($objectUid);
298
299
                            /** @var DocumentForm $documentFormField */
300
                            $documentFormField = new \EWW\Dpf\Domain\Model\DocumentFormField();
301
                            $documentFormField->setUid($metadataObject->getUid());
302
                            $documentFormField->setDisplayName($metadataObject->getDisplayName());
303
                            $documentFormField->setName($metadataObject->getName());
304
                            $documentFormField->setMandatory($metadataObject->getMandatory());
305
306
                            $documentFormField->setAccessRestrictionRoles($metadataObject->getAccessRestrictionRoles());
307
308
                            $documentFormField->setConsent($metadataObject->getConsent());
0 ignored issues
show
Bug introduced by
The method setConsent() does not exist on EWW\Dpf\Domain\Model\DocumentForm. Did you maybe mean setComment()? ( Ignorable by Annotation )

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

308
                            $documentFormField->/** @scrutinizer ignore-call */ 
309
                                                setConsent($metadataObject->getConsent());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
309
                            $documentFormField->setValidation($metadataObject->getValidation());
0 ignored issues
show
Bug introduced by
The method setValidation() does not exist on EWW\Dpf\Domain\Model\DocumentForm. Did you maybe mean setValid()? ( Ignorable by Annotation )

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

309
                            $documentFormField->/** @scrutinizer ignore-call */ 
310
                                                setValidation($metadataObject->getValidation());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
310
                            $documentFormField->setDataType($metadataObject->getDataType());
0 ignored issues
show
Bug introduced by
The method setDataType() does not exist on EWW\Dpf\Domain\Model\DocumentForm. ( Ignorable by Annotation )

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

310
                            $documentFormField->/** @scrutinizer ignore-call */ 
311
                                                setDataType($metadataObject->getDataType());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
311
                            $documentFormField->setMaxIteration($metadataObject->getMaxIteration());
312
                            $documentFormField->setInputOptions($metadataObject->getInputOptionList());
0 ignored issues
show
Bug introduced by
The method setInputOptions() does not exist on EWW\Dpf\Domain\Model\DocumentForm. ( Ignorable by Annotation )

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

312
                            $documentFormField->/** @scrutinizer ignore-call */ 
313
                                                setInputOptions($metadataObject->getInputOptionList());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
313
                            $documentFormField->setInputField($metadataObject->getInputField());
0 ignored issues
show
Bug introduced by
The method setInputField() does not exist on EWW\Dpf\Domain\Model\DocumentForm. ( Ignorable by Annotation )

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

313
                            $documentFormField->/** @scrutinizer ignore-call */ 
314
                                                setInputField($metadataObject->getInputField());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
314
                            $documentFormField->setFillOutService($metadataObject->getFillOutService());
0 ignored issues
show
Bug introduced by
The method setFillOutService() does not exist on EWW\Dpf\Domain\Model\DocumentForm. ( Ignorable by Annotation )

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

314
                            $documentFormField->/** @scrutinizer ignore-call */ 
315
                                                setFillOutService($metadataObject->getFillOutService());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
315
                            $documentFormField->setGndFieldUid($metadataObject->getGndFieldUid());
0 ignored issues
show
Bug introduced by
The method setGndFieldUid() does not exist on EWW\Dpf\Domain\Model\DocumentForm. ( Ignorable by Annotation )

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

315
                            $documentFormField->/** @scrutinizer ignore-call */ 
316
                                                setGndFieldUid($metadataObject->getGndFieldUid());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
316
                            $documentFormField->setMaxInputLength($metadataObject->getMaxInputLength());
0 ignored issues
show
Bug introduced by
The method setMaxInputLength() does not exist on EWW\Dpf\Domain\Model\DocumentForm. ( Ignorable by Annotation )

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

316
                            $documentFormField->/** @scrutinizer ignore-call */ 
317
                                                setMaxInputLength($metadataObject->getMaxInputLength());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
317
                            $documentFormField->setValue($object, $metadataObject->getDefaultValue());
0 ignored issues
show
Bug introduced by
The method setValue() does not exist on EWW\Dpf\Domain\Model\DocumentForm. Did you maybe mean setValid()? ( Ignorable by Annotation )

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

317
                            $documentFormField->/** @scrutinizer ignore-call */ 
318
                                                setValue($object, $metadataObject->getDefaultValue());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
318
319
                            $documentFormGroup->addItem($documentFormField);
320
321
                            if ($metadataGroup->isFileGroup()) {
322
323
                                // Use the existing file entry
324
                                $file = null;
325
                                $document = $this->documentRepository->findByUid($this->formData['documentUid']);
326
                                if ($document) {
327
                                    if ($metadataGroup->isPrimaryFileGroup()) {
328
                                        $file = $document->getPrimaryFile();
329
                                    } else {
330
                                        $file = $document->getFileByFileIdentifier($fileIdentifier);
331
                                    }
332
                                }
333
334
                                if ($metadataObject->isUploadField() ) {
335
                                    if ($object &&
336
                                        array_key_exists('error', $object) &&
337
                                        $object['error'] != UPLOAD_ERR_NO_FILE)
338
                                    {
339
                                        if (empty($file)) {
340
                                            $file = $this->objectManager->get(File::class);
341
                                            $file = $this->getUploadedFile(
342
                                                $object,
343
                                                $metadataGroup->isPrimaryFileGroup(),
344
                                                $file);
345
                                            $documentFormField->setFile($file);
0 ignored issues
show
Bug introduced by
The method setFile() does not exist on EWW\Dpf\Domain\Model\DocumentForm. Did you maybe mean setFiles()? ( Ignorable by Annotation )

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

345
                                            $documentFormField->/** @scrutinizer ignore-call */ 
346
                                                                setFile($file);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
346
                                        } else {
347
                                            $file = $this->getUploadedFile(
348
                                                $object,
349
                                                $metadataGroup->isPrimaryFileGroup(),
350
                                                $file);
351
                                            $documentFormField->setFile($file);
352
                                        }
353
354
                                        $documentFormField->setValue($file->getUrl());
355
356
                                    } elseif ($object && !array_key_exists('error', $object)) {
357
                                        $documentFormField->setFile($file);
358
                                    }
359
360
                                    if ($file) {
361
                                        $fileIdentifier = $file->getFileIdentifier();
362
                                        $documentForm->addFile($file);
363
                                    }
364
                                } else {
365
                                    if ($metadataObject->isFileLabelField()) {
366
                                        $fileLabel = $object;
367
                                    }
368
369
                                    if ($metadataObject->isFileDownloadField()) {
370
                                        $fileDownload = !empty($object);
371
                                    }
372
373
                                    if ($metadataObject->isFileArchiveField()) {
374
                                        $fileArchive = !empty($object);
375
                                    }
376
377
                                }
378
379
                                if ($file) {
380
                                    $file->setLabel($fileLabel);
381
                                    $file->setDownload($fileDownload);
382
                                    $file->setArchive($fileArchive);
383
                                }
384
                            }
385
                        }
386
                    }
387
388
                    if (!$metadataGroup->isFileGroup() || $fileIdentifier) {
389
                        $documentFormPage->addItem($documentFormGroup);
390
                    }
391
                }
392
            }
393
394
            $documentForm->addItem($documentFormPage);
395
        }
396
397
        return $documentForm;
398
    }
399
}
400