Test Failed
Pull Request — master (#13)
by Bernhard
02:34 queued 01:06
created

PaymentApi::filterData()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 1
nop 1
crap 12
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.2.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 3
    public function __construct(Connector $connector)
26
    {
27 3
        $this->connector = $connector;
28 3
    }
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
    public function createPaymentRequest(PaymentRequest $paymentRequest)
69
    {
70
        if ($paymentRequest->exists()) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on GloBee\PaymentApi\Models\PaymentRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        if ($paymentRequest->/** @scrutinizer ignore-call */ exists()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            throw new PaymentRequestAlreadyExistsException();
72
        }
73
        $data = $this->filterData($paymentRequest->toArray());
74
        $response = $this->connector->postJson('v1/payment-request', $data);
75
76
        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
    protected function filterData(array $data)
85
    {
86
        $data = array_map(function ($item) {
87
            if (is_array($item)) {
88
                $item = $this->filterData($item);
89
            }
90
91
            return $item;
92
        }, $data);
93
94
        return array_filter($data, function ($item) {
95
            if (is_array($item)) {
96
                return !empty($item);
97
            }
98
99
            return $item !== null;
100
        });
101
    }
102
}
103