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

addSeparatorContentIfNotPrintable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Sprain\SwissQrBill\PaymentPart\Output\MarkupOutput;
4
5
use Sprain\SwissQrBill\PaymentPart\Output\AbstractOutput;
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\PaymentPart\Output\OutputInterface;
10
use Sprain\SwissQrBill\PaymentPart\Translation\Translation;
11
12
abstract class AbstractMarkupOutput extends AbstractOutput implements OutputInterface
13
{
14
    abstract function getPaymentPartTemplate(): string;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
    abstract function getPlaceholderElementTemplate(): string;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
16
    abstract function getPrintableStylesTemplate(): string;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
    abstract function getTextElementTemplate(): string;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
    abstract function getTitleElementTemplate(): string;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    abstract function getTitleElementReceiptTemplate(): string;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
    abstract function getNewlineElementTemplate(): string;
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

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