ThermalPrinter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 84
ccs 0
cts 65
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A init() 0 7 1
A writeString() 0 5 1
A hr() 0 7 1
A hhr() 0 7 1
A inv() 0 6 2
A fiscal() 0 13 1
A writeText() 0 5 1
A __destruct() 0 5 1
1
<?php
2
3
namespace AppBundle;
4
5
class ThermalPrinter
6
{
7
    const PATH = '/dev/serial0';
8
9
    private $fp = null;
10
11
    private $isInverse = false;
12
13
    public function __construct($debug = false)
14
    {
15
        if ($debug) {
16
            $this->fp = fopen('php://stdout', 'w+');
17
        } else {
18
            $this->fp = fopen(self::PATH, 'w+');
19
        }
20
21
        $this->init();
22
23
        return $this;
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
24
    }
25
26
    public function init()
27
    {
28
        fwrite($this->fp, chr(27).chr(64)); // flush printer
29
        fwrite($this->fp, chr(27).chr(116).chr(6)); // set codepage
30
        fwrite($this->fp, chr(27).chr(55).chr(8).chr(100).chr(5)); // set codepage
31
        return $this;
32
    }
33
34
    public function writeString($string)
35
    {
36
        fwrite($this->fp, wordwrap(iconv('UTF-8', 'cp1251', $string), 32));
37
        return $this;
38
    }
39
40
    public function hr()
41
    {
42
        fwrite($this->fp, "\n\n\n");
43
        fwrite($this->fp, '================================');
44
        fwrite($this->fp, "\n\n\n");
45
        return $this;
46
    }
47
48
    public function hhr()
49
    {
50
        fwrite($this->fp, "\n\n\n");
51
        $this->inv()->writeString(str_repeat(' ', 32))->inv();
52
        fwrite($this->fp, "\n\n\n");
53
        return $this;
54
    }
55
56
    public function inv()
57
    {
58
        $this->isInverse = !$this->isInverse;
59
        fwrite($this->fp, chr(29).chr(66).chr($this->isInverse?1:0));
60
        return $this;
61
    }
62
63
    public function fiscal()
64
    {
65
        fwrite($this->fp, "\n");
66
        $this->inv();
67
        fwrite($this->fp, '           '.iconv(
68
            'UTF-8',
69
            'cp1251',
70
            '<D0><A4><D0><98><D0><A1><D0><9A><D0><90><D0><9B><D0><AC><D0><9D><D0><AB><D0><99>'
71
        ).'           ');
72
        $this->inv();
73
        fwrite($this->fp, "\n");
74
        return $this;
75
    }
76
77
    public function writeText($text)
78
    {
79
        fwrite($this->fp, iconv('UTF-8', 'cp1251', $text));
80
        return $this;
81
    }
82
83
    public function __destruct()
84
    {
85
        fwrite($this->fp, "\n\n\n\n\n\n\n");
86
        fclose($this->fp);
87
    }
88
}
89