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

FinanceClient   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 67
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
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\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\AccountTransactionParser;
12
use LauLamanApps\IzettleApi\Client\Finance\PayoutInfoParser;
13
use LauLamanApps\IzettleApi\IzettleClientInterface;
14
use Money\Currency;
15
use Money\Money;
16
use Ramsey\Uuid\UuidInterface;
17
18
final class FinanceClient
19
{
20
    const BASE_URL = 'https://finance.izettle.com/organizations/%s';
21
22
    const GET_ACCOUNT_TRANSACTIONS = self::BASE_URL . '/accounts/%s/transactions';
23
    const GET_ACCOUNT_BALANCE = self::BASE_URL . '/accounts/%s/balance';
24
25
    const GET_PAYOUT_INFO = self::BASE_URL . '/payout-info';
26
    /**
27
     * @var IzettleClientInterface
28
     */
29
    private $client;
30
    /**
31
     * @var null|UuidInterface
32
     */
33
    private $organizationUuid;
34
35
    public function __construct(
36
        IzettleClientInterface $client,
37
        ?UuidInterface $organizationUuid = null
38
    ) {
39
        $this->client = $client;
40
        $this->organizationUuid = $organizationUuid ? (string) $organizationUuid : 'self';
0 ignored issues
show
Documentation Bug introduced by
It seems like $organizationUuid ? (str...ganizationUuid : 'self' of type string is incompatible with the declared type null|object<Ramsey\Uuid\UuidInterface> of property $organizationUuid.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
    }
42
43
    /**
44
     * @return AccountTransaction[]
45
     */
46
    public function getAccountTransactions(
47
        AccountTypeGroup $accountTypeGroup,
48
        DateTime $start,
49
        DateTime $end,
50
        ?int $limit = null,
51
        ?int $offset = null
52
    ): array {
53
        $url = sprintf(self::GET_ACCOUNT_TRANSACTIONS, $this->getOrganizationUuid(), $accountTypeGroup->getValue());
0 ignored issues
show
Bug introduced by
The method getOrganizationUuid() does not seem to exist on object<LauLamanApps\Izet...i\Client\FinanceClient>.

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...
54
55
        return AccountTransactionParser::createFromResponse(
56
            $this->client->get(
57
                $url,
58
                [
59
                    'start' => $start->format('Y-m-d'),
60
                    'end' => $end->format('Y-m-d'),
61
                    'limit' => $limit,
62
                    'offset' => $offset,
63
                ]
64
            )
65
        );
66
    }
67
68
    public function getBalanceInfo(AccountTypeGroup $accountTypeGroup, ?DateTime $at = null): Money
69
    {
70
        $url = sprintf(self::GET_ACCOUNT_BALANCE, $this->getOrganizationUuid(), $accountTypeGroup->getValue());
0 ignored issues
show
Bug introduced by
The method getOrganizationUuid() does not seem to exist on object<LauLamanApps\Izet...i\Client\FinanceClient>.

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
        $response = $this->client->get($url, ['at' => $at ? $at->format('Y-m-d') : null]);
72
        $data = json_decode($response->getBody()->getContents(), true)['data'];
73
        $currency = new Currency($data['currencyId']);
74
75
        return new Money($data['totalBalance'], $currency);
76
    }
77
78
    public function getPayoutInfo(?DateTime $at = null): PayoutInfo
79
    {
80
        $url = sprintf(self::GET_PAYOUT_INFO, $this->getOrganizationUuid());
0 ignored issues
show
Bug introduced by
The method getOrganizationUuid() does not seem to exist on object<LauLamanApps\Izet...i\Client\FinanceClient>.

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...
81
82
        return PayoutInfoParser::createFromResponse($this->client->get($url, ['at' => $at ? $at->format('Y-m-d') : null]));
83
    }
84
}
85