Passed
Pull Request — master (#59)
by Manuel
10:00
created

FpdfOutput::getPaymentPart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
4
namespace Sprain\SwissQrBill\PaymentPart\Output\FpdfOutput;
5
6
use Sprain\SwissQrBill\PaymentPart\Output\AbstractOutput;
7
use Sprain\SwissQrBill\PaymentPart\Output\Element\OutputElementInterface;
8
use Sprain\SwissQrBill\PaymentPart\Output\Element\Placeholder;
9
use Sprain\SwissQrBill\PaymentPart\Output\Element\Text;
10
use Sprain\SwissQrBill\PaymentPart\Output\Element\Title;
11
use Sprain\SwissQrBill\PaymentPart\Output\FpdfOutput\Template\QrBillFooter;
12
use Sprain\SwissQrBill\PaymentPart\Output\OutputInterface;
13
use Sprain\SwissQrBill\PaymentPart\Translation\Translation;
14
use Sprain\SwissQrBill\QrBill;
15
use Sprain\SwissQrBill\QrCode\QrCode;
16
17
final class FpdfOutput extends AbstractOutput implements OutputInterface
18
{
19
    /** @var QrBillFooter */
20
    private $fpdf;
21
22
    /** @var float */
23
    private $amountLS = 0;
24
25
    public function __construct(
26
        QrBill $qrBill,
27
        string $language,
28
        QrBillFooter $fpdf
29
    ) {
30
        parent::__construct($qrBill, $language);
31
        $this->fpdf = $fpdf;
32
        $this->setQrCodeImageFormat(QrCode::FILE_FORMAT_PNG);
33
    }
34
35
    public function getPaymentPart()
36
    {
37
        $this->fpdf->SetAutoPageBreak(false);
38
        $this->addSeparatorContentIfNotPrintable();
39
        $this->addReceiptPart();
40
        $this->addPaymentPart();
41
    }
42
43
    private function addSeparatorContentIfNotPrintable()
44
    {
45
        if (!$this->isPrintable()) {
46
            $this->fpdf->SetLineWidth(0.1);
47
            $this->fpdf->SetDash(1.25, 1.25);
48
            $this->fpdf->Line(2, 193, 208, 193);
49
            $this->fpdf->Line(62, 193, 62, 296);
50
            $this->fpdf->SetFont('helvetica', '', 7);
51
            $this->fpdf->SetY(189.6);
52
            $this->fpdf->MultiCell(0, 0, utf8_decode(Translation::get('separate', $this->language)), '', 'C');
53
        }
54
    }
55
56
    private function addReceiptPart()
57
    {
58
        // Title
59
        $this->fpdf->SetFont('helvetica', 'B', 11);
60
        $this->fpdf->SetXY(4, 195.2);
61
        $this->fpdf->MultiCell(0, 7, utf8_decode(Translation::get('receipt', $this->language)));
62
63
        // Elements
64
        $this->fpdf->SetY(204);
65
        foreach ($this->getInformationElementsOfReceipt() as $receiptInformationElement) {
66
            $this->fpdf->SetX(4);
67
            $this->setContentElement($receiptInformationElement, true);
68
        }
69
70
        // Amount
71
        $this->fpdf->SetY(259.3);
72
        foreach ($this->getCurrencyElements() as $receiptCurrencyElement) {
73
            $this->amountLS = 0.6;
74
            $this->fpdf->SetX(4);
75
            $this->setContentElement($receiptCurrencyElement, true);
76
            $this->amountLS = 0;
77
        }
78
        $this->fpdf->SetY(259.3);
79
        foreach ($this->getAmountElementsReceipt() as $receiptAmountElement) {
80
            $this->amountLS = 0.6;
81
            $this->fpdf->SetX(16);
82
            $this->setContentElement($receiptAmountElement, true);
83
            $this->amountLS = 0;
84
        }
85
86
        // Acceptance section
87
        $this->fpdf->SetFont('helvetica', 'B', 6);
88
        $this->fpdf->SetXY(4, 274.3);
89
        $this->fpdf->Cell(54, 0, utf8_decode(Translation::get('acceptancePoint', $this->language)), '', '', 'R');
90
    }
91
92
    private function addPaymentPart()
93
    {
94
        // Title
95
        $this->fpdf->SetFont('helvetica', 'B', 11);
96
        $this->fpdf->SetXY(66, 195.2);
97
        $this->fpdf->MultiCell(48, 7, utf8_decode(Translation::get('paymentPart', $this->language)));
98
99
        // QRCode
100
        $image = $this->getPngImage();
101
        $this->fpdf->Image($image[0], 67, 209.5, 46, 46, $image[1]);
102
103
        // Information Section
104
        $this->fpdf->SetY(197.3);
105
        foreach ($this->getInformationElements() as $informationElement) {
106
            $this->fpdf->SetX(117);
107
            $this->setContentElement($informationElement, false);
108
        }
109
110
        // Amount section
111
        $this->fpdf->SetY(260);
112
        foreach ($this->getCurrencyElements() as $currencyElement) {
113
            $this->amountLS = 1.2;
114
            $this->fpdf->SetX(66);
115
            $this->setContentElement($currencyElement, false);
116
            $this->amountLS = 0;
117
        }
118
        $this->fpdf->SetY(260);
119
        foreach ($this->getAmountElements() as $amountElement) {
120
            $this->amountLS = 1.2;
121
            $this->fpdf->SetX(80);
122
            $this->setContentElement($amountElement, false);
123
            $this->amountLS = 0;
124
        }
125
126
        // Further Information Section
127
        $this->fpdf->SetY(286);
128
        $this->fpdf->SetFont('helvetica', '', 7);
129
        foreach ($this->getFurtherInformationElements() as $furtherInformationElement) {
130
            $this->fpdf->SetX(66);
131
            $this->setContentElement($furtherInformationElement, true);
132
        }
133
    }
134
135
    private function setContentElement(OutputElementInterface $element, bool $isReceiptPart): void
136
    {
137
        if ($element instanceof Title) {
138
            $this->setTitleElement($element, $isReceiptPart);
139
        }
140
141
        if ($element instanceof Text) {
142
            $this->setTextElement($element, $isReceiptPart);
143
        }
144
145
        if ($element instanceof Placeholder) {
146
            $this->setPlaceholderElement($element);
147
        }
148
    }
149
150
    private function setTitleElement(Title $element, bool $isReceiptPart): void
151
    {
152
        $this->fpdf->SetFont('helvetica', 'B', $isReceiptPart ? 6 : 8);
153
        $this->fpdf->MultiCell(0, 2.8, utf8_decode(
154
            Translation::get(str_replace("text.", "", $element->getTitle()), $this->language)
155
        ));
156
        $this->fpdf->Ln($this->amountLS);
157
    }
158
159
    private function setTextElement(Text $element, bool $isReceiptPart): void
160
    {
161
        $this->fpdf->SetFont('helvetica', '', $isReceiptPart ? 8 : 10);
162
        $this->fpdf->MultiCell(
163
            $isReceiptPart ? 54 : 0,
164
            $isReceiptPart ? 3.3 : 4,
165
            str_replace("text.", "", utf8_decode($element->getText())),
166
            '',
167
            'L'
168
        );
169
        $this->fpdf->Ln($isReceiptPart ? 3.4 : 4.8);
170
    }
171
172
    private function setPlaceholderElement(Placeholder $element): void
173
    {
174
        $type = $element->getType();
175
176
        switch ($type) {
177
            case Placeholder::PLACEHOLDER_TYPE_AMOUNT['type']:
178
                $y = $this->fpdf->GetY() + 1;
179
                $x = $this->fpdf->GetX() - 2;
180
                break;
181
            case Placeholder::PLACEHOLDER_TYPE_AMOUNT_RECEIPT['type']:
182
                $y = $this->fpdf->GetY() - 2;
183
                $x = $this->fpdf->GetX() + 11;
184
                break;
185
            case Placeholder::PLACEHOLDER_TYPE_PAYABLE_BY['type']:
186
            case Placeholder::PLACEHOLDER_TYPE_PAYABLE_BY_RECEIPT['type']:
187
            default:
188
                $y = $this->fpdf->GetY() + 1;
189
                $x = $this->fpdf->GetX() + 1;
190
        }
191
192
        $this->fpdf->Image(
193
            $element->getFile('png'),
194
            $x,
195
            $y,
196
            $element->getWidth(),
197
            $element->getHeight()
198
        );
199
    }
200
201
    private function getPngImage()
202
    {
203
        $qrCode = $this->getQrCode();
204
        $format = QrCode::FILE_FORMAT_PNG;
205
        $qrCode->setWriterByExtension($format);
206
        $image64 = explode(',', $qrCode->writeDataUri(), 2);
207
        $image = 'data://text/plain;base64,' . $image64[1];
208
        $type = 'png';
209
210
        return [$image, $type];
211
    }
212
}
213