BaseResponseProcessor::processCommonResponse()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 18
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
namespace Paranoia\Processor\Posnet;
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, 'respCode')) {
18
            $response->setResponseCode((string)$xml->respCode);
0 ignored issues
show
Bug introduced by
(string)$xml->respCode 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

18
            $response->setResponseCode(/** @scrutinizer ignore-type */ (string)$xml->respCode);
Loading history...
19
        }
20
        if (property_exists($xml, 'respText')) {
21
            $errorMessages[] = sprintf('%s ', (string)$xml->respText);
22
        }
23
        $errorMessage = implode(' ', $errorMessages);
24
        $response->setResponseMessage($errorMessage);
25
    }
26
27
    /**
28
     * @param \SimpleXMLElement $xml
29
     * @param Response $response
30
     */
31
    private function prepareTransactionDetails(\SimpleXMLElement $xml, Response $response)
32
    {
33
        if (property_exists($xml, 'orderId')) {
34
            $response->setOrderId((string)$xml->orderId);
35
        }
36
        $response->setTransactionId((string)$xml->hostlogkey);
37
        if (property_exists($xml, 'authCode')) {
38
            $response->setAuthCode((string)$xml->authCode);
39
        }
40
    }
41
42
    /**
43
     * @param $rawResponse
44
     * @return Response
45
     * @throws BadResponseException
46
     */
47
    protected function processCommonResponse($rawResponse)
48
    {
49
        try {
50
            /** @var \SimpleXMLElement $xml */
51
            $xml = new \SimpleXmlElement($rawResponse);
52
        } catch (\Exception $e) {
53
            $exception = new BadResponseException('Provider returned unexpected response: ' . $rawResponse);
54
            throw $exception;
55
        }
56
        $this->validateResponse($xml);
57
        $response = new Response();
58
        $response->setIsSuccess((int)$xml->approved > 0);
59
        if (!$response->isSuccess()) {
60
            $this->prepareErrorDetails($xml, $response);
61
        } else {
62
            $this->prepareTransactionDetails($xml, $response);
63
        }
64
        return $response;
65
    }
66
67
    protected function validateResponse($transformedResponse)
68
    {
69
        //TODO: response validation should implemented.
70
        return true;
71
    }
72
}
73