Completed
Push — master ( fb8009...503045 )
by Roberto
20:15 queued 10:07
created

File   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.22%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 87
ccs 35
cts 36
cp 0.9722
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 7 3
B __construct() 0 16 6
A __destruct() 0 6 2
A close() 0 9 3
B read() 0 15 5
1
<?php
2
3
namespace Posprint\Connectors;
4
5
/**
6
 * Class File
7
 * Create a binary file and writes the data line by line.
8
 * And it can also be used to provide direct connections to USB ports.
9
 *
10
 * @category  NFePHP
11
 * @package   Posprint
12
 * @copyright Copyright (c) 2016
13
 * @license   http://www.gnu.org/licenses/lesser.html LGPL v3
14
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
15
 * @link      http://github.com/nfephp-org/posprint for the canonical source repository
16
 */
17
18
use Posprint\Connectors\ConnectorInterface;
19
use RuntimeException;
20
use InvalidArgumentException;
21
22
class File implements ConnectorInterface
23
{
24
    /**
25
     * @var resource|bool The file pointer to send data to.
26
     */
27
    protected $resource = false;
28
29
    /**
30
     * Construct new connector, given a filename
31
     * If created a binary file must be granted the necessary
32
     * permissions to create and write the system file
33
     * @param string|null $filename
34
     */
35
    public function __construct($filename = null)
36 8
    {
37
        if (!is_null($filename) && empty($filename)) {
38 8
            throw new InvalidArgumentException("A filename must be passed!");
39 1
        }
40
        if (!is_null($filename)) {
41 7
            $command = 'rb+';
42 7
            if (!is_file($filename)) {
43 1
                $command = 'wb+';
44 1
            }
45 7
            $this->resource = @fopen($filename, $command);
46 7
            if ($this->resource === false) {
47 1
                throw new RuntimeException("Failed to open the file. Check the permissions!");
48
            }
49 6
        }
50
    }
51
52
    /**
53
     * Destruct conection closing the file
54 6
     */
55
    public function __destruct()
56 6
    {
57 5
        if ($this->resource != false) {
58 5
            $this->close();
59 6
        }
60
    }
61
      
62
    /**
63
     * Close file pointer
64 6
     */
65
    public function close()
66 6
    {
67 6
        if (is_resource($this->resource)) {
68
            if (! @fclose($this->resource)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
69
                //when a fclose returns false ??
70 6
            }
71 6
        }
72 6
        $this->resource = false;
73
    }
74
75
    /**
76
     * Write data to the file
77
     * @param string $data
78
     * @return int
79
     */
80 2
    public function write($data = '')
81
    {
82 2
        if (is_resource($this->resource) && !empty($data)) {
83 1
            return (int) fwrite($this->resource, $data);
84
        }
85 1
        return 0;
86
    }
87
    
88
    /**
89
     * Read some bytes from file
90
     * @param  int $len
91
     * @return string
92
     */
93 2
    public function read($len = null)
94
    {
95 2
        $data = '';
96 2
        if (!is_resource($this->resource)) {
97 1
            return $data;
98 1
        }
99
        if (!is_null($len) && is_numeric($len)) {
100 1
            $len = ceil($len);
101 1
            return fread($this->resource, $len);
102 1
        }
103 1
        while (!feof($this->resource)) {
104
            $data .= fread($this->resource, 4096);
105
        }
106
        return $data;
107
    }
108
}
109