Passed
Push — master ( 87cf99...8ae428 )
by Ralf
08:45
created

DocumentFormBEController::updateAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 1
dl 0
loc 29
rs 9.7
c 0
b 0
f 0
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\Services\Transfer\ElasticsearchRepository;
18
use EWW\Dpf\Exceptions\DPFExceptionInterface;
19
20
class DocumentFormBEController extends AbstractDocumentFormController
21
{
22
23
    public function __construct()
24
    {
25
        parent::__construct();
26
27
    }
28
29
    protected function redirectToList($message = null)
30
    {
31
        $this->redirect('list', 'Document', null, array('message' => $message));
32
    }
33
34
    /**
35
     * action delete
36
     *
37
     * @param array $documentData
38
     * @throws \Exception
39
     */
40
    public function deleteAction($documentData)
41
    {
42
        if (!$GLOBALS['BE_USER']) {
43
            throw new \Exception('Access denied');
44
        }
45
46
        try {
47
48
            $document = $this->documentRepository->findByUid($documentData['documentUid']);
49
50
            $elasticsearchRepository = $this->objectManager->get(ElasticsearchRepository::class);
51
            // send document to index
52
            $elasticsearchRepository->delete($document, "");
53
54
            $document->setState(\EWW\Dpf\Domain\Model\Document::OBJECT_STATE_LOCALLY_DELETED);
55
            $this->documentRepository->update($document);
56
57
            $this->redirectToList();
58
59
        } catch (\Exception $exception) {
60
61
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
62
63
            if ($exception instanceof DPFExceptionInterface) {
64
                $key = $exception->messageLanguageKey();
65
            } else {
66
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
67
            }
68
69
            $document = $this->documentRepository->findByUid($documentData['documentUid']);
70
            $message[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$message was never initialized. Although not strictly required by PHP, it is generally a good practice to add $message = array(); before regardless.
Loading history...
71
                'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_delete.failure',
72
                'dpf',
73
                array($document->getTitle())
74
            );
75
76
            $message[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf');
77
            $this->addFlashMessage(implode(" ", $message), '', $severity,true);
78
79
            $this->forward('edit', DocumentFormBE, null, array('document' => $document));
0 ignored issues
show
Bug introduced by
The constant EWW\Dpf\Controller\DocumentFormBE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
80
        }
81
82
83
    }
84
85
    public function editAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm)
86
    {
87
        $document = $this->documentRepository->findByUid($documentForm->getDocumentUid());
88
        $this->view->assign('document', $document);
89
90
        parent::editAction($documentForm);
91
    }
92
93
    public function updateAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm)
94
    {
95
        try {
96
            parent::updateAction($documentForm);
97
        } catch (\Exception $exception) {
98
99
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
100
101
            if ($exception instanceof DPFExceptionInterface) {
102
                $key = $exception->messageLanguageKey();
103
            } else {
104
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:error.unexpected';
105
            }
106
107
            $documentMapper = $this->objectManager->get(\EWW\Dpf\Helper\DocumentMapper::class);
108
            $updateDocument = $documentMapper->getDocument($documentForm);
109
110
            $message[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
0 ignored issues
show
Comprehensibility Best Practice introduced by
$message was never initialized. Although not strictly required by PHP, it is generally a good practice to add $message = array(); before regardless.
Loading history...
111
                'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.failure',
112
                'dpf',
113
                array($updateDocument->getTitle())
114
            );
115
116
            $message[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf');
117
118
119
            $this->addFlashMessage(implode(" ", $message), '', $severity,true);
120
121
            $this->forward('edit', DocumentFormBE, null, array('document' => $updateDocument));
0 ignored issues
show
Bug introduced by
The constant EWW\Dpf\Controller\DocumentFormBE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
122
        }
123
    }
124
125
    public function createAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm)
126
    {
127
        parent::createAction($newDocumentForm);
128
        $this->redirectToList('CREATE_OK');
129
    }
130
131
}
132