Passed
Push — master ( 0f5bf2...4c34c0 )
by Pol
14:44
created

File::truncate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs\StreamWrapper\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
    public function __construct(NodeFileInterface $file, string $mode, int $position = 0)
39
    {
40
        $this->file = $file;
41
        $this->mode = $mode;
42
        $this->position = $position;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getFile(): NodeFileInterface
49
    {
50
        return $this->file;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getMode(): string
57
    {
58
        return $this->mode;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getPosition(): int
65
    {
66
        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
    public function isReadable(): bool
93
    {
94
        $modeSplit = \str_split(\str_replace('b', '', $this->getMode()));
95
96
        return \in_array('r', $modeSplit, true);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function isWritable(): bool
103
    {
104
        $modeSplit = \str_split(\str_replace('b', '', $this->getMode()));
105
106
        return \in_array('w', $modeSplit, true);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function read(int $bytes): string
113
    {
114
        $data = \substr(
115
            $this->getFile()->getAttribute('content'),
116
            $this->getPosition(),
117
            $bytes
118
        );
119
120
        $this->offsetPosition($bytes);
121
122
        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
    public function setPosition(int $bytes): File
149
    {
150
        $this->position = $bytes;
151
152
        return $this;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function size(): int
159
    {
160
        return \strlen($this->getFile()->getAttribute('content'));
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function truncate(int $bytes = 0)
167
    {
168
        $this->setPosition(0);
169
        $newData = \substr($this->getFile()->getAttribute('content'), 0, $bytes);
170
171
        if (\is_string($newData)) {
0 ignored issues
show
introduced by
The condition is_string($newData) is always true.
Loading history...
172
            $this->getFile()->setAttribute('content', $newData);
173
        }
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function write(string $data): int
180
    {
181
        $content = $this->getFile()->getAttribute('content');
182
        $content = \substr($content, 0, $this->getPosition());
183
184
        $content .= $data;
185
        $this->getFile()->setAttribute('content', $content);
186
187
        $written = \strlen($data);
188
        $this->offsetPosition($written);
189
190
        return $written;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    private function offsetPosition(int $offset): int
197
    {
198
        $contentSize = $this->size();
199
        $newPosition = $this->getPosition() + $offset;
200
201
        $newPosition = $contentSize < $newPosition ?
202
            $contentSize :
203
            $newPosition;
204
205
        $this->setPosition($newPosition);
206
207
        return $newPosition;
208
    }
209
}
210