Passed
Branch releases/1.0-dev (6a4c8c)
by Laurens
03:03
created

Product::getVat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\IzettleApi\API\Product;
6
7
use DateTime;
8
use LauLamanApps\IzettleApi\API\ImageCollection;
9
use LauLamanApps\IzettleApi\API\Universal\IzettlePostable;
10
use LauLamanApps\IzettleApi\API\Universal\Vat;
11
use LauLamanApps\IzettleApi\Client\Exception\CantCreateProductException;
12
use Ramsey\Uuid\Uuid;
13
use Ramsey\Uuid\UuidInterface;
14
15
final class Product implements IzettlePostable
16
{
17
    /**
18
     * @var UuidInterface
19
     */
20
    private $uuid;
21
22
    /**
23
     * @var CategoryCollection
24
     */
25
    private $categories;
26
27
    /**
28
     * @var string
29
     */
30
    private $name;
31
32
    /**
33
     * @var string|null
34
     */
35
    private $description;
36
37
    /**
38
     * @var ImageCollection
39
     */
40
    private $imageCollection;
41
42
    /**
43
     * @var VariantCollection
44
     */
45
    private $variants;
46
47
    /**
48
     * @var string|null
49
     */
50
    private $externalReference;
51
52
    /**
53
     * @var string|null
54
     */
55
    private $etag;
56
57
    /**
58
     * @var DateTime|null
59
     */
60
    private $updatedAt;
61
62
    /**
63
     * @var UuidInterface|null
64
     */
65
    private $updatedBy;
66
67
    /**
68
     * @var DateTime|null
69
     */
70
    private $createdAt;
71
72
    /**
73
     * @var Vat|null
74
     */
75
    private $vat;
76
77
78 6
    public static function create(
79
        UuidInterface $uuid,
80
        CategoryCollection $categories,
81
        string $name,
82
        ?string $description = null,
83
        ImageCollection $imageCollection,
84
        VariantCollection $variants,
85
        ?string $externalReference = null,
86
        string $etag,
87
        DateTime $updatedAt,
88
        UuidInterface $updatedBy,
89
        DateTime $createdAt,
90
        ?Vat $vat = null
91
    ): self {
92 6
        return new self(
93 6
            $uuid,
94 6
            $categories,
95 6
            $name,
96 6
            $description,
97 6
            $imageCollection,
98 6
            $variants,
99 6
            $externalReference,
100 6
            $etag,
101 6
            $updatedAt,
102 6
            $updatedBy,
103 6
            $createdAt,
104 6
            $vat
105
        );
106
    }
107
108 6
    public static function new(
109
        string $name,
110
        string $description,
111
        CategoryCollection $categories,
112
        ImageCollection $imageCollection,
113
        VariantCollection $variants,
114
        ?string $externalReference = null
115
    ): self {
116 6
        return new self(
117 6
            Uuid::uuid1(),
118 6
            $categories,
119 6
            $name,
120 6
            $description,
121 6
            $imageCollection,
122 6
            $variants,
123 6
            $externalReference
124
        );
125
    }
126
127 8
    public function getUuid(): UuidInterface
128
    {
129 8
        return $this->uuid;
130
    }
131
132 4
    public function getCategories(): CategoryCollection
133
    {
134 4
        return $this->categories;
135
    }
136
137 5
    public function getName(): string
138
    {
139 5
        return $this->name;
140
    }
141
142 5
    public function getDescription(): ?string
143
    {
144 5
        return $this->description;
145
    }
146
147 4
    public function getImageLookupKeys(): ImageCollection
148
    {
149 4
        return $this->imageCollection;
150
    }
151
152 4
    public function getVariants(): VariantCollection
153
    {
154 4
        return $this->variants;
155
    }
156
157 5
    public function getExternalReference(): ?string
158
    {
159 5
        return $this->externalReference;
160
    }
161
162 5
    public function getEtag(): string
163
    {
164 5
        return $this->etag;
165
    }
166
167 5
    public function getUpdatedAt(): DateTime
168
    {
169 5
        return $this->updatedAt;
170
    }
171
172 5
    public function getUpdatedBy(): UuidInterface
173
    {
174 5
        return $this->updatedBy;
175
    }
176
177 5
    public function getCreatedAt(): DateTime
178
    {
179 5
        return $this->createdAt;
180
    }
181
182 5
    public function getVat(): ?Vat
183
    {
184 5
        return $this->vat;
185
    }
186
187 3
    public function getPostBodyData(): string
188
    {
189 3
        $this->validateMinimumVariants();
190
191
        $data = [
192 2
            'uuid' => $this->uuid,
193 2
            'categories' => $this->categories->getCreateDataArray(),
194 2
            'name' => $this->name,
195 2
            'description' => $this->description,
196 2
            'imageLookupKeys' => $this->imageCollection->getCreateDataArray(),
197 2
            'variants' => $this->variants->getCreateDataArray(),
198 2
            'externalReference' => $this->externalReference
199
        ];
200
201 2
        return json_encode($data);
202
    }
203
204 12
    private function __construct(
205
        UuidInterface $uuid,
206
        CategoryCollection $categories,
207
        string $name,
208
        ?string $description,
209
        ImageCollection $imageCollection,
210
        VariantCollection $variants,
211
        ?string $externalReference = null,
212
        ?string $etag = null,
213
        ?DateTime $updatedAt  = null,
214
        ?UuidInterface $updatedBy = null,
215
        ?DateTime $createdAt = null,
216
        ?Vat $vatPercentage = null
217
    ) {
218 12
        $this->uuid = $uuid;
219 12
        $this->categories = $categories;
220 12
        $this->name = $name;
221 12
        $this->description = $description;
222 12
        $this->imageCollection = $imageCollection;
223 12
        $this->variants = $variants;
224 12
        $this->externalReference = $externalReference;
225 12
        $this->etag = $etag;
226 12
        $this->updatedAt = $updatedAt;
227 12
        $this->updatedBy = $updatedBy;
228 12
        $this->createdAt = $createdAt;
229 12
        $this->vat = $vatPercentage;
230 12
    }
231
232 3
    private function validateMinimumVariants(): void
233
    {
234 3
        if (count($this->variants->getAll()) == 0) {
235 1
            throw new CantCreateProductException('A product should have at least one variant');
236
        }
237 2
    }
238
}
239