Completed
Push — master ( 9c30d6...210d9b )
by Laurens
01:56
created

FinanceClient::getAccountTransactions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 11
cts 11
cp 1
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 5
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Client;
6
7
use DateTime;
8
use LauLamanApps\IzettleApi\API\Finance\AccountTransaction;
9
use LauLamanApps\IzettleApi\API\Finance\Enum\AccountTypeGroup;
10
use LauLamanApps\IzettleApi\API\Finance\PayoutInfo;
11
use LauLamanApps\IzettleApi\Client\Finance\AccountTransactionBuilderInterface;
12
use LauLamanApps\IzettleApi\Client\Finance\AccountTransactionParser;
13
use LauLamanApps\IzettleApi\Client\Finance\PayoutInfoBuilderInterface;
14
use LauLamanApps\IzettleApi\Client\Finance\PayoutInfoParser;
15
use LauLamanApps\IzettleApi\IzettleClientInterface;
16
use Money\Currency;
17
use Money\Money;
18
use Ramsey\Uuid\UuidInterface;
19
20
final class FinanceClient
21
{
22
    const BASE_URL = 'https://finance.izettle.com/organizations/%s';
23
24
    const GET_ACCOUNT_TRANSACTIONS = self::BASE_URL . '/accounts/%s/transactions';
25
    const GET_ACCOUNT_BALANCE = self::BASE_URL . '/accounts/%s/balance';
26
27
    const GET_PAYOUT_INFO = self::BASE_URL . '/payout-info';
28
29
    /**
30
     * @var IzettleClientInterface
31
     */
32
    private $client;
33
34
    /**
35
     * @var string
36
     */
37
    private $organizationUuid;
38
39
    /**
40
     * @var AccountTransactionBuilderInterface
41
     */
42
    private $accountTransactionBuilder;
43
44
    /**
45
     * @var PayoutInfoBuilderInterface
46
     */
47
    private $payoutInfoBuilder;
48
49 6
    public function __construct(
50
        IzettleClientInterface $client,
51
        ?UuidInterface $organizationUuid = null,
52
        AccountTransactionBuilderInterface $accountTransactionBuilder,
53
        PayoutInfoBuilderInterface $payoutInfoBuilder
54
    ) {
55 6
        $this->client = $client;
56 6
        $this->organizationUuid = $organizationUuid ? (string) $organizationUuid : 'self';
57 6
        $this->accountTransactionBuilder = $accountTransactionBuilder;
58 6
        $this->payoutInfoBuilder = $payoutInfoBuilder;
59 6
    }
60
61
    /**
62
     * @return AccountTransaction[]
63
     */
64 2
    public function getAccountTransactions(
65
        AccountTypeGroup $accountTypeGroup,
66
        DateTime $start,
67
        DateTime $end,
68
        ?int $limit = null,
69
        ?int $offset = null
70
    ): array {
71 2
        $url = sprintf(self::GET_ACCOUNT_TRANSACTIONS, $this->organizationUuid, $accountTypeGroup->getValue());
72
        $queryParams = [
73 2
            'start' => $start->format('Y-m-d'),
74 2
            'end' => $end->format('Y-m-d'),
75 2
            'limit' => $limit,
76 2
            'offset' => $offset,
77
        ];
78
79 2
        $json = $this->client->getJson(
80 2
            $this->client->get(
81 2
                $url,
82 2
                $queryParams
83
            )
84
        );
85
86 2
        return $this->accountTransactionBuilder->buildFromJson($json);
87
    }
88
89 2
    public function getBalanceInfo(AccountTypeGroup $accountTypeGroup, ?DateTime $at = null): Money
90
    {
91 2
        $url = sprintf(self::GET_ACCOUNT_BALANCE, $this->organizationUuid, $accountTypeGroup->getValue());
92 2
        $response = $this->client->get($url, ['at' => $at ? $at->format('Y-m-d') : null]);
93 2
        $data = json_decode($this->client->getJson($response), true)['data'];
94 2
        $currency = new Currency($data['currencyId']);
95
96 2
        return new Money($data['totalBalance'], $currency);
97
    }
98
99 2
    public function getPayoutInfo(?DateTime $at = null): PayoutInfo
100
    {
101 2
        $url = sprintf(self::GET_PAYOUT_INFO, $this->organizationUuid);
102 2
        $json = $this->client->getJson(
103 2
            $this->client->get(
104 2
                $url,
105 2
                ['at' => $at ? $at->format('Y-m-d') : null]
106
            )
107
        );
108
109 2
        return $this->payoutInfoBuilder->buildFromJson($json);
110
    }
111
}
112