Completed
Pull Request — master (#11)
by Laurens
02:02
created

ProductClient   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 8
dl 0
loc 118
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A createCategory() 0 5 1
A createDiscount() 0 6 1
A deleteDiscount() 0 6 1
A createProduct() 0 6 1
A deleteProduct() 0 6 1
A __construct() 0 15 2
A getCategories() 0 7 1
A getDiscounts() 0 7 1
A getLibrary() 0 7 1
A getProducts() 0 7 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\IzettleClientInterface;
20
use Ramsey\Uuid\UuidInterface;
21
22
final class ProductClient
23
{
24
    const BASE_URL = 'https://products.izettle.com/organizations/%s';
25
26
    const POST_CATEGORY = self::BASE_URL . '/categories';
27
    const GET_CATEGORY = self::BASE_URL . '/categories/%s';
28
    const GET_CATEGORIES = self::BASE_URL . '/categories';
29
30
    const POST_DISCOUNT = self::BASE_URL . '/discounts';
31
    const GET_DISCOUNT = self::BASE_URL . '/discounts/%s';
32
    const PUT_DISCOUNT = self::BASE_URL . '/discounts/%s';
33
    const DELETE_DISCOUNT = self::BASE_URL . '/discounts/%s';
34
    const GET_DISCOUNTS = self::BASE_URL . '/discounts';
35
36
    const GET_EXPORT = self::BASE_URL . '/products/%s';
37
    const GET_EXPORT_TEMPLATE = self::BASE_URL . '/products/%s/template';
38
39
    const GET_LIBRARY = self::BASE_URL . '/library';
40
41
    const POST_PRODUCT = self::BASE_URL . '/products';
42
    const GET_PRODUCT = self::BASE_URL . '/products/%s';
43
    const PUT_PRODUCT = self::BASE_URL . '/products/v2/%s';
44
    const DELETE_PRODUCT = self::BASE_URL . '/products/%s';
45
    const POST_PRODUCT_VARIANT = self::BASE_URL . '/products/%s/variants';
46
    const PUT_PRODUCT_VARIANT = self::BASE_URL . '/products/%s/variants/%s';
47
    const DELETE_PRODUCT_VARIANT = self::BASE_URL . '/products/%s/variants/%s';
48
    const GET_PRODUCTS = self::BASE_URL . '/products';
49
    const DELETE_PRODUCTS = self::BASE_URL . '/products';
50
51
    private $client;
52
    private $organizationUuid;
53
    private $categoryBuilder;
54
    private $discountBuilder;
55
    private $libraryBuilder;
56
    private $productBuilder;
57
58 9
    public function __construct(
59
        IzettleClientInterface $client,
60
        ?UuidInterface $organizationUuid = null,
61
        CategoryBuilderInterface $categoryBuilder,
62
        DiscountBuilderInterface $discountBuilder,
63
        LibraryBuilderInterface $libraryBuilder,
64
        ProductBuilderInterface $productBuilder
65
    ) {
66 9
        $this->client = $client;
67 9
        $this->organizationUuid = $organizationUuid ? (string) $organizationUuid : 'self';
68 9
        $this->categoryBuilder = $categoryBuilder;
69 9
        $this->discountBuilder = $discountBuilder;
70 9
        $this->libraryBuilder = $libraryBuilder;
71 9
        $this->productBuilder = $productBuilder;
72 9
    }
73
74 1
    public function getCategories(): array
75
    {
76 1
        $url = sprintf(self::GET_CATEGORIES, $this->organizationUuid);
77 1
        $json = $this->client->getJson($this->client->get($url));
78
79 1
        return $this->categoryBuilder->buildFromJson($json);
80
    }
81
82 1
    public function createCategory(Category $category): void
83
    {
84 1
        $url = sprintf(self::POST_CATEGORY, $this->organizationUuid);
85 1
        $this->client->post($url, $category->getCreateData());
86 1
    }
87
88 1
    public function getDiscounts(): array
89
    {
90 1
        $url = sprintf(self::GET_DISCOUNTS, $this->organizationUuid);
91 1
        $json = $this->client->getJson($this->client->get($url));
92
93 1
        return $this->discountBuilder->buildFromJson($json);
94
    }
95
96 1
    public function createDiscount(Discount $discount): void
97
    {
98 1
        $url = sprintf(self::POST_DISCOUNT, $this->organizationUuid);
99
100 1
        $this->client->post($url, $discount->getCreateData());
101 1
    }
102
103 1
    public function deleteDiscount(Discount $discount): void
104
    {
105 1
        $url = sprintf(self::DELETE_DISCOUNT, $this->organizationUuid, (string) $discount->getUuid());
106
107 1
        $this->client->delete($url);
108 1
    }
109
110 1
    public function getLibrary(): Library
111
    {
112 1
        $url = sprintf(self::GET_LIBRARY, $this->organizationUuid);
113 1
        $json = $this->client->getJson($this->client->get($url));
114
115 1
        return $this->libraryBuilder->buildFromJson($json);
116
    }
117
118 1
    public function getProducts(): array
119
    {
120 1
        $url = sprintf(self::GET_PRODUCTS, $this->organizationUuid);
121 1
        $json = $this->client->getJson($this->client->get($url));
122
123 1
        return $this->productBuilder->buildFromJson($json);
124
    }
125
126 1
    public function createProduct(Product $product): void
127
    {
128 1
        $url = sprintf(self::POST_PRODUCT, $this->organizationUuid);
129
130 1
        $this->client->post($url, $product->getCreateData());
131 1
    }
132
133 1
    public function deleteProduct(Product $product): void
134
    {
135 1
        $url = sprintf(self::DELETE_PRODUCT, $this->organizationUuid, (string) $product->getUuid());
136
137 1
        $this->client->delete($url);
138 1
    }
139
}
140