Passed
Push — master ( c85d21...9ff213 )
by Laurens
01:24
created

Passbook::setExpirationDate()   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\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 setExpirationDate(DateTimeImmutable $expirationDate): void
96
    {
97
        $this->expirationDate = $expirationDate;
98
    }
99
100
    public function setBarcode(Barcode $barcode): void
101
    {
102
        $this->barcodes[] = $barcode;
103
    }
104
105
    public function addLocation(Location $location): void
106
    {
107
        $this->locations[] = $location;
108
    }
109
110
    public function setMaxDistance(int $maxDistance): void
111
    {
112
        $this->maxDistance = $maxDistance;
113
    }
114
115
    public function setWebService(string $url, string $authenticationToken): void
116
    {
117
        $this->webServiceURL = $url;
118
        $this->authenticationToken = $authenticationToken;
119
    }
120
121
    public function setForegroundColor(Color $foregroundColor): void
122
    {
123
        $this->foregroundColor = $foregroundColor;
124
    }
125
126
    public function setBackgroundColor(Color $backgroundColor): void
127
    {
128
        $this->backgroundColor = $backgroundColor;
129
    }
130
131
    public function setLabelColor(Color $labelColor): void
132
    {
133
        $this->labelColor = $labelColor;
134
    }
135
136
    public function addImage(Image $image): void
137
    {
138
        $this->images[] = $image;
139
    }
140
141
    public function addHeaderField(Field $field): void
142
    {
143
        $this->headerFields[] = $field;
144
    }
145
146
    public function addPrimaryField(Field $field): void
147
    {
148
        $this->primaryFields[] = $field;
149
    }
150
151
    public function addAuxiliaryField(Field $field): void
152
    {
153
        $this->auxiliaryFields[] = $field;
154
    }
155
156
    public function addSecondaryField(Field $field): void
157
    {
158 3
        $this->secondaryFields[] = $field;
159
    }
160 3
161 3
    public function addBackField(Field $field): void
162
    {
163 3
        $this->backFields[] = $field;
164
    }
165 3
166 3
    public function setAppLaunchURL(string $appLaunchURL): void
167
    {
168 3
        $this->appLaunchURL = $appLaunchURL;
169
    }
170 3
171 3
    public function addAssociatedStoreIdentifiers(int $associatedStoreIdentifiers): void
172
    {
173 3
        $this->associatedStoreIdentifiers[] = $associatedStoreIdentifiers;
174
    }
175 3
176 3
    public function setUserInfo(string $userInfo): void
177
    {
178 3
        $this->userInfo = $userInfo;
179
    }
180 3
181 3
    public function voided(): void
182
    {
183 7
        $this->voided = true;
184
    }
185 7
186 7
    public function isVoided(): bool
187
    {
188 7
        return isset($this->voided);
189
    }
190 7
191 7
    public function hasPassTypeIdentifier(): bool
192
    {
193 7
        return isset($this->passTypeIdentifier);
194
    }
195 7
196 7
    public function hasTeamIdentifier(): bool
197
    {
198 7
        return isset($this->teamIdentifier);
199
    }
200 7
201 7
    /**
202
     * @return array<int|string, mixed>
203 6
     * @throws MissingRequiredDataException
204
     */
205 6
    public function getData(): array
206 6
    {
207
        $this->validate();
208 7
209
        $data = $this->getGenericData();
210 7
        $data[static::TYPE] = $this->getFieldsData();
211 7
212 7
        return $data;
213
    }
214 7
215
    /**
216 7
     * @return array<int|string, mixed>
217 7
     */
218
    private function getGenericData(): array
219 7
    {
220
        $data = [
221 7
            'formatVersion' => $this->formatVersion,
222 7
            'passTypeIdentifier' => $this->passTypeIdentifier,
223
            'serialNumber' => $this->serialNumber,
224 6
            'teamIdentifier' => $this->teamIdentifier,
225
            'organizationName' => $this->organizationName,
226 6
            'description' => $this->description,
227 6
        ];
228
229 6
        if (isset($this->logoText)) {
230
            $data['logoText'] = $this->logoText;
231 6
        }
232 6
233
        if (count($this->barcodes) > 0) {
234 7
            $data['barcode'] = $this->barcodes[0]->toArray();
235
236 7
            foreach ($this->barcodes as $barcode) {
237 7
                $data['barcodes'][] = $barcode->toArray();
238
            }
239 7
        }
240
241 7
        if (isset($this->relevantDate)) {
242 7
            $data['relevantDate'] = $this->relevantDate->format(DateTimeInterface::W3C);
243
        }
244 1
245
        if (isset($this->expirationDate)) {
246 1
            $data['expirationDate'] = $this->expirationDate->format(DateTimeInterface::W3C);
247 1
        }
248
249 7
        if (isset($this->appLaunchURL)) {
250
            $data['appLaunchURL'] = $this->appLaunchURL;
251 7
        }
252 7
253
        if (count($this->associatedStoreIdentifiers) > 0) {
254 7
            $data['associatedStoreIdentifiers'] = $this->associatedStoreIdentifiers;
255
        }
256 7
257 7
        if (isset($this->userInfo)) {
258
            $data['userInfo'] = $this->userInfo;
259 1
        }
260
261 1
        if (isset($this->voided)) {
262 1
            $data['voided'] = true;
263
        }
264 1
265
        if (isset($this->locations)) {
266 1
            foreach ($this->locations as $location) {
267 1
                $data['locations'][] = $location->toArray();
268
            }
269 1
        }
270
271 1
        if (isset($this->maxDistance)) {
272 1
            $data['maxDistance'] = $this->maxDistance;
273
        }
274 6
275
        if (isset($this->webServiceURL) && isset($this->authenticationToken)) {
276 6
            $data['webServiceURL'] = $this->webServiceURL;
277 6
            $data['authenticationToken'] = $this->authenticationToken;
278
        }
279 6
280
        if (isset($this->foregroundColor)) {
281 6
            $data['foregroundColor'] = $this->foregroundColor->toString();
282
        }
283
284 6
        if (isset($this->backgroundColor)) {
285
            $data['backgroundColor'] = $this->backgroundColor->toString();
286 6
        }
287
288
        if (isset($this->labelColor)) {
289 3
            $data['labelColor'] = $this->labelColor->toString();
290
        }
291 3
292
        return $data;
293 2
    }
294 2
295
    /**
296 2
     * @return array<int|string, mixed>
297
     */
298
    private function getFieldsData(): array
299 2
    {
300
        $data = [];
301
302 2
        foreach ($this->headerFields as $field) {
303 2
            $data['headerFields'][] = $field->getMetaData();
304 2
        }
305 2
        foreach ($this->primaryFields as $field) {
306 2
            $data['primaryFields'][] = $field->getMetaData();
307 2
        }
308
        foreach ($this->auxiliaryFields as $field) {
309
            $data['auxiliaryFields'][] = $field->getMetaData();
310 2
        }
311 1
        foreach ($this->secondaryFields as $field) {
312
            $data['secondaryFields'][] = $field->getMetaData();
313
        }
314 2
        foreach ($this->backFields as $field) {
315 1
            $data['backFields'][] = $field->getMetaData();
316
        }
317 1
318 1
        return $data;
319
    }
320
321
    /**
322 2
     * @return Image[]
323 1
     */
324
    public function getImages(): array
325
    {
326 2
        return $this->images;
327
    }
328
329
    /**
330 2
     * @throws LogicException
331
     * @throws MissingRequiredDataException
332
     */
333
    public function validate(): void
334 2
    {
335
        if (static::TYPE === null) {
0 ignored issues
show
introduced by
The condition static::TYPE === null is always true.
Loading history...
336
            throw new LogicException('Please implement protected const TYPE in class.');
337
        }
338 2
339
        if (!isset($this->passTypeIdentifier)) {
340
            throw new MissingRequiredDataException('Please specify the PassTypeIdentifier before requesting the manifest data.');
341
        }
342 2
343
        if (!isset($this->teamIdentifier)) {
344
            throw new MissingRequiredDataException('Please specify the TeamIdentifier before requesting the manifest data.');
345
        }
346 2
347 1
        if (!isset($this->organizationName)) {
348 1
            throw new MissingRequiredDataException('Please specify the OrganizationName before requesting the manifest data.');
349
        }
350
351
        if (!isset($this->description)) {
352 2
            throw new MissingRequiredDataException('Please specify the Description before requesting the manifest data.');
353
        }
354
    }
355
}
356