Passed
Push — master ( 9ff768...b736eb )
by Laurens
02:33
created

Product::create()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 14
cts 14
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 27
nc 1
nop 12
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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