Passed
Pull Request — master (#195)
by
unknown
08:57
created

XSLTransformator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getTransformedOutputXML() 0 55 6
A transformInputXML() 0 45 3
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\Configuration\ClientConfigurationManager;
18
use EWW\Dpf\Domain\Repository\DocumentTypeRepository;
19
use EWW\Dpf\Services\Transformer\DocumentTransformer;
20
use TYPO3\CMS\Extbase\Object\ObjectManager;
21
use \TYPO3\CMS\Core\Utility\GeneralUtility;
22
use \TYPO3\CMS\Core\Log\Logger;
23
use \TYPO3\CMS\Core\Log\LogLevel;
24
use \TYPO3\CMS\Core\Log\LogManager;
25
26
class XSLTransformator
27
{
28
29
    /**
30
     * clientConfigurationManager
31
     *
32
     * @var \EWW\Dpf\Configuration\ClientConfigurationManager
33
     * @inject
34
     */
35
    protected $clientConfigurationManager;
36
37
    /**
38
     * documentTypeRepository
39
     *
40
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
41
     * @inject
42
     */
43
    protected $documentTypeRepository;
44
45
    /**
46
     * @param $xml
47
     * @return string
48
     */
49
    public function transformInputXML($xml) {
50
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class);
51
        $this->clientConfigurationManager = $objectManager->get(ClientConfigurationManager::class);
52
        $this->documentTypeRepository = $objectManager->get(DocumentTypeRepository::class);
53
54
        $docTypeInput = $this->clientConfigurationManager->getTypeXpathInput();
55
56
        $domDocument = new \DOMDocument();
57
        $domDocument->loadXML($xml);
58
59
        $domXPath = \EWW\Dpf\Helper\XPath::create($domDocument);
60
61
        $documentTypeName = $domXPath->query('//' . $docTypeInput)->item(0)->nodeValue;
62
63
        $documentType = $this->documentTypeRepository->findOneByName($documentTypeName);
64
65
        $transformationFile = $documentType->getTransformationFileInput()->current();
66
        if (!$transformationFile) {
67
            $transformationFile = $this->clientConfigurationManager->getInputTransformation();
68
        }
69
70
        if ($transformationFile != NULL) {
71
            $filePath = $transformationFile->getFile()->getOriginalResource()->getIdentifier();
72
            $documentTransformer = new DocumentTransformer();
73
74
            $transformedXml = $documentTransformer->transform(PATH_site . 'fileadmin' . $filePath, $xml);
75
        } else {
76
            // return generated xml if no transformation file is present
77
            $transformedXml = $xml;
78
79
            /** @var $logger Logger */
80
            $logger = GeneralUtility::makeInstance(
81
                LogManager::class)->getLogger(__CLASS__
82
            );
83
84
            $logger->log(
85
                LogLevel::WARNING,
86
                "Input XML: No transformation file is present. The given xml data was taken over as it is",
87
                array(
88
                    'documentTypeName' => $documentTypeName
89
                )
90
            );
91
        }
92
93
        return $transformedXml;
94
    }
95
96
    /**
97
     * @param \EWW\Dpf\Domain\Model\Document $document
98
     * @return string The transformed xml
99
     */
100
    public function getTransformedOutputXML($document, $xmlData = false)
101
    {
102
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class);
103
        $this->clientConfigurationManager = $objectManager->get(ClientConfigurationManager::class);
104
105
        $ownerId = $this->clientConfigurationManager->getOwnerId();
106
        $documentType = $document->getDocumentType();
107
108
        $transformationFile = $documentType->getTransformationFileOutput()->toArray()[0];
109
        if (!$transformationFile) {
110
            $transformationFile = $this->clientConfigurationManager->getOutputTransformation();
111
        }
112
113
        if ($transformationFile != NULL) {
114
            $filePath = $transformationFile->getFile()->getOriginalResource()->getIdentifier();
115
            $documentTransformer = new DocumentTransformer();
116
117
            if ( !$document->getRemoteState() || $document->getRemoteState() == 'NONE' ) {
118
                $remoteState = 'ACTIVE';
119
            } else {
120
                $remoteState = $document->getRemoteState();
121
            }
122
123
            $transformParams = [
124
                'record_state' => $remoteState,
125
                'agent_name' => $ownerId,
126
                'document_type' => $document->getDocumentType()->getName(),
127
                'process_number' => $document->getProcessNumber()
128
            ];
129
130
            if ($xmlData) {
131
                $transformedXml = $documentTransformer->transform(PATH_site . 'fileadmin' . $filePath, $xmlData, $transformParams);
132
            } else {
133
                $transformedXml = $documentTransformer->transform(PATH_site . 'fileadmin' . $filePath, $document->getXmlData(), $transformParams);
134
            }
135
136
        } else {
137
            // return generated xml if no transformation file is present
138
            $transformedXml = $document->getXmlData();
139
140
            /** @var $logger Logger */
141
            $logger = GeneralUtility::makeInstance(
142
                LogManager::class)->getLogger(__CLASS__
143
            );
144
145
            $logger->log(
146
                LogLevel::WARNING,
147
                "Output XML: No transformation file is present. The generated xml data was taken over as it is",
148
                array(
149
                    'documentTypeName' => $documentTypeName
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $documentTypeName does not exist. Did you maybe mean $document?
Loading history...
150
                )
151
            );
152
        }
153
154
        return $transformedXml;
155
    }
156
157
}
158