Passed
Push — master ( bfd7f8...fcc86f )
by Laurens
01:42 queued 30s
created

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