BaseResponseProcessor   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 32
dl 0
loc 69
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A validateResponse() 0 4 1
A prepareErrorDetails() 0 20 5
A processCommonResponse() 0 19 3
A prepareTransactionDetails() 0 5 1
1
<?php
2
namespace Paranoia\Processor\NestPay;
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, 'Error')) {
18
            $errorMessages[] = sprintf('Error: %s', (string)$xml->Error);
19
        }
20
        if (property_exists($xml, 'ErrMsg')) {
21
            $errorMessages[] = sprintf(
22
                'Error Message: %s ',
23
                (string)$xml->ErrMsg
24
            );
25
        }
26
        if (property_exists($xml, 'Extra') && property_exists($xml->Extra, 'HOSTMSG')) {
27
            $errorMessages[] = sprintf(
28
                'Host Message: %s',
29
                (string)$xml->Extra->HOSTMSG
30
            );
31
        }
32
        $errorMessage = implode(' ', $errorMessages);
33
        $response->setResponseMessage($errorMessage);
34
    }
35
36
    /**
37
     * @param \SimpleXMLElement $xml
38
     * @param Response $response
39
     */
40
    private function prepareTransactionDetails(\SimpleXMLElement $xml, Response $response)
41
    {
42
        $response->setOrderId((string)$xml->OrderId);
43
        $response->setTransactionId((string)$xml->TransId);
44
        $response->setAuthCode((string) $xml->AuthCode);
45
    }
46
47
    /**
48
     * @param $rawResponse
49
     * @return Response
50
     * @throws BadResponseException
51
     */
52
    protected function processCommonResponse($rawResponse)
53
    {
54
        try {
55
            /** @var \SimpleXMLElement $xml */
56
            $xml = new \SimpleXmlElement($rawResponse);
57
        } catch (\Exception $e) {
58
            $exception = new BadResponseException('Provider returned unexpected response: ' . $rawResponse);
59
            throw $exception;
60
        }
61
        $this->validateResponse($xml);
62
        $response = new Response();
63
        $response->setIsSuccess((string)$xml->Response == 'Approved');
64
        $response->setResponseCode((string)$xml->ProcReturnCode);
0 ignored issues
show
Bug introduced by
(string)$xml->ProcReturnCode 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

64
        $response->setResponseCode(/** @scrutinizer ignore-type */ (string)$xml->ProcReturnCode);
Loading history...
65
        if (!$response->isSuccess()) {
66
            $this->prepareErrorDetails($xml, $response);
67
        } else {
68
            $this->prepareTransactionDetails($xml, $response);
69
        }
70
        return $response;
71
    }
72
73
    protected function validateResponse($transformedResponse)
74
    {
75
        //TODO: response validation should implemented.
76
        return true;
77
    }
78
}
79