PaymentApi::getCurrencies()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace GloBee\PaymentApi;
4
5
use GloBee\PaymentApi\Connectors\Connector;
6
use GloBee\PaymentApi\Exceptions\PaymentRequestAlreadyExistsException;
7
use GloBee\PaymentApi\Models\Account;
8
use GloBee\PaymentApi\Models\Currency;
9
use GloBee\PaymentApi\Models\PaymentRequest;
10
11
class PaymentApi
12
{
13
    const VERSION = '0.5.0';
14
15
    /**
16
     * @var Connector
17
     */
18
    private $connector;
19
20
    /**
21
     * PaymentApi constructor.
22
     *
23
     * @param Connector $connector
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
24
     */
25 5
    public function __construct(Connector $connector)
26
    {
27 5
        $this->connector = $connector;
28 5
    }
29
30
    /**
31
     * @return Account
32
     */
33 1
    public function getAccount()
34
    {
35 1
        $data = $this->connector->getJson('v1/ping');
36
37 1
        return Account::fromResponse($data['data']);
38
    }
39
40
    /**
41
     * @return Currency[]
42
     */
43 1
    public function getCurrencies()
44
    {
45 1
        $data = $this->connector->getJson('v1/currencies');
46
47 1
        return Currency::fromResponse($data['data']);
48
    }
49
50
    /**
51
     * @param string $paymentRequestId
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
52
     *
53
     * @return PaymentRequest
54
     */
55 1
    public function getPaymentRequest($paymentRequestId)
0 ignored issues
show
Coding Style introduced by
Type hint "string" missing for $paymentRequestId
Loading history...
56
    {
57 1
        $response = $this->connector->getJson('v1/payment-request/'.$paymentRequestId);
58
59 1
        return PaymentRequest::fromResponse($response['data']);
60
    }
61
62
    /**
63
     * @param PaymentRequest $paymentRequest
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
64
     *
65
     * @return PaymentRequest
66
     * @throws PaymentRequestAlreadyExistsException
0 ignored issues
show
Coding Style introduced by
Comment missing for @throws tag in function comment
Loading history...
67
     */
68 2
    public function createPaymentRequest(PaymentRequest $paymentRequest)
69
    {
70 2
        if ($paymentRequest->id !== null) {
0 ignored issues
show
Bug Best Practice introduced by
The property $id is declared protected in GloBee\PaymentApi\Models\PaymentRequest. Since you implement __get, consider adding a @property or @property-read.
Loading history...
71 1
            throw new PaymentRequestAlreadyExistsException();
72
        }
73 1
        $data = $this->filterData($paymentRequest->toArray());
74 1
        $response = $this->connector->postJson('v1/payment-request', $data);
75
76 1
        return PaymentRequest::fromResponse($response['data']);
77
    }
78
79
    /**
80
     * @param array $data
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
81
     *
82
     * @return array
83
     */
84 1
    protected function filterData(array $data)
85
    {
86
        $data = array_map(function ($item) {
87 1
            if (is_array($item)) {
88 1
                $item = $this->filterData($item);
89
            }
90
91 1
            return $item;
92 1
        }, $data);
93
94
        return array_filter($data, function ($item) {
95 1
            if (is_array($item)) {
96 1
                return !empty($item);
97
            }
98
99 1
            return $item !== null;
100 1
        });
101
    }
102
}
103