Passed
Pull Request — master (#12)
by Laurens
01:27
created

Passbook::setBackgroundColor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\ApplePassbook;
6
7
use DateTimeImmutable;
8
use DateTimeInterface;
9
use LauLamanApps\ApplePassbook\Exception\MissingRequiredDataException;
10
use LauLamanApps\ApplePassbook\MetaData\Barcode;
11
use LauLamanApps\ApplePassbook\MetaData\Field\Field;
12
use LauLamanApps\ApplePassbook\MetaData\Image;
13
use LauLamanApps\ApplePassbook\MetaData\Location;
14
use LauLamanApps\ApplePassbook\MetaData\SemanticTag;
15
use LauLamanApps\ApplePassbook\Style\Color;
16
use LogicException;
17
18
abstract class Passbook
19
{
20
    protected const TYPE = null;
21
22
    private int $formatVersion = 1;
23
    private string $passTypeIdentifier;
24
    private string $serialNumber;
25
    private string $teamIdentifier;
26
    private string $organizationName;
27
    private string$description;
28
    private string $logoText;
29
    /** @var Barcode[] */
30
    private array $barcodes = [];
31
    private DateTimeImmutable $relevantDate;
32
    private string $appLaunchURL;
33
    /** @var int[] */
34
    private array $associatedStoreIdentifiers = [];
35
    private string $userInfo;
36
    private DateTimeImmutable $expirationDate;
37
    private bool $voided;
38
    /** @var Location[] */
39
    private array $locations = [];
40
    private int $maxDistance;
41
    private string $webServiceURL;
42
    private string $authenticationToken;
43
    private Color $foregroundColor;
44
    private Color $backgroundColor;
45
    private Color $labelColor;
46
    /** @var Image[] */
47
    private array $images = [];
48
    /** @var Field[] */
49
    private array $headerFields = [];
50
    /** @var Field[] */
51
    private array $primaryFields = [];
52
    /** @var Field[] */
53
    private array $auxiliaryFields = [];
54
    /** @var Field[] */
55
    private array $secondaryFields = [];
56
    /** @var Field[] */
57
    private array $backFields = [];
58
59
    /** @var SemanticTag[] */
60
    private array $semantics;
61
62
    public function __construct(string $serialNumber)
63
    {
64
        $this->serialNumber = $serialNumber;
65
    }
66
67
    public function setOrganizationName(string $organizationName): void
68
    {
69
        $this->organizationName = $organizationName;
70
    }
71
72
    public function setDescription(string $description): void
73
    {
74
        $this->description = $description;
75
    }
76
77
    public function setPassTypeIdentifier(string $passTypeIdentifier): void
78
    {
79
        $this->passTypeIdentifier = $passTypeIdentifier;
80
    }
81
82
    public function setTeamIdentifier(string $teamIdentifier): void
83
    {
84
        $this->teamIdentifier = $teamIdentifier;
85
    }
86
87
    public function setLogoText(string $logoText): void
88
    {
89
        $this->logoText = $logoText;
90
    }
91
92
    public function setRelevantDate(DateTimeImmutable $relevantDate): void
93
    {
94
        $this->relevantDate = $relevantDate;
95
    }
96
97
    public function setExpirationDate(DateTimeImmutable $expirationDate): void
98
    {
99
        $this->expirationDate = $expirationDate;
100
    }
101
102
    public function setBarcode(Barcode $barcode): void
103
    {
104
        $this->barcodes[] = $barcode;
105
    }
106
107
    public function addLocation(Location $location): void
108
    {
109
        $this->locations[] = $location;
110
    }
111
112
    public function setMaxDistance(int $maxDistance): void
113
    {
114
        $this->maxDistance = $maxDistance;
115
    }
116
117
    public function setWebService(string $url, string $authenticationToken): void
118
    {
119
        $this->webServiceURL = $url;
120
        $this->authenticationToken = $authenticationToken;
121
    }
122
123
    public function setForegroundColor(Color $foregroundColor): void
124
    {
125
        $this->foregroundColor = $foregroundColor;
126
    }
127
128
    public function setBackgroundColor(Color $backgroundColor): void
129
    {
130
        $this->backgroundColor = $backgroundColor;
131
    }
132
133
    public function setLabelColor(Color $labelColor): void
134
    {
135
        $this->labelColor = $labelColor;
136
    }
137
138
    public function addImage(Image $image): void
139
    {
140
        $this->images[] = $image;
141
    }
142
143
    public function addHeaderField(Field $field): void
144
    {
145
        $this->headerFields[] = $field;
146
    }
147
148
    public function addPrimaryField(Field $field): void
149
    {
150
        $this->primaryFields[] = $field;
151
    }
152
153
    public function addAuxiliaryField(Field $field): void
154
    {
155
        $this->auxiliaryFields[] = $field;
156
    }
157
158 3
    public function addSecondaryField(Field $field): void
159
    {
160 3
        $this->secondaryFields[] = $field;
161 3
    }
162
163 3
    public function addBackField(Field $field): void
164
    {
165 3
        $this->backFields[] = $field;
166 3
    }
167
168 3
    public function setAppLaunchURL(string $appLaunchURL): void
169
    {
170 3
        $this->appLaunchURL = $appLaunchURL;
171 3
    }
172
173 3
    public function addAssociatedStoreIdentifiers(int $associatedStoreIdentifiers): void
174
    {
175 3
        $this->associatedStoreIdentifiers[] = $associatedStoreIdentifiers;
176 3
    }
177
178 3
    public function addSemanticTag(SemanticTag $semanticTag): void
179
    {
180 3
        $this->semantics[] = $semanticTag;
181 3
    }
182
183 7
    public function setUserInfo(string $userInfo): void
184
    {
185 7
        $this->userInfo = $userInfo;
186 7
    }
187
188 7
    public function voided(): void
189
    {
190 7
        $this->voided = true;
191 7
    }
192
193 7
    public function isVoided(): bool
194
    {
195 7
        return isset($this->voided);
196 7
    }
197
198 7
    public function hasPassTypeIdentifier(): bool
199
    {
200 7
        return isset($this->passTypeIdentifier);
201 7
    }
202
203 6
    public function hasTeamIdentifier(): bool
204
    {
205 6
        return isset($this->teamIdentifier);
206 6
    }
207
208 7
    /**
209
     * @return array<int|string, mixed>
210 7
     * @throws MissingRequiredDataException
211 7
     */
212 7
    public function getData(): array
213
    {
214 7
        $this->validate();
215
216 7
        $data = $this->getGenericData();
217 7
        $data[static::TYPE] = $this->getFieldsData();
218
219 7
        return $data;
220
    }
221 7
222 7
    /**
223
     * @return array<int|string, mixed>
224 6
     */
225
    private function getGenericData(): array
226 6
    {
227 6
        $data = [
228
            'formatVersion' => $this->formatVersion,
229 6
            'passTypeIdentifier' => $this->passTypeIdentifier,
230
            'serialNumber' => $this->serialNumber,
231 6
            'teamIdentifier' => $this->teamIdentifier,
232 6
            'organizationName' => $this->organizationName,
233
            'description' => $this->description,
234 7
        ];
235
236 7
        if (isset($this->logoText)) {
237 7
            $data['logoText'] = $this->logoText;
238
        }
239 7
240
        if (count($this->barcodes) > 0) {
241 7
            $data['barcode'] = $this->barcodes[0]->toArray();
242 7
243
            foreach ($this->barcodes as $barcode) {
244 1
                $data['barcodes'][] = $barcode->toArray();
245
            }
246 1
        }
247 1
248
        if (isset($this->relevantDate)) {
249 7
            $data['relevantDate'] = $this->relevantDate->format(DateTimeInterface::W3C);
250
        }
251 7
252 7
        if (isset($this->expirationDate)) {
253
            $data['expirationDate'] = $this->expirationDate->format(DateTimeInterface::W3C);
254 7
        }
255
256 7
        if (isset($this->appLaunchURL)) {
257 7
            $data['appLaunchURL'] = $this->appLaunchURL;
258
        }
259 1
260
        if (count($this->associatedStoreIdentifiers) > 0) {
261 1
            $data['associatedStoreIdentifiers'] = $this->associatedStoreIdentifiers;
262 1
        }
263
264 1
        if (isset($this->userInfo)) {
265
            $data['userInfo'] = $this->userInfo;
266 1
        }
267 1
268
        if (isset($this->voided)) {
269 1
            $data['voided'] = true;
270
        }
271 1
272 1
        if (isset($this->locations)) {
273
            foreach ($this->locations as $location) {
274 6
                $data['locations'][] = $location->toArray();
275
            }
276 6
        }
277 6
278
        if (isset($this->maxDistance)) {
279 6
            $data['maxDistance'] = $this->maxDistance;
280
        }
281 6
282
        if (isset($this->webServiceURL) && isset($this->authenticationToken)) {
283
            $data['webServiceURL'] = $this->webServiceURL;
284 6
            $data['authenticationToken'] = $this->authenticationToken;
285
        }
286 6
287
        if (isset($this->foregroundColor)) {
288
            $data['foregroundColor'] = $this->foregroundColor->toString();
289 3
        }
290
291 3
        if (isset($this->backgroundColor)) {
292
            $data['backgroundColor'] = $this->backgroundColor->toString();
293 2
        }
294 2
295
        if (isset($this->labelColor)) {
296 2
            $data['labelColor'] = $this->labelColor->toString();
297
        }
298
299 2
        if (isset($this->semantics)) {
300
            foreach ($this->semantics as $tag) {
301
                $data['semantics'][$tag->getKey()] = $tag->getValue();
302 2
            }
303 2
        }
304 2
305 2
        return $data;
306 2
    }
307 2
308
    /**
309
     * @return array<int|string, mixed>
310 2
     */
311 1
    private function getFieldsData(): array
312
    {
313
        $data = [];
314 2
315 1
        foreach ($this->headerFields as $field) {
316
            $data['headerFields'][] = $field->getMetaData();
317 1
        }
318 1
        foreach ($this->primaryFields as $field) {
319
            $data['primaryFields'][] = $field->getMetaData();
320
        }
321
        foreach ($this->auxiliaryFields as $field) {
322 2
            $data['auxiliaryFields'][] = $field->getMetaData();
323 1
        }
324
        foreach ($this->secondaryFields as $field) {
325
            $data['secondaryFields'][] = $field->getMetaData();
326 2
        }
327
        foreach ($this->backFields as $field) {
328
            $data['backFields'][] = $field->getMetaData();
329
        }
330 2
331
        return $data;
332
    }
333
334 2
    /**
335
     * @return Image[]
336
     */
337
    public function getImages(): array
338 2
    {
339
        return $this->images;
340
    }
341
342 2
    /**
343
     * @throws LogicException
344
     * @throws MissingRequiredDataException
345
     */
346 2
    public function validate(): void
347 1
    {
348 1
        if (static::TYPE === null) {
0 ignored issues
show
introduced by
The condition static::TYPE === null is always true.
Loading history...
349
            throw new LogicException('Please implement protected const TYPE in class.');
350
        }
351
352 2
        if (!isset($this->passTypeIdentifier)) {
353
            throw new MissingRequiredDataException('Please specify the PassTypeIdentifier before requesting the manifest data.');
354
        }
355
356 2
        if (!isset($this->teamIdentifier)) {
357 1
            throw new MissingRequiredDataException('Please specify the TeamIdentifier before requesting the manifest data.');
358 1
        }
359
360
        if (!isset($this->organizationName)) {
361 2
            throw new MissingRequiredDataException('Please specify the OrganizationName before requesting the manifest data.');
362 1
        }
363
364
        if (!isset($this->description)) {
365 2
            throw new MissingRequiredDataException('Please specify the Description before requesting the manifest data.');
366 1
        }
367
    }
368
}
369