Completed
Pull Request — master (#51)
by Roberto
11:41
created

Bematech::barcodeQRCode()   C

Complexity

Conditions 8
Paths 30

Size

Total Lines 62
Code Lines 37

Duplication

Lines 16
Ratio 25.81 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 16
loc 62
rs 6.943
cc 8
eloc 37
nc 30
nop 4

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 Posprint\Printers;
4
5
/**
6
 * Bematech class for POS printer
7
 * Model: MP 4200TH
8
 *
9
 * @category   NFePHP
10
 * @package    Posprint
11
 * @copyright  Copyright (c) 2016
12
 * @license    http://www.gnu.org/licenses/lesser.html LGPL v3
13
 * @author     Roberto L. Machado <linux.rlm at gmail dot com>
14
 * @link       http://github.com/nfephp-org/posprint for the canonical source repository
15
 */
16
17
use Posprint\Printers\DefaultPrinter;
18
use Posprint\Printers\PrinterInterface;
19
20
final class Bematech extends DefaultPrinter implements PrinterInterface
21
{
22
    /**
23
     * Select printer mode
24
     *
25
     * @param string $mode
26
     */
27
    public function setPrintMode($mode = 'ESCPOS')
28
    {
29
        //padrão é ESC/POS
30
        $nmode = 0;
31
        if ($mode == 'ESCBEMA') {
32
            $this->printerMode = 'ESCBEMA';
33
            $nmode = 1;
34
        }
35
        $this->buffer->write(self::GS . chr(249) . chr(32) . $nmode);
36
    }
37
38
    /**
39
     * Imprime o QR Code
40
     *
41
     * @param string $data   Dados a serem inseridos no QRCode
42
     * @param string $level  Nivel de correção L,M,Q ou H
43
     * @param int    $modelo modelo de QRCode 
44
     * @param int    $wmod   largura da barra 3 ~ 16
45
     */    
46
    public function barcodeQRCode($data = '', $level = 'M', $modelo = 2, $wmod = 4)
47
    {
48
        //essa matriz especifica o numero máximo de caracteres alfanumericos que o 
49
        //modelo de QRCode suporta dependendo no nivel de correção.
50
        //Cada matriz representa um nivel de correção e cada uma das 40 posições nessas 
51
        //matrizes indicam o numero do modelo do QRCode e o numero máximo de caracteres
52
        //alfamunéricos suportados
53
        //Quanto maior o nivel de correção menor é a quantidade de caracteres suportada
54
        $aModels[0]=[25,47,77,114,154,195,224,279,335,395,468,535,619,667,758,854,938,1046,1153,1249,1352,1460,1588,1704,1853,1990,2132,2223,2369,2520,2677,2840,3009,3183,3351,3537,3729,3927,4087,4296];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$aModels was never initialized. Although not strictly required by PHP, it is generally a good practice to add $aModels = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
55
        $aModels[1]=[20,38,61,90,122,154,178,221,262,311,366,419,483,528,600,656,734,816,909,970,1035,1134,1248,1326,1451,1542,1637,1732,1839,1994,2113,2238,2369,2506,2632,2780,2894,3054,3220,3391];
56
        $aModels[2]=[16,29,47,67,87,108,125,157,189,221,259,296,352,376,426,470,531,574,644,702,742,823,890,963,1041,1094,1172,1263,1322,1429,1499,1618,1700,1787,1867,1966,2071,2181,2298,2420];
57
        $aModels[3]=[10,20,35,50,64,84,93,122,143,174,200,227,259,283,321,365,408,452,493,557,587,640,672,744,779,864,910,958,1016,1080,1150,1226,1307,1394,1431,1530,1591,1658,1774,1852];
58
        //n1 Error correction level (data restoration)
59 View Code Duplication
        switch ($level) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
60
            case 'L':
61
                $n1 = 0;
62
                break;
63
            case "M":
64
                $n1 = 1;
65
                break;
66
            case "Q":
67
                $n1 = 2;
68
                break;
69
            case "H":
70
                $n1 = 3;
71
                break;
72
            default:
73
                $n1 = 0;
74
        }
75
        //n2 Module/cell size in pixels MSB 1 ≤ module size ≤ 127 LSB 0 QR or 1 MicroQR
76
        $n2 = $wmod << 2;
77
        //comprimento da mensagem
78
        $length = strlen($data);
79
        //seleciona matriz de modelos aplicavel pelo nivel de correção
80
        $am = $aModels[$n1];
81
        $i = 0;
82
        $flag = false;
83
        foreach($am as $size) {
84
            //verifica se o tamanho maximo é maior ou igual ao comprimento da mensagem
85
            if ($size >= $length) {
86
                $flag = true;
87
                break;
88
            }
89
            $i++;
90
        }
91
        if (! $flag) {
92
            throw new InvalidArgumentException('O numero de caracteres da mensagem é maior que a capacidade do QRCode');
93
        }
94
        //n3 Version QRCode
95
        //depende do comprimento dos dados e do nivel de correção
96
        $n3 = ($i + 1); 
97
        //n4 Encoding modes
98
        //0 – Numeric only              Max. 7,089 characters
99
        //1 – Alphanumeric              Max. 4,296 characters
100
        //2 – Binary (8 bits)           Max. 2,953 bytes
101
        //3 – Kanji, full-width Kana    Max. 1,817 characters
102
        $n4 = 1;//sempre será 1 apenas caracteres alfanumericos nesse caso
103
        //n5 e n6 Indicate the number of bytes that will be coded, where total = n5 + n6 x 256, and total must be less than 7089.
104
        $n6 = intval($length / 256);
105
        $n5 = ($length % 256);
106
        $this->buffer->write(self::GS."kQ" . chr($n1) . chr($n2) . chr($n3) . chr($n4) . chr($n5) . chr($n6) . $data);
107
    }
108
}
109