BaseResponseProcessor::prepareTransactionDetails()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
rs 9.2222
c 0
b 0
f 0
cc 6
nc 10
nop 2
1
<?php
2
namespace Paranoia\Processor\Gvp;
3
4
use Paranoia\Exception\BadResponseException;
5
use Paranoia\Processor\AbstractResponseProcessor;
6
use Paranoia\Response;
7
8
abstract class BaseResponseProcessor extends AbstractResponseProcessor
9
{
10
    /**
11
     * @param \SimpleXMLElement $xml
12
     * @param Response $response
13
     */
14
    private function prepareErrorDetails(\SimpleXMLElement $xml, Response $response)
15
    {
16
        $errorMessages = array();
17
        if (property_exists($xml->Transaction->Response, 'ErrorMsg')) {
18
            $errorMessages[] = sprintf(
19
                'Error Message: %s',
20
                (string)$xml->Transaction->Response->ErrorMsg
21
            );
22
        }
23
        if (property_exists($xml->Transaction->Response, 'SysErrMsg')) {
24
            $errorMessages[] = sprintf(
25
                'System Error Message: %s',
26
                (string)$xml->Transaction->Response->SysErrMsg
27
            );
28
        }
29
        $errorMessage = implode(' ', $errorMessages);
30
        $response->setResponseMessage($errorMessage);
31
    }
32
33
    /**
34
     * @param \SimpleXMLElement $xml
35
     * @param Response $response
36
     */
37
    private function prepareTransactionDetails(\SimpleXMLElement $xml, Response $response)
38
    {
39
        if (property_exists($xml, 'Order') && property_exists($xml->Order, 'OrderID')) {
40
            $response->setOrderId((string)$xml->Order->OrderID);
41
        }
42
43
        if (property_exists($xml, 'Transaction')) {
44
            if (property_exists($xml->Transaction, 'RetrefNum')) {
45
                $response->setTransactionId((string)$xml->Transaction->RetrefNum);
46
            }
47
48
            if (property_exists($xml->Transaction, 'AuthCode')) {
49
                $response->setAuthCode((string)$xml->Transaction->AuthCode);
50
            }
51
        }
52
    }
53
54
    /**
55
     * @param $rawResponse
56
     * @return Response
57
     * @throws BadResponseException
58
     */
59
    protected function processCommonResponse($rawResponse)
60
    {
61
        $response = new Response();
62
        try {
63
            /** @var \SimpleXMLElement $xml */
64
            $xml = new \SimpleXmlElement($rawResponse);
65
        } catch (\Exception $e) {
66
            $exception = new BadResponseException('Provider returned unexpected response: ' . $rawResponse);
67
            throw $exception;
68
        }
69
        $this->validateResponse($xml);
70
        $response->setIsSuccess('00' == (string)$xml->Transaction->Response->Code);
71
        $response->setResponseCode((string)$xml->Transaction->Response->ReasonCode);
0 ignored issues
show
Bug introduced by
(string)$xml->Transaction->Response->ReasonCode of type string is incompatible with the type integer expected by parameter $responseCode of Paranoia\Response::setResponseCode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $response->setResponseCode(/** @scrutinizer ignore-type */ (string)$xml->Transaction->Response->ReasonCode);
Loading history...
72
        if (!$response->isSuccess()) {
73
            $this->prepareErrorDetails($xml, $response);
74
        } else {
75
            $this->prepareTransactionDetails($xml, $response);
76
        }
77
78
        return $response;
79
    }
80
81
    protected function validateResponse($transformedResponse)
82
    {
83
        //TODO: response validation should implemented.
84
        return true;
85
    }
86
}
87