Passed
Push — master ( fcc86f...3fa4e6 )
by Laurens
01:26
created

Passbook::prohibitSharing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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