Passed
Push — master ( 45b9b9...c85d21 )
by Laurens
01:35
created

Passbook::validate()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 8.304

Importance

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