Passed
Push — master ( ee003d...721255 )
by Pol
03:14
created

File::isReadable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs\Handler;
6
7
use drupol\phpvfs\Node\FileInterface as NodeFileInterface;
8
9
/**
10
 * Class File.
11
 *
12
 * @internal
13
 */
14
final class File implements FileInterface
15
{
16
    /**
17
     * @var \drupol\phpvfs\Node\FileInterface
18
     */
19
    private $file;
20
21
    /**
22
     * @var string
23
     */
24
    private $mode;
25
26
    /**
27
     * @var int
28
     */
29
    private $position;
30
31
    /**
32
     * File constructor.
33
     *
34
     * @param \drupol\phpvfs\Node\FileInterface $file
35
     * @param string $mode
36
     * @param int $position
37
     */
38 7
    public function __construct(NodeFileInterface $file, string $mode, int $position = 0)
39
    {
40 7
        $this->file = $file;
41 7
        $this->mode = $mode;
42 7
        $this->position = $position;
43 7
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 7
    public function getFile(): NodeFileInterface
49
    {
50 7
        return $this->file;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 7
    public function getMode(): string
57
    {
58 7
        return $this->mode;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 7
    public function getPosition(): int
65
    {
66 7
        return $this->position;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function isAppendable(): bool
73
    {
74
        $modeSplit = \str_split(\str_replace('b', '', $this->getMode()));
75
76
        return \in_array('a', $modeSplit, true);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function isExtended(): bool
83
    {
84
        $modeSplit = \str_split(\str_replace('b', '', $this->getMode()));
85
86
        return \in_array('+', $modeSplit, true);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 2
    public function isReadable(): bool
93
    {
94 2
        $modeSplit = \str_split(\str_replace('b', '', $this->getMode()));
95
96 2
        return \in_array('r', $modeSplit, true);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 5
    public function isWritable(): bool
103
    {
104 5
        $modeSplit = \str_split(\str_replace('b', '', $this->getMode()));
105
106 5
        return \in_array('w', $modeSplit, true);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 2
    public function read(int $bytes): string
113
    {
114 2
        $data = \substr(
115 2
            $this->getFile()->getAttribute('content'),
116 2
            $this->getPosition(),
117 2
            $bytes
118
        );
119
120 2
        $this->offsetPosition($bytes);
121
122 2
        return $data;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function seekToEnd()
129
    {
130
        return $this->setPosition(
131
            \strlen($this->getFile()->getAttribute('content'))
132
        );
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function setMode(string $mode): File
139
    {
140
        $this->mode = $mode;
141
142
        return $this;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 7
    public function setPosition(int $bytes): File
149
    {
150 7
        $this->position = $bytes;
151
152 7
        return $this;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 7
    public function size(): int
159
    {
160 7
        return \strlen($this->getFile()->getAttribute('content'));
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 6
    public function truncate(int $bytes = 0)
167
    {
168 6
        $this->setPosition(0);
169 6
        $newData = \substr($this->getFile()->getAttribute('content'), 0, $bytes);
170
171 6
        if (\is_string($newData)) {
0 ignored issues
show
introduced by
The condition is_string($newData) is always true.
Loading history...
172 6
            $this->getFile()->setAttribute('content', $newData);
173
        }
174 6
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179 5
    public function write(string $data): int
180
    {
181 5
        $content = $this->getFile()->getAttribute('content');
182 5
        $content = \substr($content, 0, $this->getPosition());
183
184 5
        $content .= $data;
185 5
        $this->getFile()->setAttribute('content', $content);
186
187 5
        $written = \strlen($data);
188 5
        $this->offsetPosition($written);
189
190 5
        return $written;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196 7
    private function offsetPosition($offset): int
197
    {
198 7
        $contentSize = $this->size();
199 7
        $newPosition = $this->getPosition() + $offset;
200
201 7
        $newPosition = $contentSize < $newPosition ?
202 2
            $contentSize :
203 7
            $newPosition;
204
205 7
        $newPosition = (int) $newPosition;
206
207 7
        $this->setPosition($newPosition);
208
209 7
        return $newPosition;
210
    }
211
}
212