Completed
Push — master ( f389f4...a832bc )
by Marc
04:46 queued 02:01
created

OrangePaymentSlip   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 434
Duplicated Lines 21.2 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
c 1
b 0
f 0
lcom 1
cbo 2
dl 92
loc 434
ccs 120
cts 123
cp 0.9756
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A setDefaults() 0 12 1
A getPaymentSlipData() 0 4 1
B setReferenceNumberLeftAttr() 28 28 1
B setReferenceNumberRightAttr() 32 32 2
B setCodeLineAttr() 32 32 2
A getReferenceNumberLeftAttr() 0 4 1
A getReferenceNumberRightAttr() 0 4 1
A getCodeLineAttr() 0 4 1
A setDisplayReferenceNr() 0 7 1
A getDisplayReferenceNr() 0 7 2
A setDisplayCodeLine() 0 7 1
A setReferenceNrFormatted() 0 6 1
A getReferenceNrFormatted() 0 4 1
A setReferenceNrFillZeros() 0 6 1
A getReferenceNrFillZeros() 0 4 1
A getDisplayCodeLine() 0 9 3
B getAllElements() 0 41 3
A __construct() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Swiss Payment Slip
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
6
 * @copyright 2012-2016 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/SwissPaymentSlip/
11
 */
12
13
namespace SwissPaymentSlip\SwissPaymentSlip;
14
15
/**
16
 * Orange Swiss Payment Slip
17
 *
18
 * Describes how an orange Swiss payment slip looks like and
19
 * how the various data fields of an orange payment slip are
20
 * placed or displayed respectively.
21
 *
22
 * The data of the fields is organized by its sister class OrangePaymentSlipData.
23
 *
24
 * @link https://www.postfinance.ch/content/dam/pf/de/doc/consult/templ/example/44218_templ_de_fr_it.pdf ESR layouting
25
 * template.
26
 * @link https://www.postfinance.ch/binp/postfinance/public/dam.7nf77hU5mnlo7r15PkmudMtCOvznYc0RFUecjFJued4.spool/content/dam/pf/de/doc/consult/templ/example/44205_templ_de_fr_it.pdf Payment slip ESR bank in CHF
27
 * @uses OrangePaymentSlipData To store the slip data.
28
 */
29
class OrangePaymentSlip extends PaymentSlip
30
{
31
    /**
32
     * The orange payment slip value object, which contains the payment slip data
33
     *
34
     * @var OrangePaymentSlipData
35
     */
36
    protected $paymentSlipData = null;
37
38
    /**
39
     * The height of the slip
40
     *
41
     * @var int|float
42
     */
43
    protected $slipHeight = 106; // default height of an orange slip
44
45
    /**
46
     * The width of the slip
47
     *
48
     * @var int|float
49
     */
50
    protected $slipWidth = 210; // default width of an orange slip
51
52
    /**
53
     * Determines whether the reference number should be displayed
54
     *
55
     * @var bool
56
     */
57
    protected $displayReferenceNr = true;
58
59
    /**
60
     * Determines whether the code line at the bottom should be displayed
61
     *
62
     * @var bool
63
     */
64
    protected $displayCodeLine = true;
65
66
    /**
67
     * Whether to return the reference number formatted in blocks of five (for better readability)
68
     *
69
     * @var bool
70
     */
71
    protected $referenceNrFormatted = true;
72
73
    /**
74
     * Whether to fill up the reference number with leading zeros,
75
     * only applies to the case where no banking customer ID is used.
76
     *
77
     * @var bool
78
     */
79
    protected $referenceNrFillZeros = true;
80
81
    /**
82
     * Attributes of the left reference number element
83
     *
84
     * @var array
85
     */
86
    protected $referenceNumberLeftAttr = [];
87
88
    /**
89
     * Attributes of the right reference number element
90
     *
91
     * @var array
92
     */
93
    protected $referenceNumberRightAttr = [];
94
95
    /**
96
     * Attributes of the code line element
97
     *
98
     * @var array
99
     */
100
    protected $codeLineAttr = [];
101
102
    /**
103
     * Create a new orange payment slip
104
     *
105
     * @param OrangePaymentSlipData $paymentSlipData The orange payment slip data.
106
     * @param float|null $slipPosX The optional X position of the slip.
107
     * @param float|null $slipPosY The optional Y position of the slip.
108
     */
109
    public function __construct(OrangePaymentSlipData $paymentSlipData, $slipPosX = null, $slipPosY = null)
110
    {
111
        parent::__construct($paymentSlipData, $slipPosX, $slipPosY);
112
    }
113
114
    /**
115
     * Sets the default attributes of the elements for an orange slip
116
     *
117
     * @return $this The current instance for a fluent interface.
118
     */
119 4
    protected function setDefaults()
120
    {
121 4
        parent::setDefaults();
122
123 4
        $this->setReferenceNumberLeftAttr(3, 60, 50, 4, null, null, 8);
124 4
        $this->setReferenceNumberRightAttr(125, 33.5, 80, 4);
125 4
        $this->setCodeLineAttr(64, 85, 140, 4, null, 'OCRB10');
126
127 4
        $this->setSlipBackground(__DIR__.'/Resources/img/ezs_orange.gif');
128
129 4
        return $this;
130
    }
131
132
    /**
133
     * Get the slip data object of the slip
134
     *
135
     * @return OrangePaymentSlipData The data object of the slip.
136
     */
137 1
    public function getPaymentSlipData()
138
    {
139 1
        return parent::getPaymentSlipData();
140
    }
141
142
    /**
143
     * Set the left reference number attributes
144
     *
145
     * @param float|null $posX The X position.
146
     * @param float|null $posY The Y Position.
147
     * @param float|null $width The width.
148
     * @param float|null $height The height.
149
     * @param string|null $background The background.
150
     * @param string|null $fontFamily The font family.
151
     * @param float|null $fontSize The font size.
152
     * @param string|null $fontColor The font color.
153
     * @param float|null $lineHeight The line height.
154
     * @param string|null $textAlign The text alignment.
155
     * @return $this The current instance for a fluent interface.
156
     */
157 1 View Code Duplication
    public function setReferenceNumberLeftAttr(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
        $posX = null,
159
        $posY = null,
160
        $width = null,
161
        $height = null,
162
        $background = null,
163
        $fontFamily = null,
164
        $fontSize = null,
165
        $fontColor = null,
166
        $lineHeight = null,
167
        $textAlign = null
168
    ) {
169 1
        $this->setAttributes(
170 1
            $this->referenceNumberLeftAttr,
171 1
            $posX,
172 1
            $posY,
173 1
            $width,
174 1
            $height,
175 1
            $background,
176 1
            $fontFamily,
177 1
            $fontSize,
178 1
            $fontColor,
179 1
            $lineHeight,
180
            $textAlign
181 1
        );
182
183 1
        return $this;
184
    }
185
186
    /**
187
     * Set the right reference number attributes
188
     *
189
     * @param float|null $posX The X position.
190
     * @param float|null $posY The Y Position.
191
     * @param float|null $width The width.
192
     * @param float|null $height The height.
193
     * @param string|null $background The background.
194
     * @param string|null $fontFamily The font family.
195
     * @param float|null $fontSize The font size.
196
     * @param string|null $fontColor The font color.
197
     * @param float|null $lineHeight The line height.
198
     * @param string|null $textAlign The text alignment.
199
     * @return $this The current instance for a fluent interface.
200
     */
201 1 View Code Duplication
    public function setReferenceNumberRightAttr(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
202
        $posX = null,
203
        $posY = null,
204
        $width = null,
205
        $height = null,
206
        $background = null,
207
        $fontFamily = null,
208
        $fontSize = null,
209
        $fontColor = null,
210
        $lineHeight = null,
211
        $textAlign = null
212
    ) {
213 1
        if ($textAlign === null) {
214 1
            $textAlign = 'R';
215 1
        }
216
217 1
        $this->setAttributes(
218 1
            $this->referenceNumberRightAttr,
219 1
            $posX,
220 1
            $posY,
221 1
            $width,
222 1
            $height,
223 1
            $background,
224 1
            $fontFamily,
225 1
            $fontSize,
226 1
            $fontColor,
227 1
            $lineHeight,
228
            $textAlign
229 1
        );
230
231 1
        return $this;
232
    }
233
234
    /**
235
     * Set the code line attributes
236
     *
237
     * @param float|null $posX The X position.
238
     * @param float|null $posY The Y Position.
239
     * @param float|null $width The width.
240
     * @param float|null $height The height.
241
     * @param string|null $background The background.
242
     * @param string|null $fontFamily The font family.
243
     * @param float|null $fontSize The font size.
244
     * @param string|null $fontColor The font color.
245
     * @param float|null $lineHeight The line height.
246
     * @param string|null $textAlign The text alignment.
247
     * @return $this The current instance for a fluent interface.
248
     */
249 1 View Code Duplication
    public function setCodeLineAttr(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
250
        $posX = null,
251
        $posY = null,
252
        $width = null,
253
        $height = null,
254
        $background = null,
255
        $fontFamily = null,
256
        $fontSize = null,
257
        $fontColor = null,
258
        $lineHeight = null,
259
        $textAlign = null
260
    ) {
261 1
        if ($textAlign === null) {
262 1
            $textAlign = 'R';
263 1
        }
264
265 1
        $this->setAttributes(
266 1
            $this->codeLineAttr,
267 1
            $posX,
268 1
            $posY,
269 1
            $width,
270 1
            $height,
271 1
            $background,
272 1
            $fontFamily,
273 1
            $fontSize,
274 1
            $fontColor,
275 1
            $lineHeight,
276
            $textAlign
277 1
        );
278
279 1
        return $this;
280
    }
281
282
    /**
283
     * Get the attributes of the left reference number element
284
     *
285
     * @return array The attributes of the left reference number element.
286
     */
287 1
    public function getReferenceNumberLeftAttr()
288
    {
289 1
        return $this->referenceNumberLeftAttr;
290
    }
291
292
    /**
293
     * Get the attributes of the right reference umber element
294
     *
295
     * @return array The attributes of the right reference umber element.
296
     */
297 1
    public function getReferenceNumberRightAttr()
298
    {
299 1
        return $this->referenceNumberRightAttr;
300
    }
301
302
    /**
303
     * Get the attributes of the code line element
304
     *
305
     * @return array The attributes of the code line element.
306
     */
307 1
    public function getCodeLineAttr()
308
    {
309 1
        return $this->codeLineAttr;
310
    }
311
312
    /**
313
     * Set whether or not to display the reference number
314
     *
315
     * @param bool $displayReferenceNr True if yes, false if no
316
     * @return $this The current instance for a fluent interface.
317
     */
318 2
    public function setDisplayReferenceNr($displayReferenceNr = true)
319
    {
320 2
        $this->isBool($displayReferenceNr, 'displayReferenceNr');
321 1
        $this->displayReferenceNr = $displayReferenceNr;
322
323 1
        return $this;
324
    }
325
326
    /**
327
     * Get whether or not to display the reference number
328
     *
329
     * @return bool True if yes, false if no.
330
     */
331 1
    public function getDisplayReferenceNr()
332
    {
333 1
        if ($this->getPaymentSlipData()->getWithReferenceNumber() !== true) {
334 1
            return false;
335
        }
336 1
        return $this->displayReferenceNr;
337
    }
338
339
    /**
340
     * Set whether or not to display the code line at the bottom
341
     *
342
     * @param bool $displayCodeLine True if yes, false if no
343
     * @return $this The current instance for a fluent interface.
344
     */
345 2
    public function setDisplayCodeLine($displayCodeLine = true)
346
    {
347 2
        $this->isBool($displayCodeLine, 'displayCodeLine');
348 1
        $this->displayCodeLine = $displayCodeLine;
349
350 1
        return $this;
351
    }
352
353
    /**
354
     * Set whether to format the reference number for better readability
355
     *
356
     * @param bool $referenceNrFormatted True if yes, else false.
357
     * @return $this The current instance for a fluent interface.
358
     */
359 2
    public function setReferenceNrFormatted($referenceNrFormatted)
360
    {
361 2
        $this->isBool($referenceNrFormatted, 'referenceNrFormatted');
362 1
        $this->referenceNrFormatted = $referenceNrFormatted;
363 1
        return $this;
364
    }
365
366
    /**
367
     * Get whether to format the reference number for better readability
368
     *
369
     * @return bool True if yes, else false.
370
     */
371 1
    public function getReferenceNrFormatted()
372
    {
373 1
        return $this->referenceNrFormatted;
374
    }
375
376
    /**
377
     * Set whether to fill the reference number with zeros
378
     *
379
     * @param bool $referenceNrFillZeros True if yes, else false.
380
     * @return $this The current instance for a fluent interface.
381
     */
382 2
    public function setReferenceNrFillZeros($referenceNrFillZeros)
383
    {
384 2
        $this->isBool($referenceNrFillZeros, 'referenceNrFillZeros');
385 1
        $this->referenceNrFillZeros = $referenceNrFillZeros;
386 1
        return $this;
387
    }
388
389
    /**
390
     * Get whether to fill the reference number with zeros
391
     *
392
     * @return bool True if yes, else false.
393
     */
394 1
    public function getReferenceNrFillZeros()
395
    {
396 1
        return $this->referenceNrFillZeros;
397
    }
398
399
    /**
400
     * Get whether or not to display the code line at the bottom
401
     *
402
     * Overwrites the parent method as it checks additional settings.
403
     *
404
     * @return bool True if yes, false if no.
405
     */
406 1
    public function getDisplayCodeLine()
407
    {
408 1
        if ($this->getPaymentSlipData()->getWithAccountNumber() !== true ||
409 1
            $this->getPaymentSlipData()->getWithReferenceNumber() !== true
410 1
        ) {
411 1
            return false;
412
        }
413 1
        return $this->displayCodeLine;
414
    }
415
416
    /**
417
     * Get all elements of the slip
418
     *
419
     * @return array All elements with their lines and attributes.
420
     */
421 3
    public function getAllElements()
422
    {
423 3
        $paymentSlipData = $this->paymentSlipData;
424 3
        $formatted = $this->getReferenceNrFormatted();
425 3
        $fillZeros = $this->getReferenceNrFillZeros();
426
427 3
        $elements = parent::getAllElements();
428
429
        // Place left reference number
430 3
        if ($this->getDisplayReferenceNr()) {
431
            $lines = [
432 1
                $paymentSlipData->getCompleteReferenceNumber(
433 1
                    $formatted,
434
                    $fillZeros
435 1
                )
436 1
            ];
437 1
            $elements['referenceNumberLeft'] = [
438 1
                'lines' => $lines,
439 1
            'attributes' => $this->getReferenceNumberLeftAttr()
440 1
            ];
441
442
            // Place right reference number
443
            // Reuse lines from above
444 1
            $elements['referenceNumberRight'] = [
445 1
                'lines' => $lines,
446 1
                'attributes' => $this->getReferenceNumberRightAttr()
447 1
            ];
448 1
        }
449
450
        // Place code line
451 3
        if ($this->getDisplayCodeLine()) {
452
            $lines = [
453 1
                $paymentSlipData->getCodeLine($fillZeros)];
454 1
            $elements['codeLine'] = [
455 1
                'lines' => $lines,
456 1
                'attributes' => $this->getCodeLineAttr()
457 1
            ];
458 1
        }
459
460 3
        return $elements;
461
    }
462
}
463