Completed
Push — master ( 32a55a...fb9711 )
by Mitchel
02:18
created

PaymentResource::getResourceEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Bunq\Resource;
4
5
use Bunq\BunqClient;
6
7
final class PaymentResource
8
{
9
    /**
10
     * @var BunqClient
11
     */
12
    private $client;
13
14
    /**
15
     * @param BunqClient $client
16
     */
17
    public function __construct(BunqClient $client)
18
    {
19
        $this->client = $client;
20
    }
21
22
    /**
23
     * Lists all payments.
24
     *
25
     * @param integer $userId
26
     * @param integer $monetaryAccountId
27
     *
28
     * @return array
29
     */
30
    public function listPayments($userId, $monetaryAccountId)
31
    {
32
        $payments = $this->client->get($this->getResourceEndpoint($userId, $monetaryAccountId));
33
        foreach ($payments['Response'] as $key => $payment) {
34
            $payments['Response'][$key]['Payment']['amount']['value'] = $this->floatToCents($payment['Payment']['amount']['value']);
35
        }
36
37
        return $payments;
38
    }
39
40
    /**
41
     * Gets a user its payment information.
42
     *
43
     * @param integer $userId
44
     * @param integer $monetaryAccountId
45
     * @param integer $id
46
     *
47
     * @return array
48
     */
49
    public function getPayment($userId, $monetaryAccountId, $id)
50
    {
51
        $paymentResponse = $this->client->get($this->getResourceEndpoint($userId, $monetaryAccountId) . '/' . (int)$id);
52
53
        $payment = $paymentResponse['Response'][0]['Payment'];
54
55
        $payment['amount']['value'] = $this->floatToCents($payment['amount']['value']);
56
57
        return $payment;
58
    }
59
60
    /**
61
     * @param integer $userId
62
     * @param integer $monetaryAccountId
63
     *
64
     * @return string
65
     */
66
    private function getResourceEndpoint($userId, $monetaryAccountId)
67
    {
68
        return '/v1/user/' . (int)$userId . '/monetary-account/' . (int)$monetaryAccountId . '/payment';
69
    }
70
71
    /**
72
     * @param float $amount
73
     *
74
     * @return integer
75
     */
76
    private function floatToCents($amount)
77
    {
78
        return bcmul($amount, 100);
79
    }
80
}
81