Completed
Push — master ( a19e9a...5cb337 )
by Roberto
07:17 queued 04:48
created

Bematech   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 13
c 9
b 0
f 0
lcom 1
cbo 1
dl 0
loc 114
ccs 0
cts 67
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setPrintMode() 0 13 2
D barcodeQRCode() 0 84 11
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
        //default ESC/POS
30
        $nmode = 0;
31
        if ($mode == 'ESCBEMA') {
32
            $this->printerMode = 'ESCBEMA';
33
            $nmode = 1;
34
        }
35
        //select mode ESCPOS or ESCBEMA
36
        $this->buffer->write(self::GS . chr(249) . chr(32) . $nmode);
37
        //clear Emphasized, Double height, Double width
38
        $this->buffer->write(self::ESC . '!' . chr(0));
39
    }
40
41
    /**
42
     * Imprime o QR Code
43
     *
44
     * @param string $data   Dados a serem inseridos no QRCode
45
     * @param string $level  Nivel de correção L,M,Q ou H
46
     * @param int    $modelo modelo de QRCode 0 QRCode ou 1 microQR
47
     * @param int    $wmod   largura da barra 3 ~ 16
48
     */
49
    public function barcodeQRCode($data = '', $level = 'M', $modelo = 0, $wmod = 4)
50
    {
51
        //essa matriz especifica o numero máximo de caracteres alfanumericos que o
52
        //modelo de QRCode suporta dependendo no nivel de correção.
53
        //Cada matriz representa um nivel de correção e cada uma das 40 posições nessas
54
        //matrizes indicam o numero do modelo do QRCode e o numero máximo de caracteres
55
        //alfamunéricos suportados
56
        //Quanto maior o nivel de correção menor é a quantidade de caracteres suportada
57
        $aModels[0]=[25,47,77,114,154,195,224,279,335,395,468,535,619,667,
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...
58
            758,854,938,1046,1153,1249,1352,1460,1588,1704,1853,1990,2132,
59
            2223,2369,2520,2677,2840,3009,3183,3351,3537,3729,3927,4087,4296];
60
        $aModels[1]=[20,38,61,90,122,154,178,221,262,311,366,419,483,528,600,
61
            656,734,816,909,970,1035,1134,1248,1326,1451,1542,1637,1732,1839,
62
            1994,2113,2238,2369,2506,2632,2780,2894,3054,3220,3391];
63
        $aModels[2]=[16,29,47,67,87,108,125,157,189,221,259,296,352,376,426,
64
            470,531,574,644,702,742,823,890,963,1041,1094,1172,1263,1322,1429,
65
            1499,1618,1700,1787,1867,1966,2071,2181,2298,2420];
66
        $aModels[3]=[10,20,35,50,64,84,93,122,143,174,200,227,259,283,321,365,
67
            408,452,493,557,587,640,672,744,779,864,910,958,1016,1080,1150,1226,
68
            1307,1394,1431,1530,1591,1658,1774,1852];
69
        //n1 Error correction level (data restoration)
70
        switch ($level) {
71
            case 'L':
72
                $n1 = 0;
73
                break;
74
            case "M":
75
                $n1 = 1;
76
                break;
77
            case "Q":
78
                $n1 = 2;
79
                break;
80
            case "H":
81
                $n1 = 3;
82
                break;
83
            default:
84
                $n1 = 0;
85
        }
86
        if ($modelo != 0 && $modelo != 1) {
87
            $modelo = 0;
88
        }
89
        //se for mucroQR sua capacidade é bem reduzida
90
        if ($modelo == 1) {
91
            $aModels[0] = [6,14,21];
92
            $aModels[1] = [5,11,18];
93
            $aModels[2] = [0,0,13];
94
            $aModels[3] = [0,0,0];
95
        }
96
        //n2 Module/cell size in pixels MSB 1 ≤ module size ≤ 127 LSB 0 QR or 1 MicroQR
97
        $n2 = $wmod << 1;//shift 1 é o mesmo que multiplicar por 2
98
        $n2 += $modelo;//seleciona QRCode ou microQR
99
        //comprimento da mensagem
100
        $length = strlen($data);
101
        //seleciona matriz de modelos aplicavel pelo nivel de correção
102
        $am = $aModels[$n1];
103
        $i = 0;
104
        $flag = false;
105
        foreach ($am as $size) {
106
            //verifica se o tamanho maximo é maior ou igual ao comprimento da mensagem
107
            if ($size >= $length) {
108
                $flag = true;
109
                break;
110
            }
111
            $i++;
112
        }
113
        if (! $flag) {
114
            throw new InvalidArgumentException(
115
                'O numero de caracteres da mensagem é maior que a capacidade do QRCode'
116
            );
117
        }
118
        //n3 Version QRCode
119
        //depende do comprimento dos dados e do nivel de correção
120
        $n3 = ($i + 1);
121
        //n4 Encoding modes
122
        //0 – Numeric only              Max. 7,089 characters
123
        //1 – Alphanumeric              Max. 4,296 characters
124
        //2 – Binary (8 bits)           Max. 2,953 bytes
125
        //3 – Kanji, full-width Kana    Max. 1,817 characters
126
        $n4 = 1;//sempre será 1 apenas caracteres alfanumericos nesse caso
127
        //n5 e n6 Indicate the number of bytes that will be coded, where total = n5 + n6 x 256,
128
        //and total must be less than 7089.
129
        $n6 = intval($length / 256);
130
        $n5 = ($length % 256);
131
        $this->buffer->write(self::GS."kQ" . chr($n1) . chr($n2) . chr($n3) . chr($n4) . chr($n5) . chr($n6) . $data);
132
    }
133
}
134