NovaApiSoapErrorParser::getSoapErrors()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 65
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 43
c 1
b 0
f 0
nc 5
nop 2
dl 0
loc 65
ccs 0
cts 43
cp 0
crap 42
rs 8.6097

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace OrcaServices\NovaApi\Parser;
4
5
use DOMNode;
6
use OrcaServices\NovaApi\Exception\InvalidXmlException;
7
use OrcaServices\NovaApi\Result\NovaApiErrorList;
8
use OrcaServices\NovaApi\Xml\XmlDocument;
9
10
/**
11
 * Error parser.
12
 */
13
final class NovaApiSoapErrorParser
14
{
15
    /**
16
     * Get SOAP errors.
17
     *
18
     * @param XmlDocument $xml The xml document
19
     * @param NovaApiErrorList $errors The errors
20
     *
21
     * @throws InvalidXmlException
22
     *
23
     * @return NovaApiErrorList The error messages
24
     */
25
    public function getSoapErrors(XmlDocument $xml, NovaApiErrorList $errors): NovaApiErrorList
26
    {
27
        $xml = $xml->withoutNamespaces();
28
        $xmlContent = $xml->getXml();
29
30
        // Spring validation errors
31
        if (strpos($xmlContent, 'ValidationError')) {
32
            return $this->getSoapValidationErrors($xml);
33
        }
34
35
        // Root node
36
        $faultNodeList = $xml->queryNodes('/Envelope/Body/Fault');
37
38
        $faultNode = $faultNodeList->item(0);
39
        if ($faultNode === null) {
40
            throw new InvalidXmlException('SOAP fault node not found');
41
        }
42
43
        $faultCode = $xml->getNodeValue('faultcode', $faultNode);
44
        $faultString = $xml->getNodeValue('faultstring', $faultNode);
45
46
        $errors = $errors->withError($faultCode, $faultString);
47
48
        $errorInfoNode = $xml->queryFirstNode('detail/errorInfo', $faultNode);
49
        $errorCode = $xml->getNodeValue('error-code', $errorInfoNode);
50
        $errorHeaders = $xml->getNodeValue('error-headers', $errorInfoNode);
51
        $errorMessage = $xml->getNodeValue('error-message', $errorInfoNode);
52
        $errorProtocolReasonPhrase = $xml->getNodeValue('error-protocol-reason-phrase', $errorInfoNode);
53
        $errorProtocolResponse = $xml->getNodeValue('error-protocol-response', $errorInfoNode);
54
        $errorSubCode = $xml->getNodeValue('error-subcode', $errorInfoNode);
55
        $inputExtError = $xml->getNodeValue('input-ext-error', $errorInfoNode);
56
        $errorXProtocolResponse = $xml->getNodeValue('error-x-protocol-response', $errorInfoNode);
57
        $responseContent = $xml->getNodeValue('response-content', $errorInfoNode);
58
59
        $responseContentXml = null;
60
        $errorDetailMessage = null;
61
62
        // Parse xml in xml
63
        if (strpos($responseContent, '<?xml') === 0) {
64
            $xmlError = XmlDocument::createFromXmlString($responseContent);
65
            $responseContentXml = $xmlError->getXml();
66
67
            $faultNode = $xmlError->queryNodes('/Envelope/Body/Fault');
68
69
            if ($faultNode->length === 1) {
70
                $faultCode = $xml->getNodeValue('faultcode', $faultNode->item(0));
71
                $faultString = $xml->getNodeValue('faultstring', $faultNode->item(0));
72
                $faultDetail = $xml->getNodeValue('detail', $faultNode->item(0));
73
                $errorDetailMessage = sprintf('%s %s %s', $faultCode, $faultString, $faultDetail);
74
            }
75
        }
76
77
        $details = [
78
            'error_headers' => $errorHeaders,
79
            'error_message' => $errorMessage,
80
            'error_protocol_reason_phrase' => $errorProtocolReasonPhrase,
81
            'error_protocol_response' => $errorProtocolResponse,
82
            'error_subcode' => $errorSubCode,
83
            'input_ext_error' => $inputExtError,
84
            'error_x_protocol_response' => $errorXProtocolResponse,
85
            'response_content' => $responseContent,
86
            'response_content_xml' => $responseContentXml,
87
        ];
88
89
        return $errors->withError($errorCode, $errorDetailMessage ?: $errorMessage, $details);
90
    }
91
92
    /**
93
     * Get spring validation errors.
94
     *
95
     * @param XmlDocument $xml The xml document
96
     *
97
     * @return NovaApiErrorList the validation error messages and codes
98
     */
99
    private function getSoapValidationErrors(XmlDocument $xml): NovaApiErrorList
100
    {
101
        $errors = new NovaApiErrorList();
102
103
        $faultNodeList = $xml->queryNodes('//Fault');
104
105
        if ($faultNodeList->length === 0) {
106
            return $errors;
107
        }
108
109
        $faultNode = $faultNodeList->item(0);
110
        $faultCode = $xml->getNodeValue('faultcode', $faultNode);
111
        $faultString = $xml->getNodeValue('faultstring', $faultNode);
112
113
        $errors = $errors->withError($faultCode, $faultString);
114
115
        $errorList = $xml->queryNodes('//ValidationError');
116
117
        if ($errorList->length === 0) {
118
            return $errors;
119
        }
120
121
        /** @var DOMNode $errorDetail */
122
        foreach ($errorList as $errorDetail) {
123
            $errors = $errors->withError($errorDetail->localName ?? '', $errorDetail->nodeValue ?? '');
124
        }
125
126
        return $errors;
127
    }
128
}
129