Completed
Pull Request — master (#8)
by Laurens
06:41 queued 03:06
created

FinanceClient   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 51
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccountTransactions() 0 21 1
A getBalanceInfo() 0 9 2
A getPayoutInfo() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi;
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\AccountTransactionParser;
12
use LauLamanApps\IzettleApi\Client\Finance\PayoutInfoParser;
13
use Money\Currency;
14
use Money\Money;
15
16
final class FinanceClient extends AbstractClient
17
{
18
    const BASE_URL = 'https://finance.izettle.com/organizations/%s';
19
20
    const GET_ACCOUNT_TRANSACTIONS = self::BASE_URL . '/accounts/%s/transactions';
21
    const GET_ACCOUNT_BALANCE = self::BASE_URL . '/accounts/%s/balance';
22
23
    const GET_PAYOUT_INFO = self::BASE_URL . '/payout-info';
24
25
    /**
26
     * @return AccountTransaction[]
27
     */
28
    public function getAccountTransactions(
29
        AccountTypeGroup $accountTypeGroup,
30
        DateTime $start,
31
        DateTime $end,
32
        ?int $limit = null,
33
        ?int $offset = null
34
    ): array {
35
        $url = sprintf(self::GET_ACCOUNT_TRANSACTIONS, $this->getOrganizationUuid(), $accountTypeGroup->getValue());
36
37
        return AccountTransactionParser::createFromResponse(
38
            $this->get(
39
                $url,
40
                [
41
                    'start' => $start->format('Y-m-d'),
42
                    'end' => $end->format('Y-m-d'),
43
                    'limit' => $limit,
44
                    'offset' => $offset,
45
                ]
46
            )
47
        );
48
    }
49
50
    public function getBalanceInfo(AccountTypeGroup $accountTypeGroup, ?DateTime $at = null): Money
51
    {
52
        $url = sprintf(self::GET_ACCOUNT_BALANCE, $this->getOrganizationUuid(), $accountTypeGroup->getValue());
53
        $response = $this->get($url, ['at' => $at ? $at->format('Y-m-d') : null]);
54
        $data = json_decode($response->getBody()->getContents(), true)['data'];
55
        $currency = new Currency($data['currencyId']);
56
57
        return new Money($data['totalBalance'], $currency);
58
    }
59
60
    public function getPayoutInfo(?DateTime $at = null): PayoutInfo
61
    {
62
        $url = sprintf(self::GET_PAYOUT_INFO, $this->getOrganizationUuid());
63
64
        return PayoutInfoParser::createFromResponse($this->get($url, ['at' => $at ? $at->format('Y-m-d') : null]));
65
    }
66
}
67