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

Discount::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

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