Completed
Branch add-single-product-endpoint (3e130a)
by Laurens
05:53 queued 02:51
created

ProductClient::getProduct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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 14
    public function __construct(
60
        IzettleClientInterface $client,
61
        ?UuidInterface $organizationUuid = null,
62
        CategoryBuilderInterface $categoryBuilder,
63
        DiscountBuilderInterface $discountBuilder,
64
        LibraryBuilderInterface $libraryBuilder,
65
        ProductBuilderInterface $productBuilder
66
    ) {
67 14
        $this->client = $client;
68 14
        $this->organizationUuid = $organizationUuid ? (string) $organizationUuid : 'self';
69 14
        $this->categoryBuilder = $categoryBuilder;
70 14
        $this->discountBuilder = $discountBuilder;
71 14
        $this->libraryBuilder = $libraryBuilder;
72 14
        $this->productBuilder = $productBuilder;
73 14
    }
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 1
    public function getProduct(UuidInterface $uuid): Product
135
    {
136 1
        $url = sprintf(self::GET_PRODUCT, $this->organizationUuid, (string) $uuid->toString());
137
138 1
        $json = $this->client->getJson($this->client->get($url));
139
140 1
        return $this->productBuilder->buildSingleFromJson($json);
141
    }
142
143
    /**
144
     * @return Product[]
145
     */
146 2
    public function getProducts(): array
147
    {
148 2
        $url = sprintf(self::GET_PRODUCTS, $this->organizationUuid);
149 2
        $json = $this->client->getJson($this->client->get($url));
150
151 2
        return $this->productBuilder->buildFromJson($json);
152
    }
153
154
    /**
155
     * @throws UnprocessableEntityException
156
     */
157 1
    public function createProduct(Product $product): void
158
    {
159 1
        $url = sprintf(self::POST_PRODUCT, $this->organizationUuid);
160
161 1
        $this->client->post($url, $product);
162 1
    }
163
164
    /**
165
     * @throws UnprocessableEntityException
166
     */
167 1
    public function deleteProduct(Product $product): void
168
    {
169 1
        $url = sprintf(self::DELETE_PRODUCT, $this->organizationUuid, (string) $product->getUuid());
170
171 1
        $this->client->delete($url);
172 1
    }
173
}
174