Completed
Push — master ( c5f7b8...442191 )
by Roberto
03:20
created

Serial::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 0
cts 8
cp 0
rs 9.4285
cc 1
eloc 6
nc 1
nop 4
crap 2
1
<?php
2
3
namespace Posprint\Connectors;
4
5
/**
6
 * Classe Serial
7
 * 
8
 * @category   NFePHP
9
 * @package    Posprint
10
 * @copyright  Copyright (c) 2015
11
 * @license    http://www.gnu.org/licenses/lesser.html LGPL v3
12
 * @author     Roberto L. Machado <linux.rlm at gmail dot com>
13
 * @link       http://github.com/nfephp-org/posprint for the canonical source repository
14
 */
15
16
use Posprint\Connectors\ConnectorInterface;
17
use PhpSerial;
18
19
class Serial implements ConnectorInterface
20
{
21
    protected $resource;
22
    protected $device = "/dev/ttyS0";
23
    protected $baudRate = 9600;
24
    protected $byteSize = 8;
25
    protected $parity = 'none';
26
    protected $deviceStatus = null;
27
    
28
    public function __construct($device = "/dev/ttyS0", $baudRate = 9600, $byteSize = 8, $parity = 'none')
29
    {
30
        $this->setDevice($device);
31
        $this->setBaudRate($baudRate);
32
        $this->setByteSize($byteSize);
33
        $this->setParity($parity);
34
        $this->initialize();
35
    }
36
    
37
    protected function initialize()
38
    {
39
        $this->resource = new PhpSerial();
40
        $this->resource->deviceSet($this->device);
41
        $this->resource->confBaudRate($this->baudRate);
42
        $this->resource->confCharacterLength($this->byteSize);
43
        $this->resource->confParity($this->parity);
44
        $this->deviceStatus = $this->resource->deviceOpen();
45
    }
46
    
47
    public function __destruct()
48
    {
49
        if ($this->deviceStatus == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
50
            $this->close();
51
        }
52
    }
53
    
54
    public function setDevice($device = "/dev/ttyS0")
55
    {
56
        $this->device = $device;
57
    }
58
    
59
    public function setBaudRate($baudRate = 9600)
60
    {
61
        $this->baudRate = $baudRate;
62
    }
63
    
64
    public function setByteSize($byteSize = 8)
65
    {
66
        $this->byteSize = $byteSize;
67
    }
68
    
69
    public function setParity($parity = 'none')
70
    {
71
        $this->parity = $parity;
72
    }
73
    
74
    /**
75
     * Finalize printer connection
76
     */
77
    public function close()
78
    {
79
        $this->deviceStatus = ! $this->resource->deviceClose();
80
    }
81
82
    /**
83
     * send data to printer
84
     * @param string $data
85
     */
86
    public function write($data)
87
    {
88
        $this->resource->sendMessage($data);
89
    }
90
}
91