PaymentSlipPdf::createPaymentSlip()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.6
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
1
<?php
2
/**
3
 * Swiss Payment Slip PDF
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
6
 * @copyright 2012-2015 Some nice Swiss guys
7
 * @author Marc Würth <[email protected]>
8
 * @author Manuel Reinhard <[email protected]>
9
 * @author Peter Siska <[email protected]>
10
 * @link https://github.com/ravage84/SwissPaymentSlipPdf/
11
 */
12
13
namespace SwissPaymentSlip\SwissPaymentSlipPdf;
14
15
use SwissPaymentSlip\SwissPaymentSlip\PaymentSlip;
16
17
/**
18
 * An abstract base class for concrete implementations for creating Swiss payment slips as PDFs
19
 *
20
 * Responsible for generating standard Swiss payment Slips
21
 * using a PDF engine, e.g. FPDF or TCPDF.
22
 * Layout done by utilizing PaymentSlip and
23
 * data organisation through PaymentSlipData.
24
 *
25
 * @link https://github.com/ravage84/SwissPaymentSlip/ SwissPaymentSlip
26
 * @link https://github.com/ravage84/SwissPaymentSlipTcpdf/ SwissPaymentSlipTcpdf
27
 * @link https://github.com/ravage84/SwissPaymentSlipFpdf/ SwissPaymentSlipFpdf
28
 */
29
abstract class PaymentSlipPdf
30
{
31
    /**
32
     * The PDF engine object to generate the PDF output
33
     *
34
     * @var null|object
35
     */
36
    protected $pdfEngine = null;
37
38
    /**
39
     * The payment slip object, which contains the payment slip data and layout information
40
     *
41
     * @var null|PaymentSlip
42
     */
43
    protected $paymentSlip = null;
44
45
    /**
46
     * Create a new object to create Swiss payment slips as PDFs
47
     *
48
     * @param object $pdfEngine The PDF engine object to generate the PDF output.
49
     * which contains the payment slip data and layout information.
50
     */
51 2
    public function __construct($pdfEngine)
52
    {
53 2
        if (!is_object($pdfEngine)) {
54 1
            throw new \InvalidArgumentException('$pdfEngine is not an object!');
55
        }
56 1
        $this->pdfEngine = $pdfEngine;
57 1
    }
58
59
    /**
60
     * Display a background image
61
     *
62
     * Implement this method by using the parameter(s) with he appropriate method of the PDF engine.
63
     *
64
     * @param string $background The background image path.
65
     * @return $this The current instance for a fluent interface.
66
     */
67
    abstract protected function displayImage($background);
68
69
    /**
70
     * Set the font of an element
71
     *
72
     * Implement this method by using the parameter(s) with he appropriate method of the PDF engine.
73
     *
74
     * @param string $fontFamily The font family
75
     * @param int|float $fontSize The font size.
76
     * @param string $fontColor The font color. Either the name of the color or its RGB hex code.
77
     * @return $this The current instance for a fluent interface.
78
     */
79
    abstract protected function setFont($fontFamily, $fontSize, $fontColor);
80
81
    /**
82
     * Set the background of an element
83
     *
84
     * Implement this method by using the parameter(s) with he appropriate method of the PDF engine.
85
     *
86
     * @param $background
87
     * @return $this The current instance for a fluent interface.
88
     */
89
    abstract protected function setBackground($background);
90
91
    /**
92
     * Set the position of an element
93
     *
94
     * Implement this method by using the parameter(s) with he appropriate method of the PDF engine.
95
     *
96
     * @param int|float $posX The X position.
97
     * @param int|float $posY The Y position.
98
     * @return $this The current instance for a fluent interface.
99
     */
100
    abstract protected function setPosition($posX, $posY);
101
102
    /**
103
     * Create the element cell using the PDF engine
104
     *
105
     * Implement this method by using the parameter(s) with he appropriate method of the PDF engine.
106
     *
107
     * @param int|float $width The width.
108
     * @param int|float $height The height.
109
     * @param string $line The text/content of the line.
110
     * @param string $textAlign The text alignment.
111
     * @param bool $fill Whether to fill the background of the cell.
112
     * @return $this The current instance for a fluent interface.
113
     */
114
    abstract protected function createCell($width, $height, $line, $textAlign, $fill);
115
116
    /**
117
     * Write the lines of an element to the PDf
118
     *
119
     * @param string $elementName The name of the element.
120
     * @param array $element The element.
121
     * @return $this The current instance for a fluent interface.
122
     */
123 7
    protected function writePaymentSlipLines($elementName, array $element)
124
    {
125 7
        if (!is_string($elementName)) {
126 1
            throw new \InvalidArgumentException('$elementName is not a string!');
127
        }
128 6
        if (!isset($element['lines'])) {
129 1
            throw new \InvalidArgumentException('$element contains not "lines" key!');
130
        }
131 5
        if (!isset($element['attributes'])) {
132 1
            throw new \InvalidArgumentException('$element contains not "attributes" key!');
133
        }
134 4
        $lines = $element['lines'];
135 4
        $attributes = $element['attributes'];
136
137 4
        if (!is_array($lines)) {
138 1
            throw new \InvalidArgumentException('$lines is not an array!');
139
        }
140 3
        if (!is_array($attributes)) {
141 1
            throw new \InvalidArgumentException('$attributes is not an array!');
142
        }
143
144 2
        $posX = $attributes['PosX'];
145 2
        $posY = $attributes['PosY'];
146 2
        $height = $attributes['Height'];
147 2
        $width = $attributes['Width'];
148 2
        $fontFamily = $attributes['FontFamily'];
149 2
        $background = $attributes['Background'];
150 2
        $fontSize = $attributes['FontSize'];
151 2
        $fontColor = $attributes['FontColor'];
152 2
        $lineHeight = $attributes['LineHeight'];
153 2
        $textAlign = $attributes['TextAlign'];
154
155 2
        $this->setFont($fontFamily, $fontSize, $fontColor);
156 2
        if ($background != 'transparent') {
157 1
            $this->setBackground($background);
158 1
            $fill = true;
159 1
        } else {
160 2
            $fill = false;
161
        }
162
163 2
        foreach ($lines as $lineNr => $line) {
164 2
            $this->setPosition(
165 2
                $this->paymentSlip->getSlipPosX() + $posX,
166 2
                $this->paymentSlip->getSlipPosY() + $posY + ($lineNr * $lineHeight)
167 2
            );
168 2
            $this->createCell($width, $height, $line, $textAlign, $fill);
169 2
        }
170
171 2
        return $this;
172
    }
173
174
    /**
175
     * Create a payment slip as a PDF using the PDF engine
176
     *
177
     * @param PaymentSlip $paymentSlip The payment slip to create as PDF.
178
     * @return $this The current instance for a fluent interface.
179
     */
180 2
    public function createPaymentSlip(PaymentSlip $paymentSlip)
181
    {
182 2
        $this->paymentSlip = $paymentSlip;
183
184
        // Place background image
185 2
        if ($paymentSlip->getDisplayBackground()) {
186 1
            $this->displayImage($paymentSlip->getSlipBackground());
187 1
        }
188
189 2
        $elements = $paymentSlip->getAllElements();
190
191
        // Go through all elements, write each line
192 2
        foreach ($elements as $elementName => $element) {
193 2
            $this->writePaymentSlipLines($elementName, $element);
194 2
        }
195
196 2
        $this->paymentSlip = null;
197
198 2
        return $this;
199
    }
200
}
201