1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LauLamanApps\IzettleApi\Client; |
6
|
|
|
|
7
|
|
|
use LauLamanApps\IzettleApi\API\Product\Category; |
8
|
|
|
use LauLamanApps\IzettleApi\API\Product\Discount; |
9
|
|
|
use LauLamanApps\IzettleApi\API\Product\Library; |
10
|
|
|
use LauLamanApps\IzettleApi\API\Product\Product; |
11
|
|
|
use LauLamanApps\IzettleApi\Client\Product\CategoryBuilder; |
12
|
|
|
use LauLamanApps\IzettleApi\Client\Product\CategoryBuilderInterface; |
13
|
|
|
use LauLamanApps\IzettleApi\Client\Product\DiscountBuilder; |
14
|
|
|
use LauLamanApps\IzettleApi\Client\Product\DiscountBuilderInterface; |
15
|
|
|
use LauLamanApps\IzettleApi\Client\Product\LibraryBuilder; |
16
|
|
|
use LauLamanApps\IzettleApi\Client\Product\LibraryBuilderInterface; |
17
|
|
|
use LauLamanApps\IzettleApi\Client\Product\ProductBuilder; |
18
|
|
|
use LauLamanApps\IzettleApi\Client\Product\ProductBuilderInterface; |
19
|
|
|
use LauLamanApps\IzettleApi\Exception\UnprocessableEntityException; |
20
|
|
|
use LauLamanApps\IzettleApi\IzettleClientInterface; |
21
|
|
|
use Ramsey\Uuid\UuidInterface; |
22
|
|
|
|
23
|
|
|
final class ProductClient |
24
|
|
|
{ |
25
|
|
|
const BASE_URL = 'https://products.izettle.com/organizations/%s'; |
26
|
|
|
|
27
|
|
|
const POST_CATEGORY = self::BASE_URL . '/categories'; |
28
|
|
|
const GET_CATEGORY = self::BASE_URL . '/categories/%s'; |
29
|
|
|
const GET_CATEGORIES = self::BASE_URL . '/categories'; |
30
|
|
|
|
31
|
|
|
const POST_DISCOUNT = self::BASE_URL . '/discounts'; |
32
|
|
|
const GET_DISCOUNT = self::BASE_URL . '/discounts/%s'; |
33
|
|
|
const PUT_DISCOUNT = self::BASE_URL . '/discounts/%s'; |
34
|
|
|
const DELETE_DISCOUNT = self::BASE_URL . '/discounts/%s'; |
35
|
|
|
const GET_DISCOUNTS = self::BASE_URL . '/discounts'; |
36
|
|
|
|
37
|
|
|
const GET_EXPORT = self::BASE_URL . '/products/%s'; |
38
|
|
|
const GET_EXPORT_TEMPLATE = self::BASE_URL . '/products/%s/template'; |
39
|
|
|
|
40
|
|
|
const GET_LIBRARY = self::BASE_URL . '/library'; |
41
|
|
|
|
42
|
|
|
const POST_PRODUCT = self::BASE_URL . '/products'; |
43
|
|
|
const GET_PRODUCT = self::BASE_URL . '/products/%s'; |
44
|
|
|
const PUT_PRODUCT = self::BASE_URL . '/products/v2/%s'; |
45
|
|
|
const DELETE_PRODUCT = self::BASE_URL . '/products/%s'; |
46
|
|
|
const POST_PRODUCT_VARIANT = self::BASE_URL . '/products/%s/variants'; |
47
|
|
|
const PUT_PRODUCT_VARIANT = self::BASE_URL . '/products/%s/variants/%s'; |
48
|
|
|
const DELETE_PRODUCT_VARIANT = self::BASE_URL . '/products/%s/variants/%s'; |
49
|
|
|
const GET_PRODUCTS = self::BASE_URL . '/products'; |
50
|
|
|
const DELETE_PRODUCTS = self::BASE_URL . '/products'; |
51
|
|
|
|
52
|
|
|
private $client; |
53
|
|
|
private $organizationUuid; |
54
|
|
|
private $categoryBuilder; |
55
|
|
|
private $discountBuilder; |
56
|
|
|
private $libraryBuilder; |
57
|
|
|
private $productBuilder; |
58
|
|
|
|
59
|
13 |
|
public function __construct( |
60
|
|
|
IzettleClientInterface $client, |
61
|
|
|
?UuidInterface $organizationUuid = null, |
62
|
|
|
CategoryBuilderInterface $categoryBuilder, |
63
|
|
|
DiscountBuilderInterface $discountBuilder, |
64
|
|
|
LibraryBuilderInterface $libraryBuilder, |
65
|
|
|
ProductBuilderInterface $productBuilder |
66
|
|
|
) { |
67
|
13 |
|
$this->client = $client; |
68
|
13 |
|
$this->organizationUuid = $organizationUuid ? (string) $organizationUuid : 'self'; |
69
|
13 |
|
$this->categoryBuilder = $categoryBuilder; |
70
|
13 |
|
$this->discountBuilder = $discountBuilder; |
71
|
13 |
|
$this->libraryBuilder = $libraryBuilder; |
72
|
13 |
|
$this->productBuilder = $productBuilder; |
73
|
13 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return Category[] |
77
|
|
|
*/ |
78
|
2 |
|
public function getCategories(): array |
79
|
|
|
{ |
80
|
2 |
|
$url = sprintf(self::GET_CATEGORIES, $this->organizationUuid); |
81
|
2 |
|
$json = $this->client->getJson($this->client->get($url)); |
82
|
|
|
|
83
|
2 |
|
return $this->categoryBuilder->buildFromJson($json); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @throws UnprocessableEntityException |
88
|
|
|
*/ |
89
|
1 |
|
public function createCategory(Category $category): void |
90
|
|
|
{ |
91
|
1 |
|
$url = sprintf(self::POST_CATEGORY, $this->organizationUuid); |
92
|
1 |
|
$this->client->post($url, $category); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return Discount[] |
97
|
|
|
*/ |
98
|
2 |
|
public function getDiscounts(): array |
99
|
|
|
{ |
100
|
2 |
|
$url = sprintf(self::GET_DISCOUNTS, $this->organizationUuid); |
101
|
2 |
|
$json = $this->client->getJson($this->client->get($url)); |
102
|
|
|
|
103
|
2 |
|
return $this->discountBuilder->buildFromJson($json); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @throws UnprocessableEntityException |
108
|
|
|
*/ |
109
|
1 |
|
public function createDiscount(Discount $discount): void |
110
|
|
|
{ |
111
|
1 |
|
$url = sprintf(self::POST_DISCOUNT, $this->organizationUuid); |
112
|
|
|
|
113
|
1 |
|
$this->client->post($url, $discount); |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @throws UnprocessableEntityException |
118
|
|
|
*/ |
119
|
1 |
|
public function deleteDiscount(Discount $discount): void |
120
|
|
|
{ |
121
|
1 |
|
$url = sprintf(self::DELETE_DISCOUNT, $this->organizationUuid, (string) $discount->getUuid()); |
122
|
|
|
|
123
|
1 |
|
$this->client->delete($url); |
124
|
1 |
|
} |
125
|
|
|
|
126
|
2 |
|
public function getLibrary(): Library |
127
|
|
|
{ |
128
|
2 |
|
$url = sprintf(self::GET_LIBRARY, $this->organizationUuid); |
129
|
2 |
|
$json = $this->client->getJson($this->client->get($url)); |
130
|
|
|
|
131
|
2 |
|
return $this->libraryBuilder->buildFromJson($json); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return Product[] |
136
|
|
|
*/ |
137
|
2 |
|
public function getProducts(): array |
138
|
|
|
{ |
139
|
2 |
|
$url = sprintf(self::GET_PRODUCTS, $this->organizationUuid); |
140
|
2 |
|
$json = $this->client->getJson($this->client->get($url)); |
141
|
|
|
|
142
|
2 |
|
return $this->productBuilder->buildFromJson($json); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @throws UnprocessableEntityException |
147
|
|
|
*/ |
148
|
1 |
|
public function createProduct(Product $product): void |
149
|
|
|
{ |
150
|
1 |
|
$url = sprintf(self::POST_PRODUCT, $this->organizationUuid); |
151
|
|
|
|
152
|
1 |
|
$this->client->post($url, $product); |
153
|
1 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @throws UnprocessableEntityException |
157
|
|
|
*/ |
158
|
|
|
public function updateProduct($uuid, array $array): void |
159
|
|
|
{ |
160
|
|
|
$url = sprintf(self::PUT_PRODUCT, $this->organizationUuid, $uuid); |
161
|
|
|
|
162
|
|
|
$this->client->put($url, json_encode($array)); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @throws UnprocessableEntityException |
167
|
|
|
*/ |
168
|
1 |
|
public function deleteProduct(Product $product): void |
169
|
|
|
{ |
170
|
1 |
|
$url = sprintf(self::DELETE_PRODUCT, $this->organizationUuid, (string) $product->getUuid()); |
171
|
|
|
|
172
|
1 |
|
$this->client->delete($url); |
173
|
1 |
|
} |
174
|
|
|
} |
175
|
|
|
|