Completed
Push — master ( fd3130...674c45 )
by John
03:39
created

BarcodeGeneratorEan13::convertToFontCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 62
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 1
dl 0
loc 62
rs 9.584
c 0
b 0
f 0
ccs 0
cts 22
cp 0
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Graze\CiffRenderer\BarcodeGenerator;
4
5
use Graze\CiffRenderer\BarcodeGenerator\BarcodeGeneratorInterface;
6
use Graze\CiffRenderer\BarcodeGenerator\BarcodeType;
7
use Graze\CiffRenderer\BarcodeGenerator\BarcodeGeneratorEan13;
8
9
class BarcodeGeneratorEan13 implements BarcodeGeneratorInterface
10
{
11
    /**
12
     * @param string $data
13
     * @return string
14
     */
15
    public function generate($data)
16
    {
17
        // add up each digit in the barcode - odd digits counting
18
        // from the right are tripled before being added
19
        $sum = 0;
20
        $triple = true;
21
        for ($i = strlen($data)-1; $i >= 0; $i--) {
22
            $sum += ($triple ? 3 : 1) * $data[$i];
23
            $triple = !$triple;
0 ignored issues
show
introduced by
The condition $triple is always true.
Loading history...
24
        }
25
26
        //check digit is the inverse remainder after / by 10
27
        //e.g. 4 becomes 6, 8 becomes 2 etc...
28
        $remainder = $sum % 10;
29
30
        if ($remainder > 0) {
31
            $remainder = 10 - $remainder;
32
        }
33
34
        return $this->convertToFontCode($data . $remainder);
35
    }
36
37
    /**
38
    * @param string $barcode
39
    * @return string
40
    */
41
    private function convertToFontCode($barcode)
42
    {
43
        /**
44
        * The barcode is split into the following sections:
45
        *  First digit
46
        *  First group of 6 digits (2nd-7th)
47
        *  Last group of 6 digits (8th-13th)
48
        *      e.g.        1234567890128
49
        *      becomes     1   234567  890128
50
        *
51
        * The font converts the first digit to a char between q-z
52
        *
53
        * The first group of digits are converted to chars starting at either A or Q,
54
        * the first digit affects the format of the first group as follows:
55
        *  1st digit     First group format
56
        *      0           AAAAAA
57
        *      1           AAQAQQ
58
        *      2           AAQQAQ
59
        *      3           AAQQQA
60
        *      4           AQAAQQ
61
        *      5           AQQAAQ
62
        *      6           AQQQAA
63
        *      7           AQAQAQ
64
        *      8           AQAQQA
65
        *      9           AQQAQA
66
        *
67
        * The last group of digits remain unchanged
68
        *
69
        * The actual format used by the font is:
70
        *       firstDigit  {{{{{|  firstGroup  {|{  secondGroup  |
71
        */
72
73
        // convert first digit
74
        $firstDigit = (int)substr($barcode, 0, 1);
75
        $firstDigitChar = chr(113 + $firstDigit);
76
77
        // convert first group
78
        $firstGroup = '';
79
        $firstGroupFormats = [
80
            'AAAAAA',
81
            'AAQAQQ',
82
            'AAQQAQ',
83
            'AAQQQA',
84
            'AQAAQQ',
85
            'AQQAAQ',
86
            'AQQQAA',
87
            'AQAQAQ',
88
            'AQAQQA',
89
            'AQQAQA'
90
        ];
91
        $firstGroupFormat = $firstGroupFormats[$firstDigit];
92
        for ($i = 1; $i <= 6; $i++) {
93
            $digit = (int)substr($barcode, $i, 1);
94
            $startingChar = substr($firstGroupFormat, $i - 1, 1);
95
            $firstGroup .= chr(ord($startingChar) + $digit);
96
        }
97
98
        // extract second group
99
        $secondGroup = substr($barcode, 7);
100
101
        // combine the barcode with the special chars used by the font
102
        return $firstDigitChar.'{{{{{|'.$firstGroup.'{|{'.$secondGroup.'|';
103
    }
104
}
105