Completed
Push — master ( f4874b...68ecfb )
by recca
07:51 queued 58s
created

Api   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 6
dl 0
loc 125
ccs 32
cts 33
cp 0.9697
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
getApiEndpoint() 0 1 ?
createTransaction() 0 1 ?
A __construct() 0 7 2
B doRequest() 0 19 5
A verifyHash() 0 18 2
A calculateHash() 0 6 1
1
<?php
2
3
namespace PayumTW\Collect;
4
5
use Http\Message\MessageFactory;
6
use Payum\Core\HttpClientInterface;
7
use Payum\Core\Exception\Http\HttpException;
8
9
abstract class Api
10
{
11
    /**
12
     * TIMEZONE.
13
     *
14
     * @var string
15
     */
16
    const TIMEZONE = 'Asia/Taipei';
17
18
    /**
19
     * @var \Payum\Core\HttpClientInterface
20
     */
21
    protected $client;
22
23
    /**
24
     * @var \Http\Message\MessageFactory
25
     */
26
    protected $messageFactory;
27
28
    /**
29
     * @var array
30
     */
31
    protected $options = [];
32
33
    /**
34
     * $encrypter.
35
     *
36
     * @var Encrypter
37
     */
38
    protected $encrypter;
39
40
    /**
41
     * @param array $options
42
     * @param \Payum\Core\HttpClientInterface $client
43
     * @param \Http\Message\MessageFactory $messageFactory
44
     * @param Encrypter $encrypter
45
     *
46
     * @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid
47
     */
48 12
    public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory, Encrypter $encrypter = null)
49
    {
50 12
        $this->options = $options;
51 12
        $this->client = $client;
52 12
        $this->messageFactory = $messageFactory;
53 12
        $this->encrypter = $encrypter ?: new Encrypter();
54 12
    }
55
56
    /**
57
     * @param string $method
58
     * @param array $params
59
     * @param string $type
60
     * @param bool $isJson
61
     * @return string
62
     */
63 4
    protected function doRequest($method, $params, $type = 'cancel', $isJson = true)
64
    {
65 4
        $request = $this->messageFactory->createRequest($method, $this->getApiEndpoint($type), [
66 4
            'Content-Type' => 'application/x-www-form-urlencoded',
67 4
        ], is_array($params) === true ? http_build_query($params) : $params);
68
69 4
        $response = $this->client->send($request);
70
71 4
        $statusCode = $response->getStatusCode();
72 4
        if (false == ($statusCode >= 200 && $statusCode < 300)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
73
            throw HttpException::factory($request, $response);
74
        }
75
76 4
        $contents = $response->getBody()->getContents();
77
78
        return $isJson === true
79 4
            ? json_decode($contents, true)
80 4
            : $contents;
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    abstract public function getApiEndpoint($type = 'capture');
87
88
    /**
89
     * createTransaction.
90
     *
91
     * @param array $params
92
     * @return array
93
     */
94
    abstract public function createTransaction(array $params);
95
96
    /**
97
     * Verify if the hash of the given parameter is correct.
98
     *
99
     * @param array $params
100
     * @return bool
101
     */
102 3
    public function verifyHash(array $params)
103
    {
104
        $filters = [
105 3
            'ok' => ['order_amount', 'send_time', 'ret', 'acquire_time', 'auth_code', 'card_no', 'notify_time', 'cust_order_no'],
106 3
            'fail' => ['order_amount', 'send_time', 'ret', 'notify_time', 'cust_order_no'],
107 3
            'status' => ['api_id', 'trans_id', 'amount', 'status', 'nonce'],
108 3
        ];
109
110 3
        if (isset($params['status']) === true) {
111 1
            $hashKey = 'checksum';
112 1
            $filterKeys = $filters['status'];
113 1
        } else {
114 2
            $hashKey = 'chk';
115 2
            $filterKeys = $filters[strtolower($params['ret'])];
116
        }
117
118 3
        return $params[$hashKey] === $this->calculateHash($params, $filterKeys);
119
    }
120
121
    /**
122
     * calculateHash.
123
     *
124
     * @param array $params
125
     * @return string
126
     */
127 6
    protected function calculateHash($params, $filterKeys = [])
128
    {
129 6
        return $this->encrypter
130 6
            ->setKey($this->options['hash_base'])
131 6
            ->encrypt($params, $filterKeys);
132
    }
133
}
134