Passed
Pull Request — master (#78)
by
unknown
06:05
created

AbstractMarkupOutput::translateContents()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 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 getPrintableBordersTemplate(): string;
17
18
    abstract public function getHideInPrintableTemplate(): string;
19
20
    abstract public function getTextElementTemplate(): string;
21
22
    abstract public function getTitleElementTemplate(): string;
23
24
    abstract public function getTitleElementReceiptTemplate(): string;
25
26
    abstract public function getNewlineElementTemplate(): string;
27
28
    /**
29
     * @return string
30
     */
31
    public function getPaymentPart(): string
32
    {
33
        $paymentPart = $this->getPaymentPartTemplate();
34
35
        $paymentPart = $this->addSwissQrCodeImage($paymentPart);
36
        $paymentPart = $this->addInformationContent($paymentPart);
37
        $paymentPart = $this->addInformationContentReceipt($paymentPart);
38
        $paymentPart = $this->addCurrencyContent($paymentPart);
39
        $paymentPart = $this->addCurrencyContentReceipt($paymentPart);
40
        $paymentPart = $this->addAmountContent($paymentPart);
41
        $paymentPart = $this->addAmountContentReceipt($paymentPart);
42
        $paymentPart = $this->addFurtherInformationContent($paymentPart);
43
        $paymentPart = $this->addSeparatorContentIfNotPrintable($paymentPart);
44
        $paymentPart = $this->hideInPrintable($paymentPart);
45
46
        $paymentPart = $this->translateContents($paymentPart, $this->getLanguage());
47
48
        return $paymentPart;
49
    }
50
51
    /**
52
     * @param string $paymentPart
53
     * @return string
54
     */
55
    private function addSwissQrCodeImage(string $paymentPart): string
56
    {
57
        $qrCode = $this->getQrCode();
58
        $paymentPart = str_replace('{{ swiss-qr-image }}', $qrCode->writeDataUri(), $paymentPart);
59
60
        return $paymentPart;
61
    }
62
63
    /**
64
     * @param string $paymentPart
65
     * @return string
66
     */
67
    private function addInformationContent(string $paymentPart): string
68
    {
69
        $informationContent = '';
70
71
        foreach ($this->getInformationElements() as $informationElement) {
72
            $informationContentPart = $this->getContentElement($informationElement);
73
            $informationContent .= $informationContentPart;
74
        }
75
76
        $paymentPart = str_replace('{{ information-content }}', $informationContent, $paymentPart);
77
78
        return $paymentPart;
79
    }
80
81
    /**
82
     * @param string $paymentPart
83
     * @return string
84
     */
85
    private function addInformationContentReceipt(string $paymentPart): string
86
    {
87
        $informationContent = '';
88
89
        foreach ($this->getInformationElementsOfReceipt() as $informationElement) {
90
            $informationContent .= $this->getContentElement($informationElement, true);
91
        }
92
93
        $paymentPart = str_replace('{{ information-content-receipt }}', $informationContent, $paymentPart);
94
95
        return $paymentPart;
96
    }
97
98
    /**
99
     * @param string $paymentPart
100
     * @return string
101
     */
102
    private function addCurrencyContent(string $paymentPart): string
103
    {
104
        $currencyContent = '';
105
106
        foreach ($this->getCurrencyElements() as $currencyElement) {
107
            $currencyContent .= $this->getContentElement($currencyElement);
108
        }
109
110
        $paymentPart = str_replace('{{ currency-content }}', $currencyContent, $paymentPart);
111
112
        return $paymentPart;
113
    }
114
115
    /**
116
     * @param string $paymentPart
117
     * @return string
118
     */
119
    private function addCurrencyContentReceipt(string $paymentPart): string
120
    {
121
        $currencyContent = '';
122
123
        foreach ($this->getCurrencyElements() as $currencyElement) {
124
            $currencyContent .= $this->getContentElement($currencyElement, true);
125
        }
126
127
        $paymentPart = str_replace('{{ currency-content-receipt }}', $currencyContent, $paymentPart);
128
129
        return $paymentPart;
130
    }
131
132
    /**
133
     * @param string $paymentPart
134
     * @return string
135
     */
136
    private function addAmountContent(string $paymentPart): string
137
    {
138
        $amountContent = '';
139
140
        foreach ($this->getAmountElements() as $amountElement) {
141
            $amountContent .= $this->getContentElement($amountElement);
142
        }
143
144
        $paymentPart = str_replace('{{ amount-content }}', $amountContent, $paymentPart);
145
146
        return $paymentPart;
147
    }
148
149
    /**
150
     * @param string $paymentPart
151
     * @return string
152
     */
153
    private function addAmountContentReceipt(string $paymentPart): string
154
    {
155
        $amountContent = '';
156
157
        foreach ($this->getAmountElementsReceipt() as $amountElement) {
158
            $amountContent .= $this->getContentElement($amountElement, true);
159
        }
160
161
        $paymentPart = str_replace('{{ amount-content-receipt }}', $amountContent, $paymentPart);
162
163
        return $paymentPart;
164
    }
165
166
    /**
167
     * @param string $paymentPart
168
     * @return string
169
     */
170
    private function addFurtherInformationContent(string $paymentPart): string
171
    {
172
        $furtherInformationContent = '';
173
174
        foreach ($this->getFurtherInformationElements() as $furtherInformationElement) {
175
            $furtherInformationContent .= $this->getContentElement($furtherInformationElement);
176
        }
177
178
        $paymentPart = str_replace('{{ further-information-content }}', $furtherInformationContent, $paymentPart);
179
180
        return $paymentPart;
181
    }
182
183
    /**
184
     * @param string $paymentPart
185
     * @return string
186
     */
187
    private function addSeparatorContentIfNotPrintable(string $paymentPart): string
188
    {
189
        $printableBorders = '';
190
        if (true !== $this->isPrintable()) {
191
            $printableBorders = $this->getPrintableBordersTemplate();
192
        }
193
194
        $paymentPart = str_replace('{{ printable-content }}', $printableBorders, $paymentPart);
195
196
        return $paymentPart;
197
    }
198
199
    /**
200
     * @param string $paymentPart
201
     * @return string
202
     */
203
    private function hideInPrintable(string $paymentPart): string
204
    {
205
        $hideInPrintableContent = '';
206
        if (true === $this->isPrintable()) {
207
            $hideInPrintableContent = $this->getHideInPrintableTemplate();
208
        }
209
210
        $paymentPart = str_replace('{{ hide-in-printable }}', $hideInPrintableContent, $paymentPart);
211
212
        return $paymentPart;
213
    }
214
215
    /**
216
     * @param Title|Text|Placeholder $element Instance of OutputElementInterface.
217
     * @param bool $isReceiptPart
218
     * @return string
219
     */
220
    private function getContentElement($element, bool $isReceiptPart = false): string
221
    {
222
        if ($element instanceof Title) {
223
            $elementTemplate = $this->getTitleElementTemplate();
224
            if (true === $isReceiptPart) {
225
                $elementTemplate = $this->getTitleElementReceiptTemplate();
226
            }
227
            $elementString = str_replace('{{ title }}', $element->getTitle(), $elementTemplate);
228
229
            return $elementString;
230
        }
231
232
        if ($element instanceof Text) {
233
            $elementTemplate = $this->getTextElementTemplate();
234
            $elementTextString = str_replace(array("\r\n", "\r", "\n"), $this->getNewlineElementTemplate(), $element->getText());
235
            $elementString = str_replace('{{ text }}', $elementTextString, $elementTemplate);
236
237
            return $elementString;
238
        }
239
240
        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...
241
            $elementTemplate = $this->getPlaceholderElementTemplate();
242
            $elementString = $elementTemplate;
243
244
            $dataUri = 'data:image/png;base64,' . base64_encode(file_get_contents($element->getFile(Placeholder::FILE_TYPE_PNG)));
245
246
            // The svg version works but the images have empty space on top and bottom which makes it unnecessary hard to correctly place them.
247
//            $svgDoc = new \DOMDocument();
248
//            $svgDoc->loadXML(file_get_contents($element->getFile(Placeholder::FILE_TYPE_SVG))); // Take the png version since the svgs have a bad
249
//            $svg = $svgDoc->getElementsByTagName('svg');
250
//            $dataUri = 'data:image/svg+xml;base64,' . base64_encode($svg->item(0)->C14N());
251
252
            $elementString = str_replace('{{ placeholder-file }}', $dataUri, $elementString);
253
            $elementString = str_replace('{{ placeholder-width }}', (string)$element->getWidth(), $elementString);
254
            $elementString = str_replace('{{ placeholder-height }}', (string)$element->getHeight(), $elementString);
255
            $elementString = str_replace('{{ placeholder-id }}', $element->getType(), $elementString);
256
            $elementString = str_replace('{{ placeholder-float }}', $element->getFloat(), $elementString);
257
            $elementString = str_replace('{{ placeholder-margin-top }}', (string) $element->getMarginTop(), $elementString);
258
259
            return $elementString;
260
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 240 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...
261
    }
262
263
    /**
264
     * @param string $paymentPart
265
     * @param string $language
266
     * @return string
267
     */
268
    private function translateContents(string $paymentPart, string $language): string
269
    {
270
        $translations = Translation::getAllByLanguage($language);
271
        foreach ($translations as $key => $text) {
272
            $paymentPart = str_replace('{{ text.' . $key . ' }}', $text, $paymentPart);
273
        }
274
275
        return $paymentPart;
276
    }
277
}
278