Passed
Pull Request — master (#195)
by
unknown
19:15
created

XSLTransformator::getTransformedOutputXML()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 26
c 0
b 0
f 0
nc 10
nop 2
dl 0
loc 42
rs 8.8817
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
22
class XSLTransformator
23
{
24
25
    /**
26
     * clientConfigurationManager
27
     *
28
     * @var \EWW\Dpf\Configuration\ClientConfigurationManager
29
     * @inject
30
     */
31
    protected $clientConfigurationManager;
32
33
    /**
34
     * documentTypeRepository
35
     *
36
     * @var \EWW\Dpf\Domain\Repository\DocumentTypeRepository
37
     * @inject
38
     */
39
    protected $documentTypeRepository;
40
41
    /**
42
     * @param $xml
43
     * @return string
44
     */
45
    public function transformInputXML($xml) {
46
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class);
47
        $this->clientConfigurationManager = $objectManager->get(ClientConfigurationManager::class);
48
        $this->documentTypeRepository = $objectManager->get(DocumentTypeRepository::class);
49
50
        $docTypeInput = $this->clientConfigurationManager->getTypeXpathInput();
51
52
        $domDocument = new \DOMDocument();
53
        $domDocument->loadXML($xml);
54
55
        $domXPath = \EWW\Dpf\Helper\XPath::create($domDocument);
56
57
        $documentTypeName = $domXPath->query('//' . $docTypeInput)->item(0)->nodeValue;
58
59
        $documentType = $this->documentTypeRepository->findOneByName($documentTypeName);
60
61
        $transformationFile = $documentType->getTransformationFileInput()->current();
62
        if (!$transformationFile) {
63
            $transformationFile = $this->clientConfigurationManager->getInputTransformation();
64
        }
65
66
        if ($transformationFile != NULL) {
67
            $filePath = $transformationFile->getFile()->getOriginalResource()->getIdentifier();
68
            $documentTransformer = new DocumentTransformer();
69
70
            $transformedXml = $documentTransformer->transform(PATH_site . 'fileadmin' . $filePath, $xml);
71
        } else {
72
            // return generated xml if no transformation file is present
73
            $transformedXml = $xml;
74
        }
75
76
        return $transformedXml;
77
    }
78
79
    /**
80
     * @param \EWW\Dpf\Domain\Model\Document $document
81
     * @return string The transformed xml
82
     */
83
    public function getTransformedOutputXML($document, $xmlData = false)
84
    {
85
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class);
86
        $this->clientConfigurationManager = $objectManager->get(ClientConfigurationManager::class);
87
88
        $ownerId = $this->clientConfigurationManager->getOwnerId();
89
        $documentType = $document->getDocumentType();
90
91
        $transformationFile = $documentType->getTransformationFileOutput()->toArray()[0];
92
        if (!$transformationFile) {
93
            $transformationFile = $this->clientConfigurationManager->getOutputTransformation();
94
        }
95
96
        if ($transformationFile != NULL) {
97
            $filePath = $transformationFile->getFile()->getOriginalResource()->getIdentifier();
98
            $documentTransformer = new DocumentTransformer();
99
100
            if ( !$document->getRemoteState() || $document->getRemoteState() == 'NONE' ) {
101
                $remoteState = 'ACTIVE';
102
            } else {
103
                $remoteState = $document->getRemoteState();
104
            }
105
106
            $transformParams = [
107
                'record_state' => $remoteState,
108
                'agent_name' => $ownerId,
109
                'document_type' => $document->getDocumentType()->getName(),
110
                'process_number' => $document->getProcessNumber()
111
            ];
112
113
            if ($xmlData) {
114
                $transformedXml = $documentTransformer->transform(PATH_site . 'fileadmin' . $filePath, $xmlData, $transformParams);
115
            } else {
116
                $transformedXml = $documentTransformer->transform(PATH_site . 'fileadmin' . $filePath, $document->getXmlData(), $transformParams);
117
            }
118
119
        } else {
120
            // return generated xml if no transformation file is present
121
            $transformedXml = $document->getXmlData();
122
        }
123
124
        return $transformedXml;
125
    }
126
127
}
128