Test Setup Failed
Push — master ( b9c0be...6a3b1f )
by Francimar
06:58
created

Epson::fontChanged()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
namespace Thermal\Profile;
4
5
use Thermal\Printer;
6
7
class Epson extends Profile
8
{
9 4
    public function setAlignment($align)
10
    {
11
        $cmd = [
12 4
            Printer::ALIGN_LEFT => "\ea0",
13 4
            Printer::ALIGN_CENTER => "\ea1",
14 4
            Printer::ALIGN_RIGHT => "\ea2"
15
        ];
16 4
        $this->getConnection()->write($cmd[$align]);
17 4
    }
18
19 7
    protected function setStyle($style, $enable)
20
    {
21 7
        if ($enable) {
22
            // enable styles
23 7
            if (Printer::STYLE_CONDENSED == $style) {
24 1
                $this->getConnection()->write("\e\x0f");
25 7
            } elseif (Printer::STYLE_BOLD == $style) {
26 1
                $this->getConnection()->write("\eE1");
27 7
            } elseif (Printer::STYLE_ITALIC == $style) {
28 3
                $this->getConnection()->write("\e4");
29 7
            } elseif (Printer::STYLE_UNDERLINE == $style) {
30 7
                $this->getConnection()->write("\e-1");
31
            }
32
        } else {
33
            // disable styles
34 7
            if (Printer::STYLE_UNDERLINE == $style) {
35 1
                $this->getConnection()->write("\e-0");
36 7
            } elseif (Printer::STYLE_ITALIC == $style) {
37 3
                $this->getConnection()->write("\e5");
38 7
            } elseif (Printer::STYLE_BOLD == $style) {
39 1
                $this->getConnection()->write("\eE0");
40 7
            } elseif (Printer::STYLE_CONDENSED == $style) {
41 1
                $this->getConnection()->write("\x12");
42
            }
43
        }
44 7
    }
45
46 3
    protected function setMode($mode, $enable)
47
    {
48 3
        $byte = 0b00000000; // keep Font A selected
49 3
        if ($this->getFont()['name'] == 'Font B') {
50 1
            $byte |= 0b00000001; // keep Font B selected
51
        }
52 3
        $before = $byte;
53 3
        if (Printer::STYLE_DOUBLE_HEIGHT & $mode) {
54 2
            $byte |= 0b00010000;
55
        }
56 3
        if (Printer::STYLE_DOUBLE_WIDTH & $mode) {
57 2
            $byte |= 0b00100000;
58
        }
59 3
        if ($enable) {
60 3
            $mask = 0b00110001;
61
        } else {
62 3
            $mask = 0b00000001;
63
        }
64 3
        if ($before != $byte) {
65 2
            $this->getConnection()->write("\e!" . chr($byte & $mask));
66
        }
67 3
    }
68
69 4
    public function feed($lines)
70
    {
71 4
        if ($lines > 1) {
72 1
            $count = (int)($lines / 255);
73 1
            $cmd = \str_repeat("\ed" . chr(min($lines, 255)), $count);
74 1
            $remaining = $lines - $count * 255;
75 1
            if ($remaining > 0) {
76 1
                $cmd .= "\ed" . chr($remaining);
77
            }
78 1
            $this->getConnection()->write($cmd);
79
        } else {
80 3
            $this->getConnection()->write("\r\n");
81
        }
82 4
        return $this;
83
    }
84
85 4
    public function buzzer()
86
    {
87 4
        $this->getConnection()->write("\x07");
88 2
        return $this;
89
    }
90
91 2
    public function cutter($mode)
92
    {
93
        // only partial cut
94 2
        $this->getConnection()->write("\em");
95 2
        return $this;
96
    }
97
98
    /**
99
     * @param int $number drawer id
100
     * @param int $on_time time in milliseconds that activate the drawer
101
     * @param int $off_time time in milliseconds that deactivate the drawer
102
     */
103 1
    public function drawer($number, $on_time, $off_time)
104
    {
105
        $index = [
106 1
            Printer::DRAWER_1 => 0,
107 1
            Printer::DRAWER_2 => 1
108
        ];
109 1
        if (!isset($index[$number])) {
110 1
            throw new \Exception(
111 1
                sprintf('Drawer %d not available for printer "%s"', $this->getName(), intval($number)),
112 1
                404
113
            );
114
        }
115 1
        $on_time = min((int)($on_time / 2), 255);
116 1
        $off_time = min((int)($off_time / 2), 255);
117 1
        $this->getConnection()->write("\ep" . chr($index[$number]) . chr($on_time) . chr($off_time));
118 1
        return $this;
119
    }
120
121 1
    public function qrcode($data, $size)
122
    {
123 1
        $tipo = '2';
124 1
        $size = $size ?: 4;
125 1
        $error = '0';
126 1
        $len = strlen($data) + 3;
127 1
        $pL = $len & 0xFF;
0 ignored issues
show
Coding Style introduced by
$pL does not seem to conform to the naming convention (^[a-z_][a-z0-9_]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
128 1
        $pH = ($len >> 8) & 0xFF;
0 ignored issues
show
Coding Style introduced by
$pH does not seem to conform to the naming convention (^[a-z_][a-z0-9_]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
129 1
        $this->getConnection()->write("\x1d(k\x04\x001A" . $tipo . "\x00");
130 1
        $this->getConnection()->write("\x1d(k\x03\x001C" . chr($size));
131 1
        $this->getConnection()->write("\x1d(k\x03\x001E" . $error);
132 1
        $this->getConnection()->write("\x1d(k" . chr($pL) . chr($pH) . "1P0");
0 ignored issues
show
Coding Style introduced by
$pL does not seem to conform to the naming convention (^[a-z_][a-z0-9_]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
133 1
        $this->getConnection()->write($data);
134 1
        $this->getConnection()->write("\x1d(k\x03\x001Q0");
135 1
    }
136
137 6
    protected function fontChanged($new_font, $old_font)
138
    {
139 6
        if ($new_font['name'] == 'Font A') {
140 4
            $this->getConnection()->write("\eM\x00");
141 5
        } elseif ($new_font['name'] == 'Font B') {
142 5
            $this->getConnection()->write("\eM\x01");
143
        }
144 6
        parent::fontChanged($new_font, $old_font);
145 6
    }
146
}
147