Completed
Push — master ( 43e941...fafc95 )
by Roberto
29:58 queued 15:03
created

File::__destruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
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 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
     *
34
     * @param string $filename
35
     */
36
    public function __construct($filename = null)
37
    {
38
        if (is_null($filename) || empty($filename)) {
39
            throw new InvalidArgumentException("A filepath must be passed!");
40
        }
41
        $command = 'rb+';
42
        if (! is_file($filename)) {
43
            $command = 'wb+';    
44
        }
45
        $this->resource = @fopen($filename, $command);
46
        if ($this->resource === false) {
47
            throw new RuntimeException("Failed to open the file. Check the permissions!");
48
        }
49
    }
50
51
    /**
52
     * Destruct conection closing the file
53
     */
54
    public function __destruct()
55
    {
56
        if ($this->resource != false) {
57
            $this->close();
58
        }
59
    }
60
      
61
    /**
62
     * Close file pointer
63
     */
64
    public function close()
65
    {
66
        if (is_resource($this->resource)) {
67
            @fclose($this->resource);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
68
            $this->resource = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type resource of property $resource.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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