Completed
Push — master ( 73b08b...9131b2 )
by Laurens
02:28
created

Product::getPostBodyData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
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\Client\Exception\CantCreateProductException;
11
use Ramsey\Uuid\Uuid;
12
use Ramsey\Uuid\UuidInterface;
13
14
final class Product implements IzettlePostable
15
{
16
    private $uuid;
17
    private $categories;
18
    private $name;
19
    private $description;
20
    private $imageCollection;
21
    private $variants;
22
    private $externalReference;
23
    private $etag;
24
    private $updatedAt;
25
    private $updatedBy;
26
    private $createdAt;
27
    private $vatPercentage;
28
29 6
    public static function create(
30
        UuidInterface $uuid,
31
        CategoryCollection $categories,
32
        string $name,
33
        ?string $description = null,
34
        ImageCollection $imageCollection,
35
        VariantCollection $variants,
36
        ?string $externalReference =  null,
37
        string $etag,
38
        DateTime $updatedAt,
39
        UuidInterface $updatedBy,
40
        DateTime $createdAt,
41
        float $vatPercentage
42
    ): self {
43 6
        return new self(
44 6
            $uuid,
45 6
            $categories,
46 6
            $name,
47 6
            $description,
48 6
            $imageCollection,
49 6
            $variants,
50 6
            $externalReference,
51 6
            $etag,
52 6
            $updatedAt,
53 6
            $updatedBy,
54 6
            $createdAt,
55 6
            $vatPercentage
56
        );
57
    }
58
59 6
    public static function new(
60
        string $name,
61
        string $description,
62
        CategoryCollection $categories,
63
        ImageCollection $imageCollection,
64
        VariantCollection $variants,
65
        ?string $externalReference = null
66
    ): self {
67 6
        return new self(
68 6
            Uuid::uuid1(),
69 6
            $categories,
70 6
            $name,
71 6
            $description,
72 6
            $imageCollection,
73 6
            $variants,
74 6
            $externalReference
75
        );
76
    }
77
78 8
    public function getUuid(): UuidInterface
79
    {
80 8
        return $this->uuid;
81
    }
82
83 4
    public function getCategories(): CategoryCollection
84
    {
85 4
        return $this->categories;
86
    }
87
88 5
    public function getName(): string
89
    {
90 5
        return $this->name;
91
    }
92
93 5
    public function getDescription(): ?string
94
    {
95 5
        return $this->description;
96
    }
97
98 4
    public function getImageLookupKeys(): ImageCollection
99
    {
100 4
        return $this->imageCollection;
101
    }
102
103 4
    public function getVariants(): VariantCollection
104
    {
105 4
        return $this->variants;
106
    }
107
108 5
    public function getExternalReference(): ?string
109
    {
110 5
        return $this->externalReference;
111
    }
112
113 5
    public function getEtag(): string
114
    {
115 5
        return $this->etag;
116
    }
117
118 5
    public function getUpdatedAt(): DateTime
119
    {
120 5
        return $this->updatedAt;
121
    }
122
123 5
    public function getUpdatedBy(): UuidInterface
124
    {
125 5
        return $this->updatedBy;
126
    }
127
128 5
    public function getCreatedAt(): DateTime
129
    {
130 5
        return $this->createdAt;
131
    }
132
133 5
    public function getVatPercentage(): float
134
    {
135 5
        return $this->vatPercentage;
136
    }
137
138 3
    public function getPostBodyData(): string
139
    {
140 3
        $this->validateMinimumVariants();
141
142
        $data = [
143 2
            'uuid' => $this->uuid,
144 2
            'categories' => $this->categories->getCreateDataArray(),
145 2
            'name' => $this->name,
146 2
            'description' => $this->description,
147 2
            'imageLookupKeys' => $this->imageCollection->getCreateDataArray(),
148 2
            'variants' => $this->variants->getCreateDataArray(),
149 2
            'externalReference' => $this->externalReference
150
        ];
151
152 2
        return json_encode($data);
153
    }
154
155 12
    private function __construct(
156
        UuidInterface $uuid,
157
        CategoryCollection $categories,
158
        string $name,
159
        ?string $description,
160
        ImageCollection $imageCollection,
161
        VariantCollection $variants,
162
        ?string $externalReference = null,
163
        ?string $etag = null,
164
        ?DateTime $updatedAt  = null,
165
        ?UuidInterface $updatedBy = null,
166
        ?DateTime $createdAt = null,
167
        ?float $vatPercentage = null
168
    ) {
169 12
        $this->uuid = $uuid;
170 12
        $this->categories = $categories;
171 12
        $this->name = $name;
172 12
        $this->description = $description;
173 12
        $this->imageCollection = $imageCollection;
174 12
        $this->variants = $variants;
175 12
        $this->externalReference = $externalReference;
176 12
        $this->etag = $etag;
177 12
        $this->updatedAt = $updatedAt;
178 12
        $this->updatedBy = $updatedBy;
179 12
        $this->createdAt = $createdAt;
180 12
        $this->vatPercentage = $vatPercentage;
181 12
    }
182
183 3
    private function validateMinimumVariants(): void
184
    {
185 3
        if (count($this->variants->getAll()) == 0) {
186 1
            throw new CantCreateProductException('A product should have at least one variant');
187
        }
188 2
    }
189
}
190