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