Passed
Pull Request — master (#78)
by
unknown
11:13
created

AbstractMarkupOutput   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 27
eloc 86
dl 0
loc 254
c 0
b 0
f 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A addAmountContentReceipt() 0 11 2
A addFurtherInformationContent() 0 11 2
A addSeparatorContentIfNotPrintable() 0 10 2
A addInformationContentReceipt() 0 11 2
A addInformationContent() 0 12 2
A getContentElement() 0 40 5
A getPaymentPart() 0 17 1
A addCurrencyContentReceipt() 0 11 2
A addSwissQrCodeImage() 0 6 1
A addAmountContent() 0 11 2
A translateContents() 0 15 4
A addCurrencyContent() 0 11 2
1
<?php
2
3
namespace Sprain\SwissQrBill\PaymentPart\Output;
4
5
use Sprain\SwissQrBill\PaymentPart\Output\Element\Placeholder;
6
use Sprain\SwissQrBill\PaymentPart\Output\Element\Text;
7
use Sprain\SwissQrBill\PaymentPart\Output\Element\Title;
8
use Sprain\SwissQrBill\PaymentPart\Translation\Translation;
9
10
abstract class AbstractMarkupOutput extends AbstractOutput implements OutputInterface
11
{
12
    abstract public function getPaymentPartTemplate(): string;
13
14
    abstract public function getPlaceholderElementTemplate(): string;
15
16
    abstract public function getPrintableStylesTemplate(): string;
17
18
    abstract public function getTextElementTemplate(): string;
19
20
    abstract public function getTitleElementTemplate(): string;
21
22
    abstract public function getTitleElementReceiptTemplate(): string;
23
24
    abstract public function getNewlineElementTemplate(): string;
25
26
    /**
27
     * @return string
28
     */
29
    public function getPaymentPart(): string
30
    {
31
        $paymentPart = $this->getPaymentPartTemplate();
32
33
        $paymentPart = $this->addSwissQrCodeImage($paymentPart);
34
        $paymentPart = $this->addInformationContent($paymentPart);
35
        $paymentPart = $this->addInformationContentReceipt($paymentPart);
36
        $paymentPart = $this->addCurrencyContent($paymentPart);
37
        $paymentPart = $this->addCurrencyContentReceipt($paymentPart);
38
        $paymentPart = $this->addAmountContent($paymentPart);
39
        $paymentPart = $this->addAmountContentReceipt($paymentPart);
40
        $paymentPart = $this->addFurtherInformationContent($paymentPart);
41
        $paymentPart = $this->addSeparatorContentIfNotPrintable($paymentPart);
42
43
        $paymentPart = $this->translateContents($paymentPart, $this->getLanguage());
44
45
        return $paymentPart;
46
    }
47
48
    /**
49
     * @param string $paymentPart
50
     * @return string
51
     */
52
    private function addSwissQrCodeImage(string $paymentPart): string
53
    {
54
        $qrCode = $this->getQrCode();
55
        $paymentPart = str_replace('{{ swiss-qr-image }}', $qrCode->writeDataUri(), $paymentPart);
56
57
        return $paymentPart;
58
    }
59
60
    /**
61
     * @param string $paymentPart
62
     * @return string
63
     */
64
    private function addInformationContent(string $paymentPart): string
65
    {
66
        $informationContent = '';
67
68
        foreach ($this->getInformationElements() as $informationElement) {
69
            $informationContentPart = $this->getContentElement($informationElement);
70
            $informationContent .= $informationContentPart;
71
        }
72
73
        $paymentPart = str_replace('{{ information-content }}', $informationContent, $paymentPart);
74
75
        return $paymentPart;
76
    }
77
78
    /**
79
     * @param string $paymentPart
80
     * @return string
81
     */
82
    private function addInformationContentReceipt(string $paymentPart): string
83
    {
84
        $informationContent = '';
85
86
        foreach ($this->getInformationElementsOfReceipt() as $informationElement) {
87
            $informationContent .= $this->getContentElement($informationElement, true);
88
        }
89
90
        $paymentPart = str_replace('{{ information-content-receipt }}', $informationContent, $paymentPart);
91
92
        return $paymentPart;
93
    }
94
95
    /**
96
     * @param string $paymentPart
97
     * @return string
98
     */
99
    private function addCurrencyContent(string $paymentPart): string
100
    {
101
        $currencyContent = '';
102
103
        foreach ($this->getCurrencyElements() as $currencyElement) {
104
            $currencyContent .= $this->getContentElement($currencyElement);
105
        }
106
107
        $paymentPart = str_replace('{{ currency-content }}', $currencyContent, $paymentPart);
108
109
        return $paymentPart;
110
    }
111
112
    /**
113
     * @param string $paymentPart
114
     * @return string
115
     */
116
    private function addCurrencyContentReceipt(string $paymentPart): string
117
    {
118
        $currencyContent = '';
119
120
        foreach ($this->getCurrencyElements() as $currencyElement) {
121
            $currencyContent .= $this->getContentElement($currencyElement, true);
122
        }
123
124
        $paymentPart = str_replace('{{ currency-content-receipt }}', $currencyContent, $paymentPart);
125
126
        return $paymentPart;
127
    }
128
129
    /**
130
     * @param string $paymentPart
131
     * @return string
132
     */
133
    private function addAmountContent(string $paymentPart): string
134
    {
135
        $amountContent = '';
136
137
        foreach ($this->getAmountElements() as $amountElement) {
138
            $amountContent .= $this->getContentElement($amountElement);
139
        }
140
141
        $paymentPart = str_replace('{{ amount-content }}', $amountContent, $paymentPart);
142
143
        return $paymentPart;
144
    }
145
146
    /**
147
     * @param string $paymentPart
148
     * @return string
149
     */
150
    private function addAmountContentReceipt(string $paymentPart): string
151
    {
152
        $amountContent = '';
153
154
        foreach ($this->getAmountElementsReceipt() as $amountElement) {
155
            $amountContent .= $this->getContentElement($amountElement, true);
156
        }
157
158
        $paymentPart = str_replace('{{ amount-content-receipt }}', $amountContent, $paymentPart);
159
160
        return $paymentPart;
161
    }
162
163
    /**
164
     * @param string $paymentPart
165
     * @return string
166
     */
167
    private function addFurtherInformationContent(string $paymentPart): string
168
    {
169
        $furtherInformationContent = '';
170
171
        foreach ($this->getFurtherInformationElements() as $furtherInformationElement) {
172
            $furtherInformationContent .= $this->getContentElement($furtherInformationElement);
173
        }
174
175
        $paymentPart = str_replace('{{ further-information-content }}', $furtherInformationContent, $paymentPart);
176
177
        return $paymentPart;
178
    }
179
180
    /**
181
     * @param string $paymentPart
182
     * @return string
183
     */
184
    private function addSeparatorContentIfNotPrintable(string $paymentPart): string
185
    {
186
        $printableStyles = '';
187
        if (true !== $this->isPrintable()) {
188
            $printableStyles = $this->getPrintableStylesTemplate();
189
        }
190
191
        $paymentPart = str_replace('{{ printable-content }}', $printableStyles, $paymentPart);
192
193
        return $paymentPart;
194
    }
195
196
    /**
197
     * @param Title|Text|Placeholder $element Instance of OutputElementInterface.
198
     * @param bool $isReceiptPart
199
     * @return string
200
     */
201
    private function getContentElement($element, bool $isReceiptPart = false): string
202
    {
203
        if ($element instanceof Title) {
204
            $elementTemplate = $this->getTitleElementTemplate();
205
            if (true === $isReceiptPart) {
206
                $elementTemplate = $this->getTitleElementReceiptTemplate();
207
            }
208
            $elementString = str_replace('{{ title }}', $element->getTitle(), $elementTemplate);
209
210
            return $elementString;
211
        }
212
213
        if ($element instanceof Text) {
214
            $elementTemplate = $this->getTextElementTemplate();
215
            $elementTextString = str_replace(array("\r\n", "\r", "\n"), $this->getNewlineElementTemplate(), $element->getText());
216
            $elementString = str_replace('{{ text }}', $elementTextString, $elementTemplate);
217
218
            return $elementString;
219
        }
220
221
        if ($element instanceof Placeholder) {
0 ignored issues
show
introduced by
$element is always a sub-type of Sprain\SwissQrBill\Payme...put\Element\Placeholder.
Loading history...
222
            $elementTemplate = $this->getPlaceholderElementTemplate();
223
            $elementString = $elementTemplate;
224
225
            $dataUri = 'data:image/png;base64,' . base64_encode(file_get_contents($element->getFile(Placeholder::FILE_TYPE_PNG)));
226
227
            // The svg version works but the images have empty space on top and bottom which makes it unnecessary hard to correctly place them.
228
//            $svgDoc = new \DOMDocument();
229
//            $svgDoc->loadXML(file_get_contents($element->getFile(Placeholder::FILE_TYPE_SVG))); // Take the png version since the svgs have a bad
230
//            $svg = $svgDoc->getElementsByTagName('svg');
231
//            $dataUri = 'data:image/svg+xml;base64,' . base64_encode($svg->item(0)->C14N());
232
233
            $elementString = str_replace('{{ placeholder-file }}', $dataUri, $elementString);
234
            $elementString = str_replace('{{ placeholder-width }}', (string)$element->getWidth(), $elementString);
235
            $elementString = str_replace('{{ placeholder-height }}', (string)$element->getHeight(), $elementString);
236
            $elementString = str_replace('{{ placeholder-id }}', $element->getType(), $elementString);
237
            $elementString = str_replace('{{ placeholder-float }}', $element->getFloat(), $elementString);
238
            $elementString = str_replace('{{ placeholder-margin-top }}', $element->getMarginTop(), $elementString);
239
240
            return $elementString;
241
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 221 is false. This is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
242
    }
243
244
    /**
245
     * @param string $paymentPart
246
     * @param string $language
247
     * @return string
248
     */
249
    private function translateContents(string $paymentPart, string $language): string
250
    {
251
        $translations = Translation::getAllByLanguage($language);
252
        foreach ($translations as $key => $text) {
253
254
            if ('separate' === $key && true === $this->isPrintable()) {
255
                // Do not display the separator text at all when printable is true.
256
                $paymentPart = str_replace('{{ text.' . $key . ' }}', '', $paymentPart);
257
                continue;
258
            }
259
260
            $paymentPart = str_replace('{{ text.' . $key . ' }}', $text, $paymentPart);
261
        }
262
263
        return $paymentPart;
264
    }
265
}
266