Passed
Pull Request — master (#132)
by
unknown
15:02 queued 06:44
created

AbstractDocumentFormController   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 368
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 142
dl 0
loc 368
rs 9.1199
c 0
b 0
f 0
wmc 41

13 Methods

Rating   Name   Duplication   Size   Complexity  
B listAction() 0 40 6
A newAction() 0 3 1
A initializeCreateAction() 0 26 4
A initializeNewAction() 0 20 4
A showAction() 0 4 1
B createAction() 0 49 7
A editAction() 0 3 1
A initializeAction() 0 3 1
A initializeEditAction() 0 16 3
A initializeUpdateAction() 0 25 4
B updateAction() 0 47 7
A cancelAction() 0 3 1
A redirectToList() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like AbstractDocumentFormController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use AbstractDocumentFormController, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace EWW\Dpf\Controller;
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\Document;
18
use EWW\Dpf\Services\Email\Notifier;
19
use EWW\Dpf\Services\Transfer\ElasticsearchRepository;
20
use EWW\Dpf\Helper\DocumentMapper;
21
use EWW\Dpf\Helper\ElasticsearchMapper;
22
use EWW\Dpf\Helper\FormDataReader;
23
24
/**
25
 * DocumentFormController
26
 */
27
abstract class AbstractDocumentFormController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
28
{
29
30
    /**
31
     * documentRepository
32
     *
33
     * @var \EWW\Dpf\Domain\Repository\DocumentRepository
34
     * @inject
35
     */
36
    protected $documentRepository = null;
37
38
    /**
39
     * fileRepository
40
     *
41
     * @var \EWW\Dpf\Domain\Repository\FileRepository
42
     * @inject
43
     */
44
    protected $fileRepository = null;
45
46
    /**
47
     * documentTypeRepository
48
     *
49
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
50
     * @inject
51
     */
52
    protected $documentTypeRepository = null;
53
54
    /**
55
     * metadataGroupRepository
56
     *
57
     * @var \EWW\Dpf\Domain\Repository\MetadataGroupRepository
58
     * @inject
59
     */
60
    protected $metadataGroupRepository = null;
61
62
    /**
63
     * metadataObjectRepository
64
     *
65
     * @var \EWW\Dpf\Domain\Repository\MetadataObjectRepository
66
     * @inject
67
     */
68
    protected $metadataObjectRepository = null;
69
70
    /**
71
     * persistence manager
72
     *
73
     * @var \TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface
74
     * @inject
75
     */
76
    protected $persistenceManager;
77
78
    /**
79
     * action list
80
     *
81
     * @return void
82
     */
83
    public function listAction()
84
    {
85
86
        $documents = $this->documentRepository->findAll();
87
88
        $documentTypes = $this->documentTypeRepository->findAll();
89
90
        $data = array();
91
        $docTypes = array();
92
        $name = array();
93
        $type = array();
94
95
        foreach ($documentTypes as $docType) {
96
            $data[] = array(
97
                "name" => $docType->getDisplayName(),
98
                "type" => $docType,
99
            );
100
        }
101
102
        foreach ($data as $key => $row) {
103
            $name[$key] = $row['name'];
104
            $type[$key] = $row['type'];
105
        }
106
107
        array_multisort($name, SORT_ASC, SORT_LOCALE_STRING, $type, SORT_ASC, $data);
108
109
        foreach ($data as $item) {
110
            $docTypes[] = $item['type'];
111
        }
112
113
        if ($this->request->hasArgument('message')) {
114
            $this->view->assign('message', $this->request->getArgument('message'));
115
        }
116
117
        if ($this->request->hasArgument('errorFiles')) {
118
            $this->view->assign('errorFiles', $this->request->getArgument('errorFiles'));
119
        }
120
121
        $this->view->assign('documentTypes', $docTypes);
122
        $this->view->assign('documents', $documents);
123
    }
124
125
    /**
126
     * action show
127
     *
128
     * @param \EWW\Dpf\Domain\Model\Document $document
129
     * @return void
130
     */
131
    public function showAction(\EWW\Dpf\Domain\Model\Document $document)
132
    {
133
134
        $this->view->assign('document', $document);
135
    }
136
137
    /**
138
     * initialize newAction
139
     *
140
     * @return void
141
     */
142
    public function initializeNewAction()
143
    {
144
145
        $requestArguments = $this->request->getArguments();
146
147
        if (array_key_exists('documentData', $requestArguments)) {
148
            die('Error: initializeNewAction');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
149
        } elseif (array_key_exists('documentType', $requestArguments)) {
150
            $docTypeUid   = $this->request->getArgument('documentType');
151
            $documentType = $this->documentTypeRepository->findByUid($docTypeUid);
0 ignored issues
show
Bug introduced by
$docTypeUid of type string is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Extbase\Persis...Repository::findByUid(). ( Ignorable by Annotation )

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

151
            $documentType = $this->documentTypeRepository->findByUid(/** @scrutinizer ignore-type */ $docTypeUid);
Loading history...
152
            $document     = $this->objectManager->get(Document::class);
153
            $document->setDocumentType($documentType);
154
            $mapper  = $this->objectManager->get(DocumentMapper::class);
155
            $docForm = $mapper->getDocumentForm($document);
156
        } elseif (array_key_exists('newDocumentForm', $requestArguments)) {
157
            $docForm = $this->request->getArgument('newDocumentForm');
158
        }
159
160
        $requestArguments['newDocumentForm'] = $docForm;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $docForm does not seem to be defined for all execution paths leading up to this point.
Loading history...
161
        $this->request->setArguments($requestArguments);
162
    }
163
164
    /**
165
     * action new
166
     *
167
     * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm
168
     * @ignorevalidation $newDocumentForm
169
     * @return void
170
     */
171
    public function newAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm = null)
172
    {
173
        $this->view->assign('documentForm', $newDocumentForm);
174
    }
175
176
    public function initializeCreateAction()
177
    {
178
179
        $requestArguments = $this->request->getArguments();
180
181
        if ($this->request->hasArgument('documentData')) {
182
            $documentData = $this->request->getArgument('documentData');
183
184
            $formDataReader = $this->objectManager->get(FormDataReader::class);
185
            $formDataReader->setFormData($documentData);
186
187
            $docForm                             = $formDataReader->getDocumentForm();
188
            $requestArguments['newDocumentForm'] = $docForm;
189
190
            $docTypeUid = $documentData['type'];
191
            $documentType = $this->documentTypeRepository->findByUid($docTypeUid);
192
            $virtual = $documentType->getVirtual();
193
194
            if (!$formDataReader->uploadError() || $virtual === true) {
195
                $this->request->setArguments($requestArguments);
196
            } else {
197
                $t = $docForm->getNewFileNames();
198
                $this->redirect('list', 'DocumentForm', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t));
199
            }
200
        } else {
201
            $this->redirectToList("UPLOAD_POST_SIZE_ERROR");
202
        }
203
    }
204
205
    /**
206
     * action create
207
     *
208
     * @param \EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm
209
     * @return void
210
     */
211
    public function createAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm)
212
    {
213
214
        $documentMapper = $this->objectManager->get(DocumentMapper::class);
215
        $newDocument    = $documentMapper->getDocument($newDocumentForm);
216
217
        // xml data fields are limited to 64 KB
218
        if (strlen($newDocument->getXmlData()) >= 64 * 1024 || strlen($newDocument->getSlubInfoData() >= 64 * 1024)) {
219
            throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded.");
220
        }
221
222
        $this->documentRepository->add($newDocument);
223
        $this->persistenceManager->persistAll();
224
225
        $newDocument = $this->documentRepository->findByUid($newDocument->getUid());
226
        $this->persistenceManager->persistAll();
227
228
        // Add or update files
229
        $newFiles = $newDocumentForm->getNewFiles();
230
231
        if (is_array($newFiles)) {
0 ignored issues
show
introduced by
The condition is_array($newFiles) is always true.
Loading history...
232
            foreach ($newFiles as $newFile) {
233
234
                if ($newFile->getUID()) {
235
                    $this->fileRepository->update($newFile);
236
                } else {
237
                    $newFile->setDocument($newDocument);
238
                    $this->fileRepository->add($newFile);
239
                }
240
            }
241
        }
242
243
        $notifier = $this->objectManager->get(Notifier::class);
244
245
        $notifier->sendNewDocumentNotification($newDocument);
246
247
        $requestArguments = $this->request->getArguments();
248
249
        if (array_key_exists('savecontinue', $requestArguments)) {
250
251
            $tmpDocument = $this->objectManager->get(Document::class);
252
253
            $tmpDocument->setTitle($newDocument->getTitle());
254
            $tmpDocument->setAuthors($newDocument->getAuthors());
255
            $tmpDocument->setXmlData($newDocument->getXmlData());
256
            $tmpDocument->setSlubInfoData($newDocument->getSlubInfoData());
257
            $tmpDocument->setDocumentType($newDocument->getDocumentType());
258
259
            $this->forward('new', null, null, array('newDocumentForm' => $documentMapper->getDocumentForm($tmpDocument)));
260
        }
261
262
    }
263
264
    public function initializeEditAction()
265
    {
266
267
        $requestArguments = $this->request->getArguments();
268
269
        if (array_key_exists('document', $requestArguments)) {
270
            $documentUid  = $this->request->getArgument('document');
271
            $document     = $this->documentRepository->findByUid($documentUid);
0 ignored issues
show
Bug introduced by
$documentUid of type string is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Extbase\Persis...Repository::findByUid(). ( Ignorable by Annotation )

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

271
            $document     = $this->documentRepository->findByUid(/** @scrutinizer ignore-type */ $documentUid);
Loading history...
272
            $mapper       = $this->objectManager->get(DocumentMapper::class);
273
            $documentForm = $mapper->getDocumentForm($document);
274
        } elseif (array_key_exists('documentForm', $requestArguments)) {
275
            $documentForm = $this->request->getArgument('documentForm');
276
        }
277
278
        $requestArguments['documentForm'] = $documentForm;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $documentForm does not seem to be defined for all execution paths leading up to this point.
Loading history...
279
        $this->request->setArguments($requestArguments);
280
    }
281
282
    /**
283
     * action edit
284
     *
285
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
286
     * @ignorevalidation $documentForm
287
     * @return void
288
     */
289
    public function editAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm)
290
    {
291
        $this->view->assign('documentForm', $documentForm);
292
    }
293
294
    public function initializeUpdateAction()
295
    {
296
        $requestArguments = $this->request->getArguments();
297
298
        if ($this->request->hasArgument('documentData')) {
299
            $documentData = $this->request->getArgument('documentData');
300
301
            $formDataReader = $this->objectManager->get(FormDataReader::class);
302
            $formDataReader->setFormData($documentData);
303
            $docForm = $formDataReader->getDocumentForm();
304
305
            $requestArguments['documentForm'] = $docForm;
306
307
            $docTypeUid = $documentData['type'];
308
            $documentType = $this->documentTypeRepository->findByUid($docTypeUid);
309
            $virtual = $documentType->getVirtual();
310
311
            if (!$formDataReader->uploadError() || $virtual === true) {
312
                $this->request->setArguments($requestArguments);
313
            } else {
314
                $t = $docForm->getNewFileNames();
315
                $this->redirect('list', 'Document', null, array('message' => 'UPLOAD_MAX_FILESIZE_ERROR', 'errorFiles' => $t));
316
            }
317
        } else {
318
            $this->redirectToList("UPLOAD_POST_SIZE_ERROR");
319
        }
320
    }
321
322
    /**
323
     * action update
324
     *
325
     * @param \EWW\Dpf\Domain\Model\DocumentForm $documentForm
326
     * @return void
327
     */
328
    public function updateAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm)
329
    {
330
331
        $requestArguments = $this->request->getArguments();
332
333
        $documentMapper = $this->objectManager->get(DocumentMapper::class);
334
        $updateDocument = $documentMapper->getDocument($documentForm);
335
336
        // xml data fields are limited to 64 KB
337
        if (strlen($updateDocument->getXmlData()) >= 64 * 1024 || strlen($updateDocument->getSlubInfoData() >= 64 * 1024)) {
338
            throw new \EWW\Dpf\Exceptions\DocumentMaxSizeErrorException("Maximum document size exceeded.");
339
        }
340
341
        // add document to local es index
342
        $elasticsearchMapper = $this->objectManager->get(ElasticsearchMapper::class);
343
        $json                = $elasticsearchMapper->getElasticsearchJson($updateDocument);
344
345
        $elasticsearchRepository = $this->objectManager->get(ElasticsearchRepository::class);
346
        // send document to index
347
        $elasticsearchRepository->add($updateDocument, $json);
348
349
        $updateDocument->setChanged(true);
350
351
        $this->documentRepository->update($updateDocument);
352
353
        // Delete files
354
        foreach ($documentForm->getDeletedFiles() as $deleteFile) {
355
            $deleteFile->setStatus(\EWW\Dpf\Domain\Model\File::STATUS_DELETED);
356
            $this->fileRepository->update($deleteFile);
357
        }
358
359
        // Add or update files
360
        foreach ($documentForm->getNewFiles() as $newFile) {
361
362
            if ($newFile->getUID()) {
363
                $this->fileRepository->update($newFile);
364
            } else {
365
                $updateDocument->addFile($newFile);
366
            }
367
368
        }
369
370
        if (array_key_exists('savecontinue', $requestArguments)) {
371
            $this->forward('edit', null, null, array('documentForm' => $documentForm));
372
        }
373
374
        $this->redirectToList();
375
    }
376
377
    /**
378
     * action cancel
379
     *
380
     * @return void
381
     */
382
    public function cancelAction()
383
    {
384
        $this->redirectToList();
385
    }
386
387
    public function initializeAction()
388
    {
389
        parent::initializeAction();
390
    }
391
392
    protected function redirectToList($message = null)
0 ignored issues
show
Unused Code introduced by
The parameter $message 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

392
    protected function redirectToList(/** @scrutinizer ignore-unused */ $message = null)

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...
393
    {
394
        $this->redirect('list');
395
    }
396
397
}
398