Passed
Push — master ( da9b2c...acc548 )
by Manuel
04:25
created

HtmlOutput::getPaymentPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput;
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\HtmlOutput\Template\PlaceholderElementTemplate;
10
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\PrintableStylesTemplate;
11
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\TextElementTemplate;
12
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\PaymentPartTemplate;
13
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\TitleElementTemplate;
14
use Sprain\SwissQrBill\PaymentPart\Output\OutputInterface;
15
use Sprain\SwissQrBill\PaymentPart\Translation\Translation;
16
17
final class HtmlOutput extends AbstractOutput implements OutputInterface
18
{
19
    public function getPaymentPart(): string
20
    {
21
        $paymentPart = PaymentPartTemplate::TEMPLATE;
22
23
        $paymentPart = $this->addSwissQrCodeImage($paymentPart);
24
        $paymentPart = $this->addInformationContent($paymentPart);
25
        $paymentPart = $this->addInformationContentReceipt($paymentPart);
26
        $paymentPart = $this->addCurrencyContent($paymentPart);
27
        $paymentPart = $this->addAmountContent($paymentPart);
28
        $paymentPart = $this->addAmountContentReceipt($paymentPart);
29
        $paymentPart = $this->addFurtherInformationContent($paymentPart);
30
        $paymentPart = $this->hideSeparatorContentIfPrintable($paymentPart);
31
32
        $paymentPart = $this->translateContents($paymentPart, $this->getLanguage());
33
34
        return $paymentPart;
35
    }
36
37
    private function addSwissQrCodeImage(string $paymentPart): string
38
    {
39
        $qrCode = $this->getQrCode();
40
        $paymentPart = str_replace('{{ swiss-qr-image }}', $qrCode->writeDataUri(), $paymentPart);
41
42
        return $paymentPart;
43
    }
44
45
    private function addInformationContent(string $paymentPart): string
46
    {
47
        $informationContent = '';
48
49
        foreach ($this->getInformationElements() as $informationElement) {
50
            $informationContentPart = $this->getContentElement($informationElement);
51
            $informationContent .= $informationContentPart;
52
        }
53
54
        $paymentPart = str_replace('{{ information-content }}', $informationContent, $paymentPart);
55
56
        return $paymentPart;
57
    }
58
59
    private function addInformationContentReceipt(string $paymentPart): string
60
    {
61
        $informationContent = '';
62
63
        foreach ($this->getInformationElementsOfReceipt() as $informationElement) {
64
            $informationContent .= $this->getContentElement($informationElement);
65
        }
66
67
        $paymentPart = str_replace('{{ information-content-receipt }}', $informationContent, $paymentPart);
68
69
        return $paymentPart;
70
    }
71
72
    private function addCurrencyContent(string $paymentPart): string
73
    {
74
        $currencyContent = '';
75
76
        foreach ($this->getCurrencyElements() as $currencyElement) {
77
            $currencyContent .= $this->getContentElement($currencyElement);
78
        }
79
80
        $paymentPart = str_replace('{{ currency-content }}', $currencyContent, $paymentPart);
81
82
        return $paymentPart;
83
    }
84
85
    private function addAmountContent(string $paymentPart): string
86
    {
87
        $amountContent = '';
88
89
        foreach ($this->getAmountElements() as $amountElement) {
90
            $amountContent .= $this->getContentElement($amountElement);
91
        }
92
93
        $paymentPart = str_replace('{{ amount-content }}', $amountContent, $paymentPart);
94
95
        return $paymentPart;
96
    }
97
98
    private function addAmountContentReceipt(string $paymentPart): string
99
    {
100
        $amountContent = '';
101
102
        foreach ($this->getAmountElementsReceipt() as $amountElement) {
103
            $amountContent .= $this->getContentElement($amountElement);
104
        }
105
106
        $paymentPart = str_replace('{{ amount-content-receipt }}', $amountContent, $paymentPart);
107
108
        return $paymentPart;
109
    }
110
111
    private function addFurtherInformationContent(string $paymentPart): string
112
    {
113
        $furtherInformationContent = '';
114
115
        foreach ($this->getFurtherInformationElements() as $furtherInformationElement) {
116
            $furtherInformationContent .= $this->getContentElement($furtherInformationElement);
117
        }
118
119
        $paymentPart = str_replace('{{ further-information-content }}', $furtherInformationContent, $paymentPart);
120
121
        return $paymentPart;
122
    }
123
124
    private function hideSeparatorContentIfPrintable(string $paymentPart): string
125
    {
126
        $printableStyles = '';
127
        if ($this->isPrintable()) {
128
            $printableStyles = PrintableStylesTemplate::TEMPLATE;
129
        }
130
131
        $paymentPart = str_replace('{{ printable-content }}', $printableStyles, $paymentPart);
132
133
        return $paymentPart;
134
    }
135
136
    private function getContentElement(Title|Text|Placeholder $element): string
137
    {
138
        return match (get_class($element)) {
139
            Title::class => $this->getTitleElement($element),
0 ignored issues
show
Bug introduced by
It seems like $element can also be of type Sprain\SwissQrBill\Payme...put\Element\Placeholder and Sprain\SwissQrBill\PaymentPart\Output\Element\Text; however, parameter $element of Sprain\SwissQrBill\Payme...tput::getTitleElement() does only seem to accept Sprain\SwissQrBill\Payme...rt\Output\Element\Title, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
            Title::class => $this->getTitleElement(/** @scrutinizer ignore-type */ $element),
Loading history...
140
            Text::class => $this->getTextElement($element),
0 ignored issues
show
Bug introduced by
It seems like $element can also be of type Sprain\SwissQrBill\Payme...put\Element\Placeholder and Sprain\SwissQrBill\Payme...rt\Output\Element\Title; however, parameter $element of Sprain\SwissQrBill\Payme...utput::getTextElement() does only seem to accept Sprain\SwissQrBill\PaymentPart\Output\Element\Text, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
            Text::class => $this->getTextElement(/** @scrutinizer ignore-type */ $element),
Loading history...
141
            Placeholder::class => $this->getPlaceholderElement($element),
0 ignored issues
show
Bug introduced by
It seems like $element can also be of type Sprain\SwissQrBill\PaymentPart\Output\Element\Text and Sprain\SwissQrBill\Payme...rt\Output\Element\Title; however, parameter $element of Sprain\SwissQrBill\Payme...getPlaceholderElement() does only seem to accept Sprain\SwissQrBill\Payme...put\Element\Placeholder, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
            Placeholder::class => $this->getPlaceholderElement(/** @scrutinizer ignore-type */ $element),
Loading history...
142
        };
143
    }
144
145
    private function getTitleElement(Title $element): string
146
    {
147
        $elementTemplate = TitleElementTemplate::TEMPLATE;
148
        $elementString = str_replace('{{ title }}', $element->getTitle(), $elementTemplate);
149
150
        return $elementString;
151
    }
152
153
    private function getTextElement(Text $element): string
154
    {
155
        $elementTemplate = TextElementTemplate::TEMPLATE;
156
        $elementString = str_replace('{{ text }}', nl2br($element->getText()), $elementTemplate);
157
158
        return $elementString;
159
    }
160
161
    private function getPlaceholderElement(Placeholder $element): string
162
    {
163
        $elementTemplate = PlaceholderElementTemplate::TEMPLATE;
164
        $elementString = $elementTemplate;
165
166
        $svgDoc = new \DOMDocument();
167
        $svgDoc->loadXML(file_get_contents($element->getFile()));
168
        $svg = $svgDoc->getElementsByTagName('svg');
169
        $dataUri = 'data:image/svg+xml;base64,' . base64_encode($svg->item(0)->C14N());
170
171
        $elementString = str_replace('{{ file }}', $dataUri, $elementString);
172
        $elementString = str_replace('{{ width }}', (string) $element->getWidth(), $elementString);
173
        $elementString = str_replace('{{ height }}', (string) $element->getHeight(), $elementString);
174
        $elementString = str_replace('{{ id }}', $element->getType(), $elementString);
175
176
        return $elementString;
177
    }
178
179
    private function translateContents(string $paymentPart, string $language): string
180
    {
181
        $translations = Translation::getAllByLanguage($language);
182
        foreach ($translations as $key => $text) {
183
            $paymentPart = str_replace('{{ text.' . $key . ' }}', $text, $paymentPart);
184
        }
185
186
        return $paymentPart;
187
    }
188
}
189