Completed
Push — master ( 0c99ca...63ab58 )
by Roberto
05:23 queued 03:13
created

Bematech::barcodeQRCode()   D

Complexity

Conditions 11
Paths 120

Size

Total Lines 85
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 85
ccs 0
cts 0
cp 0
rs 4.9629
cc 11
eloc 55
nc 120
nop 4
crap 132

How to fix   Long Method    Complexity   

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
class Bematech extends DefaultPrinter implements PrinterInterface
21
{
22
    
23
    /**
24
     * List all available code pages.
25
     *
26
     * @var array
27
     */
28
    protected $aCodePage = array(
29
        'CP437' => array('conv' => '437', 'table' => '3', 'desc' => 'PC437: USA, Standard Europe'),
30
        'CP850' => array('conv' => '850', 'table' => '2', 'desc' => 'PC850: Multilingual'),
31
        'CP858' => array('conv' => '858', 'table' => '5', 'desc' => 'PC858: Multilingual'),
32
        'CP860' => array('conv' => '860', 'table' => '4', 'desc' => 'PC860: Portuguese'),
33
        'CP864' => array('conv' => '864', 'table' => '7', 'desc' => 'PC864: Arabic'),
34
        'CP866' => array('conv' => '866', 'table' => '6', 'desc' => 'PC866: Cyrillic'),
35
        'UTF8'  => array('conv' => 'UTF8', 'table' => '8', 'desc' => 'UTF-8: Unicode')
36
    );
37
    /**
38
     * List all available region pages.
39
     *
40
     * @var array
41
     */
42
    protected $aRegion = array(
43
        'LATIN'
44
    );
45
    /**
46
     * Seleted printer mode.
47 1
     *
48
     * @var string
49 1
     */
50
    protected $printerMode = 'ESCBEMA';
51 1
    /**
52
     * List all avaiable fonts
53
     *
54
     * @var array
55
     */
56
    protected $aFont = array(0 => 'C', 1 => 'D');
57
    /**
58
     * Selected internal font.
59
     *
60
     * @var string
61
     */
62
    protected $font = 'C';
63
    /**
64
     * Seleted code page
65
     * Defined in printer class.
66
     *
67
     * @var string
68
     */
69
    protected $codepage = 'CP850';
70
    /**
71
     * Acceptable barcodes list
72
     * @var array
73
     */
74
    protected $barcode1Dlist = [
75
        'UPC_A' => 65,
76
        'UPC_E' => 66,
77
        'EAN13' => 67,
78
        'EAN8' => 68,
79
        'CODE39' => 69,
80
        'I25' => 70,
81
        'CODABAR' => 71,
82
        'CODE93' => 72,
83
        'CODE128' => 73,
84
        'ISBN' => null,
85
        'MSI' => null
86
    ];
87
    /**
88
     * List of supported models
89
     * @var array
90
     */
91
    protected $modelList = [
92
        '4200TH'
93
    ];
94
    /**
95
     * Selected model
96
     * @var string
97
     */
98
    protected $printerModel = '4200TH';
99
100
    //public function __construct(); vide DefaultPrinter
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
    //public function defaultCodePage(); vide DefaultPrinter
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
    //public function defaultRegionPage(); vide DefaultPrinter
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
    //public function defaultFont(); vide DefaultPrinter
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
    //public function defaultModel(); vide DefaultPrinter
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
105
    //public function initialize(); vide DefaultPrinter
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106
    
107
    /**
108
     * Select printer mode
109
     *
110
     * @param string $mode
111
     */
112
    public function setPrintMode($mode = 'ESCBEMA')
113
    {
114
        if ($mode != $this->printerMode) {
115
            switch ($mode) {
116
                case 'ESCBEMA':
117
                    $this->printerMode = 'ESCBEMA';
118
                    break;
119
                default:
120
                    $this->printerMode = 'ESCPOS';
121
            }
122
        }
123
        $nmod = 0;
0 ignored issues
show
Unused Code introduced by
$nmod is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
124
        if ($this->printerMode == 'ESCPOS') {
125
            $nmod = 1;
0 ignored issues
show
Unused Code introduced by
$nmod is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
126
        }
127
        $this->buffer->write(self::GS . chr(249) . chr(53) . $nmode);
0 ignored issues
show
Bug introduced by
The variable $nmode does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
128
    }
129
    
130
    /**
131
     * Set a codepage table in printer.
132
     *
133
     * @param string $codepage
134
     */
135
    public function setCodePage($codepage = null)
136
    {
137
        if ($this->printerMode == 'ESCPOS') {
138
            parent::setCodePage($codepage);
139
            return;
140
        }
141
        $codepage = $this->defaultCodePage($codepage);
0 ignored issues
show
Unused Code introduced by
$codepage is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
142
        $this->buffer->write(self::GS . chr(249) . chr(55) . chr($this->charsetTableNum));
143
    }
144
    
145
    /**
146
     * Set a region page.
147
     * The numeric key of array $this->aRegion is the command parameter.
148
     *
149
     * @param string $region
150
     */
151
    public function setRegionPage($region = null)
152
    {
153
        //not used for this printer
154
    }
155
    
156
    /**
157
     * Set a printer font
158
     * If send a valid font name will set the printer otherelse a default font is selected
159
     *
160
     * @param string $font
161
     */
162
    public function setFont($font = null)
163
    {
164
        if ($this->printerMode == 'ESCPOS') {
165
            parent::setFont($codepage);
0 ignored issues
show
Bug introduced by
The variable $codepage does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
166
        }
167
    }
168
169
    /**
170
     * Set emphasys mode on or off.
171
     */
172
    public function setBold()
173
    {
174
        if ($this->printerMode == 'ESCPOS') {
175
            parent::setBold();
176
            return;
177
        }
178
        $this->boldMode = ! $this->boldMode;
179
        if ($this->boldMode) {
180
            $this->buffer->write(self::ESC . 'E');
181
        } else {
182
            $this->buffer->write(self::ESC . 'F');
183
        }
184
    }
185
186
    /**
187
     * Set Italic mode
188
     */
189
    public function setItalic()
190
    {
191
        $this->italicMode = ! $this->italicMode;
192
        if ($this->printerMode == 'ESCPOS') {
193
            return;
194
        }
195
        if ($this->italicMode) {
196
            $this->buffer->write(self::ESC . '4');
197
        } else {
198
            $this->buffer->write(self::ESC . '5');
199
        }
200
    }
201
202
    //public function setUnderlined(); vide DefaultPrinter
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
203
204
    /**
205
     * Set or unset condensed mode.
206
     */
207
    public function setCondensed()
208
    {
209
        if ($this->printerMode == 'ESCPOS') {
210
            return;
211
        }
212
        $this->condensedMode = ! $this->condensedMode;
213
        if ($this->condensedMode) {
214
            $this->buffer->write(self::SI);
215
        } else {
216
            $this->buffer->write(self::DC2);
217
        }
218
    }
219
    
220
    /**
221
     * Set or unset expanded mode.
222
     *
223
     * @param integer $size not used
224
     */
225
    public function setExpanded($size = null)
226
    {
227
        $this->expandedMode = ! $this->expandedMode;
228
        if ($this->printerMode == 'ESCPOS') {
229
            return;
230
        }
231
        $n = 0;
232
        if ($this->expandedMode) {
233
            $n = 1;
234
        }
235
        $this->buffer->write(self::ESC . 'W' . chr($n));
236
    }
237
    
238
    //public function setAlign(); vide DefaultPrinter
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
239
    
240
    /**
241
     * Turns white/black reverse print On or Off for characters.
242
     */
243
    public function setReverseColors()
244
    {
245
        if ($this->printerMode == 'ESCPOS') {
246
            parent::reverseColors();
0 ignored issues
show
Bug introduced by
The method reverseColors() does not exist on Posprint\Printers\DefaultPrinter. Did you maybe mean setReverseColors()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Comprehensibility Bug introduced by
It seems like you call parent on a different method (reverseColors() instead of setReverseColors()). Are you sure this is correct? If so, you might want to change this to $this->reverseColors().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
247
        }
248
    }
249
    
250
    /**
251
     * Set rotate 90 degrees.
252
     */
253
    public function setRotate90()
254
    {
255
        if ($this->printerMode == 'ESCPOS') {
256
            parent::setRotate90();
257
        }
258
    }
259
    
260
    /**
261
     * Set horizontal and vertical motion units
262
     * $horizontal => character spacing 1/x"
263
     * $vertical => line spacing 1/y".
264
     */
265
    public function setSpacing($horizontal = 30, $vertical = 30)
266
    {
267
        //not used for this printer
268
    }
269
    
270
    /**
271
     * Set right-side character spacing
272
     * 0 ≤ n ≤ 255 => 1/x".
273
     *
274
     * @param int $value
275
     */
276
    public function setCharSpacing($value = 3)
277
    {
278
        //not used for this printer
279
    }
280
    
281
    //public function setParagraph(); vide DefaultPrinter
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
282
    //public function text(); vide default
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
283
284
    /**
285
     * Prints data and feeds paper n lines
286
     * ESC d n Prints data and feeds paper n lines.
287
     *
288
     * @param integer $lines
289
     */
290
    public function lineFeed($lines = 1)
291
    {
292
        if ($this->printerMode == 'ESCPOS') {
293
            parent::lineFeed($lines);
294
            return;
295
        }
296
        $lines = self::validateInteger($lines, 0, 255, 1);
297
        for ($lin = 1; $lin <= $lines; $lin++) {
298
            $this->buffer->write(self::LF);
299
        }
300
    }
301
    
302
    //public function dotFeed(); vide default
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
303
304
    /**
305
     * Put a image
306
     * GS v0 m xL xH yL yH d1 ... dk
307
     *
308
     * @param string $filename
309
     * @param intger $width
310
     * @param integer $height
311
     * @param integer $size resolution relation
312
     * @throws RuntimeException
313
     */
314
    public function putImage($filename = '', $width = null, $height = null, $size = 0)
315
    {
316
        try {
317
            $img = new Graphics($filename, $width, $height);
318
        } catch (RuntimeException $e) {
0 ignored issues
show
Bug introduced by
The class Posprint\Printers\RuntimeException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
319
            throw new RuntimeException($e->getMessage());
320
        } catch (InvalidArgumentException $e) {
0 ignored issues
show
Bug introduced by
The class Posprint\Printers\InvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
321
            throw new RuntimeException($e->getMessage());
322
        }
323
        $size = self::validateInteger($size, 0, 3, 0);
324
        //get xL xH yL yH
325
        $imgHeader = self::dataHeader(array($img->getWidth(), $img->getHeight()), true);
326
        //send graphics command to printer
327
        $this->buffer->write(self::GS.'v0'.chr($size).$imgHeader.$img->getRasterImage());
328
    }
329
330
    /**
331
     * Generate a pulse, for opening a cash drawer if one is connected.
332
     *
333
     *
334
     * @param int $pin    0 or 1, for pin 2 or pin 5 kick-out connector respectively.
335
     * @param int $on_ms  pulse ON time, in milliseconds.
336
     * @param int $off_ms pulse OFF time, in milliseconds.
337
     */
338
    public function pulse($pin = 0, $on_ms = 120, $off_ms = 240)
339
    {
340
        if ($this->printerMode == 'ESCPOS') {
341
            parent::pulse($pin, $on_ms, $off_ms);
342
            return;
343
        }
344
        $on_ms = self::validateInteger($on_ms, 50, 250, 50);
345
        if ($pin == 0 || $pin == 1) {
346
            $this->buffer->write(self::ESC.'v'. chr($on_ms));
347
        } else {
348
            $this->buffer->write(self::ESC. chr(128) . chr($on_ms));
349
        }
350
    }
351
352
    /**
353
     * Cut the paper.
354
     *
355
     * @param int $mode  FULL or PARTIAL. If not specified, FULL will be used.
356
     * @param int $lines Number of lines to feed after cut
357
     */
358
    public function cut($mode = 'PARTIAL', $lines = 3)
359
    {
360
        if ($this->printerMode == 'ESCPOS') {
361
            parent::cut($mode, $lines);
362
        }
363
        $lines = self::validateInteger($lines, 1, 10, 3);
364
        if ($mode == 'FULL') {
365
            $this->buffer->write(self::ESC.'w');
366
        } else {
367
            $this->buffer->write(self::ESC.'m');
368
        }
369
        $this->lineFeed($lines);
370
    }
371
372
    /**
373
     * Implements barcodes 1D
374
     *
375
     * @param int    $type        Default CODE128
376
     * @param int    $height
377
     * @param int    $lineWidth
378
     * @param string $txtPosition
379
     * @param string $txtFont
380
     * @param string $data
381
     */
382
    public function barcode(
383
        $data = '123456',
384
        $type = 'CODE128',
385
        $height = 162,
386
        $lineWidth = 2,
387
        $txtPosition = 'none',
388
        $txtFont = ''
389
    ) {
390
        if ($this->printerMode == 'ESCPOS') {
391
            parent::barcode($data, $type, $height, $lineWidth, $txtPosition, $txtFont);
392
            return;
393
        }
394
        if (! $data = Barcodes\Barcode1DAnalysis::validate($data, $type)) {
395
            throw new \InvalidArgumentException('Data or barcode type is incorrect.');
396
        }
397
        if (! array_key_exists($type, $this->barcode1Dlist)) {
398
            throw new \InvalidArgumentException('This barcode type is not listed.');
399
        }
400
        $n = strlen($data);
401
        $id = $this->barcode1Dlist[$type];
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
402
        $height = self::validateInteger($height, 50, 200, 50);
0 ignored issues
show
Unused Code introduced by
$height is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
403
        $lineWidth = self::validateInteger($lineWidth, 2, 5, 2);
0 ignored issues
show
Unused Code introduced by
$lineWidth is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
404
        $n4 = 0;
0 ignored issues
show
Unused Code introduced by
$n4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
405
        if ($txtPosition != 'none') {
406
            $n4 = 1;
0 ignored issues
show
Unused Code introduced by
$n4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
407
        }
408
        switch ($type) {
409
            case 'UPC_A':
410
                $this->buffer->write(self::GS . 'kA' .self::VT . $data);
411
                break;
412
            case 'UPC_E':
413
                $this->buffer->write(self::GS . 'kB' .self::ACK . $data);
414
                break;
415
            case 'EAN13':
416
                $this->buffer->write(self::GS . 'kC' .self::FF . $data);
417
                break;
418
            case 'EAN8':
419
                $this->buffer->write(self::GS . 'kD' .self::BEL . $data);
420
                break;
421
            case 'CODE39':
422
                $this->buffer->write(self::GS . 'kE' . char($n) . $data);
423
                break;
424
            case 'I25':
425
                $this->buffer->write(self::GS . 'kF' . char($n) . $data);
426
                break;
427
            case 'CODABAR':
428
                $this->buffer->write(self::GS . 'kG' . char($n) . $data);
429
                break;
430
            case 'CODE93':
431
                $this->buffer->write(self::GS . 'kH' . char($n) . $data);
432
                break;
433
            case 'CODE128':
434
                $this->buffer->write(self::GS . 'kI' . char($n) . $data);
435
                break;
436
            case 'ISBN':
437
                $this->buffer->write(self::GS . 'k' . self::NAK . $data . self::NUL);
438
                break;
439
            case 'MSI':
440
                $this->buffer->write(self::GS . 'k' . self::SYN . $data . self::NUL);
441
                break;
442
        }
443
    }
444
    
445
    /**
446
     * Print PDF 417 2D barcode
447
     * @param string $data
448
     * @param integer $ecc
449
     * @param integer $pheight
450
     * @param integer $pwidth
451
     * @param integer $colunms
452
     * @return boolean
453
     */
454
    public function barcodePDF417($data, $ecc = 5, $pheight = 2, $pwidth = 2, $colunms = 3)
455
    {
456
        if ($this->printerMode == 'ESCPOS') {
457
            parent::barcodePDF417($data, $ecc, $pheight, $pwidth, $colunms);
458
        }
459
        $ecc = self::validateInteger($ecc, 0, 8, 5);
460
        $pheight = self::validateInteger($pheight, 1, 8, 2);
461
        $pwidth = self::validateInteger($pwidth, 1, 4, 2);
462
        $length = strlen($data);
463
        $n6 = intval($length / 256);
464
        $n5 = ($length % 256);
465
        $this->buffer->write(
466
            self::GS
467
            . 'k'
468
            . chr(128)
469
            . chr($ecc)
470
            . chr($pheight)
471
            . chr($pwidth)
472
            . chr(0)
473
            . chr($n5)
474
            . chr($n6)
475
            . $data
476
        );
477
    }
478
479
    
480
    /**
481
     * Imprime o QR Code
482
     *
483
     * @param string $data   Dados a serem inseridos no QRCode
484
     * @param string $level  Nivel de correção L,M,Q ou H
485
     * @param int    $modelo modelo de QRCode 0 QRCode ou 1 microQR
486
     * @param int    $wmod   largura da barra 3 ~ 16
487
     */
488
    public function barcodeQRCode($data = '', $level = 'M', $modelo = 0, $wmod = 4)
489
    {
490
        $aModels = array();
491
        //essa matriz especifica o numero máximo de caracteres alfanumericos que o
492
        //modelo de QRCode suporta dependendo no nivel de correção.
493
        //Cada matriz representa um nivel de correção e cada uma das 40 posições nessas
494
        //matrizes indicam o numero do modelo do QRCode e o numero máximo de caracteres
495
        //alfamunéricos suportados
496
        //Quanto maior o nivel de correção menor é a quantidade de caracteres suportada
497
        $aModels[0]=[25,47,77,114,154,195,224,279,335,395,468,535,619,667,
498
            758,854,938,1046,1153,1249,1352,1460,1588,1704,1853,1990,2132,
499
            2223,2369,2520,2677,2840,3009,3183,3351,3537,3729,3927,4087,4296];
500
        $aModels[1]=[20,38,61,90,122,154,178,221,262,311,366,419,483,528,600,
501
            656,734,816,909,970,1035,1134,1248,1326,1451,1542,1637,1732,1839,
502
            1994,2113,2238,2369,2506,2632,2780,2894,3054,3220,3391];
503
        $aModels[2]=[16,29,47,67,87,108,125,157,189,221,259,296,352,376,426,
504
            470,531,574,644,702,742,823,890,963,1041,1094,1172,1263,1322,1429,
505
            1499,1618,1700,1787,1867,1966,2071,2181,2298,2420];
506
        $aModels[3]=[10,20,35,50,64,84,93,122,143,174,200,227,259,283,321,365,
507
            408,452,493,557,587,640,672,744,779,864,910,958,1016,1080,1150,1226,
508
            1307,1394,1431,1530,1591,1658,1774,1852];
509
        //n1 Error correction level (data restoration)
510
        switch ($level) {
511
            case 'L':
512
                $n1 = 0;
513
                break;
514
            case "M":
515
                $n1 = 1;
516
                break;
517
            case "Q":
518
                $n1 = 2;
519
                break;
520
            case "H":
521
                $n1 = 3;
522
                break;
523
            default:
524
                $n1 = 0;
525
        }
526
        if ($modelo != 0 && $modelo != 1) {
527
            $modelo = 0;
528
        }
529
        //se for mucroQR sua capacidade é bem reduzida
530
        if ($modelo == 1) {
531
            $aModels[0] = [6,14,21];
532
            $aModels[1] = [5,11,18];
533
            $aModels[2] = [0,0,13];
534
            $aModels[3] = [0,0,0];
535
        }
536
        //n2 Module/cell size in pixels MSB 1 ≤ module size ≤ 127 LSB 0 QR or 1 MicroQR
537
        $n2 = $wmod << 1;//shift 1 é o mesmo que multiplicar por 2
538
        $n2 += $modelo;//seleciona QRCode ou microQR
539
        //comprimento da mensagem
540
        $length = strlen($data);
541
        //seleciona matriz de modelos aplicavel pelo nivel de correção
542
        $am = $aModels[$n1];
543
        $i = 0;
544
        $flag = false;
545
        foreach ($am as $size) {
546
            //verifica se o tamanho maximo é maior ou igual ao comprimento da mensagem
547
            if ($size >= $length) {
548
                $flag = true;
549
                break;
550
            }
551
            $i++;
552
        }
553
        if (! $flag) {
554
            throw new InvalidArgumentException(
555
                'O numero de caracteres da mensagem é maior que a capacidade do QRCode'
556
            );
557
        }
558
        //n3 Version QRCode
559
        //depende do comprimento dos dados e do nivel de correção
560
        $n3 = ($i + 1);
561
        //n4 Encoding modes
562
        //0 – Numeric only              Max. 7,089 characters
563
        //1 – Alphanumeric              Max. 4,296 characters
564
        //2 – Binary (8 bits)           Max. 2,953 bytes
565
        //3 – Kanji, full-width Kana    Max. 1,817 characters
566
        $n4 = 1;//sempre será 1 apenas caracteres alfanumericos nesse caso
567
        //n5 e n6 Indicate the number of bytes that will be coded, where total = n5 + n6 x 256,
568
        //and total must be less than 7089.
569
        $n6 = intval($length / 256);
570
        $n5 = ($length % 256);
571
        $this->buffer->write(self::GS."kQ" . chr($n1) . chr($n2) . chr($n3) . chr($n4) . chr($n5) . chr($n6) . $data);
572
    }
573
}
574