Passed
Pull Request — master (#130)
by
unknown
08:07
created

DocumentFormBEController::updateAction()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 4
nop 1
dl 0
loc 31
rs 9.6333
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
19
class DocumentFormBEController extends AbstractDocumentFormController
20
{
21
22
    public function __construct()
23
    {
24
        parent::__construct();
25
26
    }
27
28
    protected function redirectToList($message = null)
29
    {
30
        $this->redirect('list', 'Document', null, array('message' => $message));
31
    }
32
33
    /**
34
     * action delete
35
     *
36
     * @param array $documentData
37
     * @throws \Exception
38
     */
39
    public function deleteAction($documentData)
40
    {
41
        if (!$GLOBALS['BE_USER']) {
42
            throw new \Exception('Access denied');
43
        }
44
45
        try {
46
47
            $document = $this->documentRepository->findByUid($documentData['documentUid']);
48
49
            $elasticsearchRepository = $this->objectManager->get(ElasticsearchRepository::class);
50
            // send document to index
51
            $elasticsearchRepository->delete($document, "");
52
53
            $document->setState(\EWW\Dpf\Domain\Model\Document::OBJECT_STATE_LOCALLY_DELETED);
54
            $this->documentRepository->update($document);
55
56
            $this->redirectToList();
57
58
        } catch (\Exception $exception) {
59
60
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
61
62
            if ($exception instanceof \EWW\Dpf\Exceptions\ConnectionErrorException) {
63
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_transfer.connection_error';
64
            } elseif ($exception instanceof \EWW\Dpf\Exceptions\ConnectionTimeoutErrorException) {
65
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_transfer.connection_timeout_error';
66
            } else {
67
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_transfer.unexpected_error';
68
            }
69
70
            $document = $this->documentRepository->findByUid($documentData['documentUid']);
71
            $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...
72
                'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_delete.failure',
73
                'dpf',
74
                array($document->getTitle())
75
            );
76
77
            $message[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf');
78
            $this->addFlashMessage(implode(" ", $message), '', $severity,true);
79
80
            $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...
81
        }
82
83
84
    }
85
86
    public function editAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm)
87
    {
88
        $document = $this->documentRepository->findByUid($documentForm->getDocumentUid());
89
        $this->view->assign('document', $document);
90
91
        parent::editAction($documentForm);
92
    }
93
94
    public function updateAction(\EWW\Dpf\Domain\Model\DocumentForm $documentForm)
95
    {
96
        try {
97
            parent::updateAction($documentForm);
98
        } catch (\Exception $exception) {
99
100
            $severity = \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR;
101
102
            if ($exception instanceof \EWW\Dpf\Exceptions\ConnectionErrorException) {
103
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_transfer.connection_error';
104
            } elseif ($exception instanceof \EWW\Dpf\Exceptions\ConnectionTimeoutErrorException) {
105
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_transfer.connection_timeout_error';
106
            } else {
107
                $key = 'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_transfer.unexpected_error';
108
            }
109
110
            $documentMapper = $this->objectManager->get(\EWW\Dpf\Helper\DocumentMapper::class);
111
            $updateDocument = $documentMapper->getDocument($documentForm);
112
113
            $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...
114
                'LLL:EXT:dpf/Resources/Private/Language/locallang.xlf:document_update.failure',
115
                'dpf',
116
                array($updateDocument->getTitle())
117
            );
118
119
            $message[] = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate($key, 'dpf');
120
121
122
            $this->addFlashMessage(implode(" ", $message), '', $severity,true);
123
124
            $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...
125
        }
126
    }
127
128
    public function createAction(\EWW\Dpf\Domain\Model\DocumentForm $newDocumentForm)
129
    {
130
        parent::createAction($newDocumentForm);
131
        $this->redirectToList('CREATE_OK');
132
    }
133
134
}
135