Barcode::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 4
nop 3
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 12
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\ApplePassbook\MetaData;
6
7
use LauLamanApps\ApplePassbook\Style\BarcodeFormat;
8
use LogicException;
9
10
class Barcode
11
{
12
    private string $altText;
13
    private BarcodeFormat $format;
14
    private string $message;
15
    private string $messageEncoding = 'iso-8859-1';
16
17
    public function __construct(?BarcodeFormat $format = null, ?string $message = null, ?string $altText = null)
18
    {
19
        $this->format = $format ?? BarcodeFormat::pdf417();
20
21
        if ($message) {
22
            $this->message = $message;
23
        }
24
25
        if ($altText) {
26
            $this->altText = $altText;
27
        }
28
    }
29
30
    public function setFormat(BarcodeFormat $format): void
31 4
    {
32
        $this->format = $format;
33 4
    }
34 4
35 4
    public function setMessage(string $message): void
36 4
    {
37
        $this->message = $message;
38 1
    }
39
40 1
    public function setAltText(string $altText): void
41 1
    {
42
        $this->altText = $altText;
43 1
    }
44
45 1
    public function setMessageEncoding(string $messageEncoding): void
46 1
    {
47
        $this->messageEncoding = $messageEncoding;
48 1
    }
49
50 1
    /**
51 1
     * @return array<string, string>
52
     */
53 1
    public function toArray(): array
54
    {
55 1
        $this->validate();
56 1
57
        $data = [
58 4
            'format' => (string) $this->format->getValue(),
59
            'message' => $this->message,
60
            'messageEncoding' => $this->messageEncoding,
61 4
        ];
62 4
63 4
        if (isset($this->altText)) {
64
            $data['altText'] = $this->altText;
65
        }
66 4
67 1
        return $data;
68
    }
69
70 4
    private function validate(): void
71
    {
72
        if (!isset($this->message)) {
73
            throw new LogicException('no message specified');
74
        }
75
    }
76
}
77