Completed
Pull Request — master (#8)
by Laurens
01:58
created

FinanceClient::getPayoutInfo()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 1
nop 1
crap 6
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
    public function __construct(
50
        IzettleClientInterface $client,
51
        ?UuidInterface $organizationUuid = null,
52
        AccountTransactionBuilderInterface $accountTransactionBuilder,
53
        PayoutInfoBuilderInterface $payoutInfoBuilder
54
    ) {
55
        $this->client = $client;
56
        $this->organizationUuid = $organizationUuid ? (string) $organizationUuid : 'self';
57
        $this->accountTransactionBuilder = $accountTransactionBuilder;
58
        $this->payoutInfoBuilder = $payoutInfoBuilder;
59
    }
60
61
    /**
62
     * @return AccountTransaction[]
63
     */
64
    public function getAccountTransactions(
65
        AccountTypeGroup $accountTypeGroup,
66
        DateTime $start,
67
        DateTime $end,
68
        ?int $limit = null,
69
        ?int $offset = null
70
    ): array {
71
        $url = sprintf(self::GET_ACCOUNT_TRANSACTIONS, $this->organizationUuid, $accountTypeGroup->getValue());
72
        $json = $this->client->getJson(
73
            $this->client->get(
74
                $url,
75
                [
76
                    'start' => $start->format('Y-m-d'),
77
                    'end' => $end->format('Y-m-d'),
78
                    'limit' => $limit,
79
                    'offset' => $offset,
80
                ]
81
            )
82
        );
83
84
        return $this->accountTransactionBuilder->buildFromJson($json);
85
    }
86
87
    public function getBalanceInfo(AccountTypeGroup $accountTypeGroup, ?DateTime $at = null): Money
88
    {
89
        $url = sprintf(self::GET_ACCOUNT_BALANCE, $this->organizationUuid, $accountTypeGroup->getValue());
90
        $response = $this->client->get($url, ['at' => $at ? $at->format('Y-m-d') : null]);
91
        $data = json_decode($this->client->getJson($response), true)['data'];
92
        $currency = new Currency($data['currencyId']);
93
94
        return new Money($data['totalBalance'], $currency);
95
    }
96
97
    public function getPayoutInfo(?DateTime $at = null): PayoutInfo
98
    {
99
        $url = sprintf(self::GET_PAYOUT_INFO, $this->organizationUuid);
100
        $json = $this->client->getJson(
101
            $this->client->get(
102
                $url,
103
                ['at' => $at ? $at->format('Y-m-d') : null]
104
            )
105
        );
106
107
        return $this->payoutInfoBuilder->buildFromJson($json);
108
    }
109
}
110