CompletePurchaseRequest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 9
dl 0
loc 55
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getData() 0 10 2
A sendData() 0 4 1
A getVerificationHash() 0 5 1
A mergeResponse() 0 7 2
A queryGateway() 0 20 2
1
<?php
2
namespace Omnipay\Gtpay\Message;
3
4
use Omnipay\Common\Exception\InvalidRequestException;
5
use Omnipay\Common\Exception\InvalidResponseException;
6
7
class CompletePurchaseRequest extends AbstractRequest
8
{
9
10 11
    public function getData()
11
    {
12 11
        $data = $this->httpRequest->request->all();
13
14 11
        if (empty($data)) {
15 1
            throw new InvalidRequestException('No Request Body Found');
16
        }
17
18 10
        return $this->mergeResponse($this->queryGateway(), $data);
19
    }
20
21 10
    public function sendData($data)
22
    {
23 10
        return new CompletePurchaseResponse($this, $data);
24
    }
25
26
27 10
    public function queryGateway()
28
    {
29
        $param = [
30 10
            'tranxid' => $this->getTransactionId(),
31 10
            'amount' => $this->getAmountInteger(),
32 10
            'mertid' => $this->getMerchantId(),
33 10
            'hash'=>$this->getVerificationHash()
34
        ];
35 10
        $response = $this->httpClient->get(
36 10
            $this->getWebserviceUrl(),
37 10
            null,
38 10
            ['query'=>$param,'read_timeout'=>60]
39 10
        )->send();
40
41 10
        if ($response->getStatusCode() !== 200) {
42
            throw new InvalidResponseException();
43
        }
44 10
        $body = (string) $response->getBody();
45 10
        return json_decode($body, true);
46
    }
47
48 10
    private function getVerificationHash()
49
    {
50 10
        $hashString = $this->getMerchantId() . $this->getTransactionId() . $this->getHashKey();
51 10
        return hash('sha512', $hashString);
52
    }
53
54 10
    private function mergeResponse($newResponse, $data)
55
    {
56 10
        if (empty($data)) {
57
            return $newResponse;
58
        }
59 10
        return array_merge($data, $newResponse);
60
    }
61
}
62