Passed
Push — master ( 0c29e2...c7b87e )
by Pol
02:37
created

File   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Test Coverage

Coverage 80.95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 194
ccs 51
cts 63
cp 0.8095
rs 10
wmc 18

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getMode() 0 3 1
A seekToEnd() 0 4 1
A getPosition() 0 3 1
A isWritable() 0 5 1
A size() 0 3 1
A isAppendable() 0 5 1
A setPosition() 0 5 1
A truncate() 0 7 2
A setMode() 0 5 1
A isExtended() 0 5 1
A write() 0 12 1
A isReadable() 0 5 1
A __construct() 0 5 1
A getFile() 0 3 1
A read() 0 11 1
A offsetPosition() 0 12 2
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 8
    public function __construct(NodeFileInterface $file, string $mode, int $position = 0)
39
    {
40 8
        $this->file = $file;
41 8
        $this->mode = $mode;
42 8
        $this->position = $position;
43 8
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 8
    public function getFile(): NodeFileInterface
49
    {
50 8
        return $this->file;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 8
    public function getMode(): string
57
    {
58 8
        return $this->mode;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 8
    public function getPosition(): int
65
    {
66 8
        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 3
    public function isReadable(): bool
93
    {
94 3
        $modeSplit = \str_split(\str_replace('b', '', $this->getMode()));
95
96 3
        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 3
    public function read(int $bytes): string
113
    {
114 3
        $data = \substr(
115 3
            $this->getFile()->getAttribute('content'),
116 3
            $this->getPosition(),
117 3
            $bytes
118
        );
119
120 3
        $this->offsetPosition($bytes);
121
122 3
        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 8
    public function setPosition(int $bytes): File
149
    {
150 8
        $this->position = $bytes;
151
152 8
        return $this;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 8
    public function size(): int
159
    {
160 8
        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 8
    private function offsetPosition(int $offset): int
197
    {
198 8
        $contentSize = $this->size();
199 8
        $newPosition = $this->getPosition() + $offset;
200
201 8
        $newPosition = $contentSize < $newPosition ?
202 2
            $contentSize :
203 8
            $newPosition;
204
205 8
        $this->setPosition($newPosition);
206
207 8
        return $newPosition;
208
    }
209
}
210