Test Failed
Pull Request — master (#10)
by Laurens
03:11 queued 10s
created

InventoryClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 121
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A setOrganizationUuid() 0 4 1
A resetOrganizationUuid() 0 4 1
A getLocations() 0 7 1
A getLocation() 0 7 1
A getProduct() 0 7 1
A getHistory() 0 7 1
A getSettings() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\Client;
6
7
use LauLamanApps\IzettleApi\API\Inventory\Location;
8
use LauLamanApps\IzettleApi\API\Inventory\Settings;
9
use LauLamanApps\IzettleApi\Client\Inventory\HistoryBuilderInterface;
10
use LauLamanApps\IzettleApi\Client\Inventory\LocationBuilderInterface;
11
use LauLamanApps\IzettleApi\Client\Inventory\ProductBuilderInterface;
12
use LauLamanApps\IzettleApi\Client\Inventory\SettingsBuilderInterface;
13
use LauLamanApps\IzettleApi\IzettleClientInterface;
14
use Ramsey\Uuid\UuidInterface;
15
16
final class InventoryClient
17
{
18
    private const DEFAULT_ORGANIZATION_UUID = 'self';
19
20
    const BASE_URL = 'https://inventory.izettle.com/organizations/%s';
21
22
    const GET_HISTORY = self::BASE_URL . '/history/locations/%s';
23
24
    const GET_PRODUCT_INVENTORY = self::BASE_URL . '/inventory/locations/%s/products/%s';
25
    const GET_LOCATION_INVENTORY = self::BASE_URL . '/inventory/locations/%s';
26
    const POST_INVENTORY = self::BASE_URL . '/inventory';
27
    const POST_INVENTORY_BULK = self::BASE_URL . '/inventory/bulk';
28
    const PUT_INVENTORY = self::BASE_URL . '/inventory';
29
    const DELETE_PRODUCT_INVENTORY = self::BASE_URL . '/inventory/products/%s';
30
31
    const GET_LOCATIONS = self::BASE_URL . '/locations';
32
    const PUT_LOCATIONS = self::BASE_URL . '/locations/%s';
33
34
    const GET_SETTINGS = self::BASE_URL . '/settings';
35
    const POST_SETTINGS = self::BASE_URL . '/settings';
36
    const PUT_SETTINGS = self::BASE_URL . '/settings';
37
38
    /**
39
     * @var IzettleClientInterface
40
     */
41
    private $client;
42
43
    /**
44
     * @var string
45
     */
46
    private $organizationUuid;
47
48
    /**
49
     * @var LocationBuilderInterface
50
     */
51
    private $locationBuilder;
52
53
    /**
54
     * @var ProductBuilderInterface
55
     */
56
    private $productBuilder;
57
58
    /**
59
     * @var HistoryBuilderInterface
60
     */
61
    private $historyBuilder;
62
63
    /**
64
     * @var SettingsBuilderInterface
65
     */
66
    private $settingsBuilder;
67
68
    public function __construct(
69
        IzettleClientInterface $client,
70
        ?UuidInterface $organizationUuid = null,
71
        LocationBuilderInterface $locationBuilder,
72
        ProductBuilderInterface $productBuilder,
73
        HistoryBuilderInterface $historyBuilder,
74
        SettingsBuilderInterface $settingsBuilder
75
    ) {
76
        $this->client = $client;
77
        $this->organizationUuid = $organizationUuid ? $organizationUuid->toString() : self::DEFAULT_ORGANIZATION_UUID;
78
        $this->locationBuilder = $locationBuilder;
79
        $this->productBuilder = $productBuilder;
80
        $this->historyBuilder = $historyBuilder;
81
        $this->settingsBuilder = $settingsBuilder;
82
    }
83
84
    public function setOrganizationUuid(UuidInterface $organizationUuid): void
85
    {
86
        $this->organizationUuid = $organizationUuid->toString();
87
    }
88
89
    public function resetOrganizationUuid(): void
90
    {
91
        $this->organizationUuid = self::DEFAULT_ORGANIZATION_UUID;
92
    }
93
94
    /**
95
     * @return Location[]
96
     */
97
    public function getLocations(): array
98
    {
99
        $url = sprintf(self::GET_LOCATIONS, $this->organizationUuid);
100
        $json = $this->client->getJson($this->client->get($url));
101
102
        return $this->locationBuilder->buildFromJsonArray($json);
103
    }
104
105
    public function getLocation(UuidInterface $locationUuid): Location
106
    {
107
        $url = sprintf(self::GET_LOCATION_INVENTORY, $this->organizationUuid, $locationUuid->toString());
108
        $json = $this->client->getJson($this->client->get($url));
109
110
        return $this->locationBuilder->buildFromJson($json);
111
    }
112
113
    public function getProduct(UuidInterface $locationUuid, UuidInterface $productUuid): Product
114
    {
115
        $url = sprintf(self::GET_PRODUCT_INVENTORY, $this->organizationUuid, $locationUuid->toString(), $productUuid->toString());
116
        $json = $this->client->getJson($this->client->get($url));
117
118
        return $this->productBuilder->buildFromJson($json);
119
    }
120
121
    public function getHistory(UuidInterface $locationUuid): History
122
    {
123
        $url = sprintf(self::GET_HISTORY, $this->organizationUuid, $locationUuid->toString());
124
        $json = $this->client->getJson($this->client->get($url, ['balanceChangeType' => 'RESTOCK']));
125
126
        return $this->historyBuilder->buildFromJson($json);
127
    }
128
129
    public function getSettings(): Settings
130
    {
131
        $url = sprintf(self::GET_SETTINGS, $this->organizationUuid);
132
        $json = $this->client->getJson($this->client->get($url));
133
134
        return $this->settingsBuilder->buildFromJson($json);
135
    }
136
}
137