AbstractOutput::getQrCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Sprain\SwissQrBill\PaymentPart\Output;
4
5
use Sprain\SwissQrBill\DataGroup\Element\PaymentReference;
6
use Sprain\SwissQrBill\PaymentPart\Output\Element\Placeholder;
7
use Sprain\SwissQrBill\PaymentPart\Output\Element\Text;
8
use Sprain\SwissQrBill\PaymentPart\Output\Element\Title;
9
use Sprain\SwissQrBill\QrBill;
10
use Sprain\SwissQrBill\QrCode\QrCode;
11
12
abstract class AbstractOutput
13
{
14
    protected QrBill $qrBill;
15
    protected string $language;
16
    protected bool $printable;
17
    protected string $qrCodeImageFormat;
18
19
    public function __construct(QrBill $qrBill, string $language)
20
    {
21
        $this->qrBill = $qrBill;
22
        $this->language = $language;
23
        $this->printable = false;
24
        $this->qrCodeImageFormat = QrCode::FILE_FORMAT_SVG;
25
    }
26
27
    public function getQrBill(): ?QrBill
28
    {
29
        return $this->qrBill;
30
    }
31
32
    public function getLanguage(): ?string
33
    {
34
        return $this->language;
35
    }
36
37
    public function setPrintable(bool $printable): self
38
    {
39
        $this->printable = $printable;
40
41
        return $this;
42
    }
43
44
    public function isPrintable(): bool
45
    {
46
        return $this->printable;
47
    }
48
49
    public function setQrCodeImageFormat(string $fileExtension): self
50
    {
51
        $this->qrCodeImageFormat = $fileExtension;
52
53
        return $this;
54
    }
55
56
    public function getQrCodeImageFormat(): string
57
    {
58
        return $this->qrCodeImageFormat;
59
    }
60
61
    protected function getInformationElements(): array
62
    {
63
        $informationElements = [];
64
65
        $informationElements[] = Title::create('text.creditor');
66
        $informationElements[] = Text::create($this->qrBill->getCreditorInformation()->getFormattedIban() . "\n" . $this->qrBill->getCreditor()->getFullAddress());
67
68
        if ($this->qrBill->getPaymentReference()->getType() !== PaymentReference::TYPE_NON) {
69
            $informationElements[] = Title::create('text.reference');
70
            $informationElements[] = Text::create($this->qrBill->getPaymentReference()->getFormattedReference());
71
        }
72
73
        if ($this->qrBill->getAdditionalInformation()) {
74
            $informationElements[] = Title::create('text.additionalInformation');
75
            $informationElements[] = Text::create($this->qrBill->getAdditionalInformation()->getFormattedString());
76
        }
77
78
        if ($this->qrBill->getUltimateDebtor()) {
79
            $informationElements[] = Title::create('text.payableBy');
80
            $informationElements[] = Text::create($this->qrBill->getUltimateDebtor()->getFullAddress());
81
        } else {
82
            $informationElements[] = Title::create('text.payableByName');
83
            $informationElements[] = Placeholder::create(Placeholder::PLACEHOLDER_TYPE_PAYABLE_BY);
84
        }
85
86
        return $informationElements;
87
    }
88
89
    protected function getInformationElementsOfReceipt(): array
90
    {
91
        $informationElements = [];
92
93
        $informationElements[] = Title::create('text.creditor');
94
        $informationElements[] = Text::create($this->qrBill->getCreditorInformation()->getFormattedIban() . "\n" . $this->qrBill->getCreditor()->getFullAddress());
95
96
        if ($this->qrBill->getPaymentReference()->getType() !== PaymentReference::TYPE_NON) {
97
            $informationElements[] = Title::create('text.reference');
98
            $informationElements[] = Text::create($this->qrBill->getPaymentReference()->getFormattedReference());
99
        }
100
101
        if ($this->qrBill->getUltimateDebtor()) {
102
            $informationElements[] = Title::create('text.payableBy');
103
            $informationElements[] = Text::create($this->qrBill->getUltimateDebtor()->getFullAddress());
104
        } else {
105
            $informationElements[] = Title::create('text.payableByName');
106
            $informationElements[] = Placeholder::create(Placeholder::PLACEHOLDER_TYPE_PAYABLE_BY_RECEIPT);
107
        }
108
109
        return $informationElements;
110
    }
111
112
    protected function getCurrencyElements(): array
113
    {
114
        $currencyElements = [];
115
116
        $currencyElements[] = Title::create('text.currency');
117
        $currencyElements[] = Text::create($this->qrBill->getPaymentAmountInformation()->getCurrency());
118
119
        return $currencyElements;
120
    }
121
122
    protected function getAmountElements(): array
123
    {
124
        $amountElements = [];
125
126
        $amountElements[] = Title::create('text.amount');
127
128
        if (null === $this->qrBill->getPaymentAmountInformation()->getAmount()) {
129
            $amountElements[] = Placeholder::create(Placeholder::PLACEHOLDER_TYPE_AMOUNT);
130
        } else {
131
            $amountElements[] = Text::create($this->qrBill->getPaymentAmountInformation()->getFormattedAmount());
132
        }
133
134
        return $amountElements;
135
    }
136
137
    protected function getAmountElementsReceipt(): array
138
    {
139
        $amountElements = [];
140
141
        $amountElements[] = Title::create('text.amount');
142
143
        if (null === $this->qrBill->getPaymentAmountInformation()->getAmount()) {
144
            $amountElements[] = Placeholder::create(Placeholder::PLACEHOLDER_TYPE_AMOUNT_RECEIPT);
145
        } else {
146
            $amountElements[] = Text::create($this->qrBill->getPaymentAmountInformation()->getFormattedAmount());
147
        }
148
149
        return $amountElements;
150
    }
151
152
    protected function getFurtherInformationElements(): array
153
    {
154
        $furtherInformationElements = [];
155
156
        $furtherInformationLines= [];
157
        foreach ($this->qrBill->getAlternativeSchemes() as $alternativeScheme) {
158
            $furtherInformationLines[] = $alternativeScheme->getParameter();
159
        }
160
        $furtherInformationElements[] = Text::create(implode("\n", $furtherInformationLines));
161
162
        return $furtherInformationElements;
163
    }
164
165
    protected function getQrCode(): QrCode
166
    {
167
        $qrCode = $this->qrBill->getQrCode($this->getQrCodeImageFormat());
168
169
        return $qrCode;
170
    }
171
}
172