BaseFileStream::write()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.0185
1
<?php
2
namespace pastuhov\FileStream;
3
4
/**
5
 * Class BaseFileStream.
6
 * @package pastuhov\FileStream
7
 */
8
class BaseFileStream
9
{
10
    /**
11
     * Base file name.
12
     *
13
     * Example:
14
     * /var/www/export.yml
15
     * /var/www/sitemap{count}.xml
16
     *
17
     * @var string
18
     */
19
    protected $fileName;
20
21
    /**
22
     * @param string $fileName file name
23
     */
24 9
    public function __construct($fileName)
25
    {
26 9
        $this->fileName = $fileName;
27 9
    }
28
29
    private $_handle = false;
30
31
    /**
32
     * Get file handle.
33
     * @return bool|resource
34
     * @throws \Exception
35
     */
36 6
    public function getHandle()
37
    {
38 6
        if ($this->_handle === false) {
39 6
            $this->openHandle();
40 6
        }
41
42 6
        return $this->_handle;
43
    }
44
45
    /**
46
     * Open new file handle.
47
     * @throws \Exception
48
     */
49 6
    protected function openHandle()
50
    {
51 6
        $fileName = $this->getFileName();
52
53 6
        $this->_handle = fopen($fileName, 'w');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($fileName, 'w') of type resource is incompatible with the declared type boolean of property $_handle.

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...
54
55 6
        if (!$this->_handle) {
56
            throw new \Exception('Cannot open file ' . $fileName);
57
        }
58 6
    }
59
60
    /**
61
     * Binary-safe file handle write.
62
     * @param string $string contents
63
     * @throws \Exception
64
     */
65 6
    public function write($string)
66
    {
67 6
        $fileName = $this->getFileName();
68 6
        $handle = $this->getHandle();
69
70 6
        if (fwrite($handle, $string) === false) {
71
            throw new \Exception('Cannot write to file ' . $fileName);
72
        }
73 6
    }
74
75
    /**
76
     * Close file handle.
77
     * @throws \Exception
78
     */
79 6
    protected function closeHandle()
80
    {
81 6
        $handle = $this->getHandle();
82 6
        fclose($handle);
83 6
        $this->_handle = false;
84
85 6
        $this->runCommand($this->getFileName());
86 6
    }
87
88
    /**
89
     * @var \CommandInterface[]
90
     */
91
    protected $commands = [];
92 6
    protected function runCommand($filename)
93
    {
94 6
        foreach ($this->commands as $command) {
95 3
            $command->runCommand($filename);
96 6
        }
97 6
    }
98
99 3
    public function addCommand(CommandInterface $command)
100
    {
101 3
        $this->commands[] = $command;
102 3
    }
103
104
    /**
105
     * Base file name with replaced count placeholder.
106
     * @return string Base file name with replaced placeholder
107
     */
108 6
    protected function getFileName()
109
    {
110 6
        $fileName = $this->fileName;
111
112 6
        return $fileName;
113
    }
114
115
    /**
116
     * Destruct.
117
     */
118 6
    public function __destruct()
119
    {
120 6
        $this->closeHandle();
121 6
    }
122
}
123