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

ProductClient   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 127
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 127
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 2
A getCategories() 0 7 1
A createCategory() 0 5 1
A getDiscounts() 0 7 1
A createDiscount() 0 6 1
A deleteDiscount() 0 6 1
A getLibrary() 0 7 1
A getProducts() 0 7 1
A createProduct() 0 6 1
A deleteProduct() 0 6 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 13
    public function __construct(
59
        IzettleClientInterface $client,
60
        ?UuidInterface $organizationUuid = null,
61
        CategoryBuilderInterface $categoryBuilder,
62
        DiscountBuilderInterface $discountBuilder,
63
        LibraryBuilderInterface $libraryBuilder,
64
        ProductBuilderInterface $productBuilder
65
    ) {
66 13
        $this->client = $client;
67 13
        $this->organizationUuid = $organizationUuid ? (string) $organizationUuid : 'self';
68 13
        $this->categoryBuilder = $categoryBuilder;
69 13
        $this->discountBuilder = $discountBuilder;
70 13
        $this->libraryBuilder = $libraryBuilder;
71 13
        $this->productBuilder = $productBuilder;
72 13
    }
73
74
    /**
75
     * @return Category[]
76
     */
77 2
    public function getCategories(): array
78
    {
79 2
        $url = sprintf(self::GET_CATEGORIES, $this->organizationUuid);
80 2
        $json = $this->client->getJson($this->client->get($url));
81
82 2
        return $this->categoryBuilder->buildFromJson($json);
83
    }
84
85 1
    public function createCategory(Category $category): void
86
    {
87 1
        $url = sprintf(self::POST_CATEGORY, $this->organizationUuid);
88 1
        $this->client->post($url, $category->getCreateData());
89 1
    }
90
91
    /**
92
     * @return Discount[]
93
     */
94 2
    public function getDiscounts(): array
95
    {
96 2
        $url = sprintf(self::GET_DISCOUNTS, $this->organizationUuid);
97 2
        $json = $this->client->getJson($this->client->get($url));
98
99 2
        return $this->discountBuilder->buildFromJson($json);
100
    }
101
102 1
    public function createDiscount(Discount $discount): void
103
    {
104 1
        $url = sprintf(self::POST_DISCOUNT, $this->organizationUuid);
105
106 1
        $this->client->post($url, $discount->getCreateData());
107 1
    }
108
109 1
    public function deleteDiscount(Discount $discount): void
110
    {
111 1
        $url = sprintf(self::DELETE_DISCOUNT, $this->organizationUuid, (string) $discount->getUuid());
112
113 1
        $this->client->delete($url);
114 1
    }
115
116 2
    public function getLibrary(): Library
117
    {
118 2
        $url = sprintf(self::GET_LIBRARY, $this->organizationUuid);
119 2
        $json = $this->client->getJson($this->client->get($url));
120
121 2
        return $this->libraryBuilder->buildFromJson($json);
122
    }
123
124
    /**
125
     * @return Product[]
126
     */
127 2
    public function getProducts(): array
128
    {
129 2
        $url = sprintf(self::GET_PRODUCTS, $this->organizationUuid);
130 2
        $json = $this->client->getJson($this->client->get($url));
131
132 2
        return $this->productBuilder->buildFromJson($json);
133
    }
134
135 1
    public function createProduct(Product $product): void
136
    {
137 1
        $url = sprintf(self::POST_PRODUCT, $this->organizationUuid);
138
139 1
        $this->client->post($url, $product->getCreateData());
140 1
    }
141
142 1
    public function deleteProduct(Product $product): void
143
    {
144 1
        $url = sprintf(self::DELETE_PRODUCT, $this->organizationUuid, (string) $product->getUuid());
145
146 1
        $this->client->delete($url);
147 1
    }
148
}
149