Passed
Pull Request — master (#53)
by Manuel
04:17
created

HtmlOutput::addPrintableContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput;
4
5
use Sprain\SwissQrBill\PaymentPart\Output\AbstractOutput;
6
use Sprain\SwissQrBill\PaymentPart\Output\Element\OutputElementInterface;
7
use Sprain\SwissQrBill\PaymentPart\Output\Element\Placeholder;
8
use Sprain\SwissQrBill\PaymentPart\Output\Element\Text;
9
use Sprain\SwissQrBill\PaymentPart\Output\Element\Title;
10
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\PlaceholderElementTemplate;
11
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\PrintableStylesTemplate;
12
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\TextElementTemplate;
13
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\PaymentPartTemplate;
14
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\TitleElementTemplate;
15
use Sprain\SwissQrBill\PaymentPart\Output\OutputInterface;
16
use Sprain\SwissQrBill\PaymentPart\Translation\Translation;
17
18
final class HtmlOutput extends AbstractOutput implements OutputInterface
19
{
20
    public function getPaymentPart(): string
21
    {
22
        $paymentPart = PaymentPartTemplate::TEMPLATE;
23
24
        $paymentPart = $this->addSwissQrCodeImage($paymentPart);
25
        $paymentPart = $this->addInformationContent($paymentPart);
26
        $paymentPart = $this->addInformationContentReceipt($paymentPart);
27
        $paymentPart = $this->addCurrencyContent($paymentPart);
28
        $paymentPart = $this->addAmountContent($paymentPart);
29
        $paymentPart = $this->addAmountContentReceipt($paymentPart);
30
        $paymentPart = $this->addFurtherInformationContent($paymentPart);
31
        $paymentPart = $this->hideSeparatorContentIfPrintable($paymentPart);
32
33
        $paymentPart = $this->translateContents($paymentPart, $this->getLanguage());
34
35
        return $paymentPart;
36
    }
37
38
    private function addSwissQrCodeImage(string $paymentPart): string
39
    {
40
        $qrCode = $this->getQrCode();
41
        $paymentPart = str_replace('{{ swiss-qr-image }}', $qrCode->writeDataUri(), $paymentPart);
42
43
        return $paymentPart;
44
    }
45
46
    private function addInformationContent(string $paymentPart): string
47
    {
48
        $informationContent = '';
49
50
        foreach ($this->getInformationElements() as $informationElement) {
51
            $informationContentPart = $this->getContentElement($informationElement);
52
            $informationContent .= $informationContentPart;
53
        }
54
55
        $paymentPart = str_replace('{{ information-content }}', $informationContent, $paymentPart);
56
57
        return $paymentPart;
58
    }
59
60
    private function addInformationContentReceipt(string $paymentPart): string
61
    {
62
        $informationContent = '';
63
64
        foreach ($this->getInformationElementsOfReceipt() as $informationElement) {
65
            $informationContent .= $this->getContentElement($informationElement);
66
        }
67
68
        $paymentPart = str_replace('{{ information-content-receipt }}', $informationContent, $paymentPart);
69
70
        return $paymentPart;
71
    }
72
73
    private function addCurrencyContent(string $paymentPart): string
74
    {
75
        $currencyContent = '';
76
77
        foreach ($this->getCurrencyElements() as $currencyElement) {
78
            $currencyContent .= $this->getContentElement($currencyElement);
79
        }
80
81
        $paymentPart = str_replace('{{ currency-content }}', $currencyContent, $paymentPart);
82
83
        return $paymentPart;
84
    }
85
86
    private function addAmountContent(string $paymentPart): string
87
    {
88
        $amountContent = '';
89
90
        foreach ($this->getAmountElements() as $amountElement) {
91
            $amountContent .= $this->getContentElement($amountElement);
92
        }
93
94
        $paymentPart = str_replace('{{ amount-content }}', $amountContent, $paymentPart);
95
96
        return $paymentPart;
97
    }
98
99
    private function addAmountContentReceipt(string $paymentPart): string
100
    {
101
        $amountContent = '';
102
103
        foreach ($this->getAmountElementsReceipt() as $amountElement) {
104
            $amountContent .= $this->getContentElement($amountElement);
105
        }
106
107
        $paymentPart = str_replace('{{ amount-content-receipt }}', $amountContent, $paymentPart);
108
109
        return $paymentPart;
110
    }
111
112
    private function addFurtherInformationContent(string $paymentPart): string
113
    {
114
        $furtherInformationContent = '';
115
116
        foreach ($this->getFurtherInformationElements() as $furtherInformationElement) {
117
            $furtherInformationContent .= $this->getContentElement($furtherInformationElement);
118
        }
119
120
        $paymentPart = str_replace('{{ further-information-content }}', $furtherInformationContent, $paymentPart);
121
122
        return $paymentPart;
123
    }
124
125
    private function hideSeparatorContentIfPrintable(string $paymentPart): string
126
    {
127
        $printableStyles = '';
128
        if ($this->isPrintable()) {
129
            $printableStyles = PrintableStylesTemplate::TEMPLATE;
130
        }
131
132
        $paymentPart = str_replace('{{ printable-content }}', $printableStyles, $paymentPart);
133
134
        return $paymentPart;
135
    }
136
137
    /**
138
     * @param Title|Text|Placeholder $element Instance of OutputElementInterface.
139
     */
140
    private function getContentElement($element): string
141
    {
142
        if ($element instanceof Title) {
143
            $elementTemplate = TitleElementTemplate::TEMPLATE;
144
            $elementString = str_replace('{{ title }}', $element->getTitle(), $elementTemplate);
145
146
            return $elementString;
147
        }
148
149
        if ($element instanceof Text) {
150
            $elementTemplate = TextElementTemplate::TEMPLATE;
151
            $elementString = str_replace('{{ text }}', nl2br($element->getText()), $elementTemplate);
152
153
            return $elementString;
154
        }
155
156
        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...
157
            $elementTemplate = PlaceholderElementTemplate::TEMPLATE;
158
            $elementString = $elementTemplate;
159
160
            $svgDoc = new \DOMDocument();
161
            $svgDoc->loadXML(file_get_contents($element->getFile()));
162
            $svg = $svgDoc->getElementsByTagName('svg');
163
            $dataUri = 'data:image/svg+xml;base64,' . base64_encode($svg->item(0)->C14N());
164
165
            $elementString = str_replace('{{ file }}', $dataUri, $elementString);
166
            $elementString = str_replace('{{ width }}', (string) $element->getWidth(), $elementString);
167
            $elementString = str_replace('{{ height }}', (string) $element->getHeight(), $elementString);
168
            $elementString = str_replace('{{ id }}', $element->getType(), $elementString);
169
170
            return $elementString;
171
        }
172
    }
173
174
    private function translateContents(string $paymentPart, string $language): string
175
    {
176
        $translations = Translation::getAllByLanguage($language);
177
        foreach ($translations as $key => $text) {
178
            $paymentPart = str_replace('{{ text.' . $key . ' }}', $text, $paymentPart);
179
        }
180
181
        return $paymentPart;
182
    }
183
}
184