Passed
Pull Request — master (#40)
by Manuel
02:15
created

AbstractOutput::getQrCodeImageFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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