BarcodeGeneratorEan13::convertToFontCode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 62
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

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

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 3
    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 3
        $sum = 0;
20 3
        $triple = true;
21 3
        for ($i = strlen($data)-1; $i >= 0; $i--) {
22 3
            $sum += ($triple ? 3 : 1) * $data[$i];
23 3
            $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 3
        $remainder = $sum % 10;
29
30 3
        if ($remainder > 0) {
31 2
            $remainder = 10 - $remainder;
32
        }
33
34 3
        return $this->convertToFontCode($data . $remainder);
35
    }
36
37
    /**
38
    * @param string $barcode
39
    * @return string
40
    */
41 3
    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 3
        $firstDigit = (int)substr($barcode, 0, 1);
75 3
        $firstDigitChar = chr(113 + $firstDigit);
76
77
        // convert first group
78 3
        $firstGroup = '';
79
        $firstGroupFormats = [
80 3
            'AAAAAA',
81
            'AAQAQQ',
82
            'AAQQAQ',
83
            'AAQQQA',
84
            'AQAAQQ',
85
            'AQQAAQ',
86
            'AQQQAA',
87
            'AQAQAQ',
88
            'AQAQQA',
89
            'AQQAQA'
90
        ];
91 3
        $firstGroupFormat = $firstGroupFormats[$firstDigit];
92 3
        for ($i = 1; $i <= 6; $i++) {
93 3
            $digit = (int)substr($barcode, $i, 1);
94 3
            $startingChar = substr($firstGroupFormat, $i - 1, 1);
95 3
            $firstGroup .= chr(ord($startingChar) + $digit);
96
        }
97
98
        // extract second group
99 3
        $secondGroup = substr($barcode, 7);
100
101
        // combine the barcode with the special chars used by the font
102 3
        return $firstDigitChar.'{{{{{|'.$firstGroup.'{|{'.$secondGroup.'|';
103
    }
104
}
105