Passed
Pull Request — master (#22)
by Manuel
02:21 queued 46s
created

HtmlOutput::getPaymentPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 13
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\HtmlOutput\Template\ContentElementTemplate;
7
use Sprain\SwissQrBill\PaymentPart\Output\HtmlOutput\Template\PaymentPartTemplate;
8
use Sprain\SwissQrBill\PaymentPart\Output\OutputInterface;
9
use Sprain\SwissQrBill\PaymentPart\Translation\Translation;
10
11
final class HtmlOutput extends AbstractOutput implements OutputInterface
12
{
13
    public function getPaymentPart() : string
14
    {
15
        $paymentPart = PaymentPartTemplate::TEMPLATE;
16
17
        $paymentPart = $this->addSwissQrCodeImage($paymentPart);
18
        $paymentPart = $this->addInformationContent($paymentPart);
19
        $paymentPart = $this->addInformationContentReceipt($paymentPart);
20
        $paymentPart = $this->addCurrencyContent($paymentPart);
21
        $paymentPart = $this->addAmountContent($paymentPart);
22
23
        $paymentPart = $this->translateContents($paymentPart, $this->getLanguage());
24
25
        return $paymentPart;
26
    }
27
28
    private function addSwissQrCodeImage(string $paymentPart) : string
29
    {
30
        $paymentPart = str_replace('{{ swiss-qr-image }}', $this->qrBill->getQrCode()->writeDataUri(), $paymentPart);
31
32
        return $paymentPart;
33
    }
34
35
    private function addInformationContent(string $paymentPart) : string
36
    {
37
        $informationContent = '';
38
39
        foreach($this->getInformationElements() as $key => $content) {
40
            $informationContentPart = $this->getContentElement('{{ '.$key.' }}', $content);
41
            $informationContent .= $informationContentPart;
42
        }
43
44
        $paymentPart = str_replace('{{ information-content }}', $informationContent, $paymentPart);
45
46
        return $paymentPart;
47
    }
48
49
    private function addInformationContentReceipt(string $paymentPart) : string
50
    {
51
        $informationContent = '';
52
53
        foreach($this->getInformationElementsOfReceipt() as $key => $content) {
54
            $informationContentPart = $this->getContentElement('{{ '.$key.' }}', $content);
55
            $informationContent .= $informationContentPart;
56
        }
57
58
        $paymentPart = str_replace('{{ information-content-receipt }}', $informationContent, $paymentPart);
59
60
        return $paymentPart;
61
    }
62
63
    private function addCurrencyContent(string $paymentPart) : string
64
    {
65
        $currencyContent = $this->getContentElement('{{ text.currency }}', $this->qrBill->getPaymentAmountInformation()->getCurrency());
66
        $paymentPart = str_replace('{{ currency-content }}', $currencyContent, $paymentPart);
67
68
        return $paymentPart;
69
    }
70
71
    private function addAmountContent(string $paymentPart) : string
72
    {
73
        $amountContent = $this->getContentElement('{{ text.amount }}', $this->qrBill->getPaymentAmountInformation()->getFormattedAmount());
74
        $paymentPart = str_replace('{{ amount-content }}', $amountContent, $paymentPart);
75
76
        return $paymentPart;
77
    }
78
79
    private function getContentElement(string $title, string $content) : string
80
    {
81
        $contentElementTemplate = ContentElementTemplate::TEMPLATE;
82
        $contentElement = $contentElementTemplate;
83
84
        $contentElement = str_replace('{{ title }}', $title, $contentElement);
85
        $contentElement = str_replace('{{ content }}', nl2br($content), $contentElement);
86
87
        return $contentElement;
88
    }
89
90
    private function translateContents($paymentPart, $language)
91
    {
92
        $translations = Translation::getAllByLanguage($language);
93
        foreach($translations as $key => $text) {
94
            $paymentPart = str_replace('{{ text.' . $key . ' }}', $text, $paymentPart);
95
        }
96
97
        return $paymentPart;
98
    }
99
}