Passed
Push — master ( 4c34c0...51b2c4 )
by Pol
02:33
created

File   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 186
ccs 60
cts 60
cp 1
rs 10
c 0
b 0
f 0
wmc 17

15 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 11 1
A isExtended() 0 5 1
A isReadable() 0 5 1
A setPosition() 0 5 1
A offsetPosition() 0 12 2
A getPosition() 0 3 1
A size() 0 3 1
A isWritable() 0 5 1
A getMode() 0 3 1
A write() 0 12 1
A seekToEnd() 0 4 1
A isAppendable() 0 5 1
A __construct() 0 5 1
A truncate() 0 10 2
A getFile() 0 3 1
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 9
    public function __construct(NodeFileInterface $file, string $mode, int $position = 0)
39
    {
40 9
        $this->file = $file;
41 9
        $this->mode = $mode;
42 9
        $this->position = $position;
43 9
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 9
    public function getFile(): NodeFileInterface
49
    {
50 9
        return $this->file;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 9
    public function getMode(): string
57
    {
58 9
        return $this->mode;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 9
    public function getPosition(): int
65
    {
66 9
        return $this->position;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 1
    public function isAppendable(): bool
73
    {
74 1
        $modeSplit = \str_split(\str_replace('b', '', $this->getMode()));
75
76 1
        return \in_array('a', $modeSplit, true);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function isExtended(): bool
83
    {
84 1
        $modeSplit = \str_split(\str_replace('b', '', $this->getMode()));
85
86 1
        return \in_array('+', $modeSplit, true);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 4
    public function isReadable(): bool
93
    {
94 4
        $modeSplit = \str_split(\str_replace('b', '', $this->getMode()));
95
96 4
        return \in_array('r', $modeSplit, true);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 6
    public function isWritable(): bool
103
    {
104 6
        $modeSplit = \str_split(\str_replace('b', '', $this->getMode()));
105
106 6
        return \in_array('w', $modeSplit, true);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 4
    public function read(int $bytes): string
113
    {
114 4
        $data = \substr(
115 4
            $this->getFile()->getAttribute('content'),
116 4
            $this->getPosition(),
117 4
            $bytes
118
        );
119
120 4
        $this->offsetPosition($bytes);
121
122 4
        return $data;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 1
    public function seekToEnd()
129
    {
130 1
        return $this->setPosition(
131 1
            \strlen($this->getFile()->getAttribute('content'))
132
        );
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 9
    public function setPosition(int $bytes): File
139
    {
140 9
        $this->position = $bytes;
141
142 9
        return $this;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 9
    public function size(): int
149
    {
150 9
        return \strlen($this->getFile()->getAttribute('content'));
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 7
    public function truncate(int $bytes = 0): File
157
    {
158 7
        $this->setPosition($bytes);
159 7
        $newData = \substr($this->getFile()->getAttribute('content'), 0, $bytes);
160
161 7
        if (\is_string($newData)) {
0 ignored issues
show
introduced by
The condition is_string($newData) is always true.
Loading history...
162 7
            $this->getFile()->setAttribute('content', $newData);
163
        }
164
165 7
        return $this;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171 5
    public function write(string $data): int
172
    {
173 5
        $content = $this->getFile()->getAttribute('content');
174 5
        $content = \substr($content, 0, $this->getPosition());
175
176 5
        $content .= $data;
177 5
        $this->getFile()->setAttribute('content', $content);
178
179 5
        $written = \strlen($data);
180 5
        $this->offsetPosition($written);
181
182 5
        return $written;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 9
    private function offsetPosition(int $offset): int
189
    {
190 9
        $contentSize = $this->size();
191 9
        $newPosition = $this->getPosition() + $offset;
192
193 9
        $newPosition = $contentSize < $newPosition ?
194 3
            $contentSize :
195 9
            $newPosition;
196
197 9
        $this->setPosition($newPosition);
198
199 9
        return $newPosition;
200
    }
201
}
202