Test Failed
Pull Request — master (#59)
by
unknown
03:00
created

FpdfOutput::getReceiptPart()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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