Passed
Push — master ( 62941e...7e45f3 )
by Pol
02:18
created

PhpVfs::stripScheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 2
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs;
6
7
use drupol\phpvfs\Command\Cd;
8
use drupol\phpvfs\Command\Exist;
9
use drupol\phpvfs\Command\Get;
10
use drupol\phpvfs\Filesystem\Filesystem;
11
use drupol\phpvfs\Filesystem\FilesystemInterface;
12
use drupol\phpvfs\Node\File;
13
use drupol\phpvfs\Node\FileInterface;
14
use drupol\phpvfs\Utils\Path;
15
16
class PhpVfs
17
{
18
    public const SCHEME = 'phpvfs';
19
20
    /**
21
     * @var array
22
     */
23
    public $context;
24
25
    /**
26
     * @return \drupol\phpvfs\Filesystem\FilesystemInterface
27
     */
28 6
    public static function fs(): FilesystemInterface
29
    {
30 6
        $options = \stream_context_get_options(
31 6
            \stream_context_get_default()
32
        );
33
34 6
        return $options[static::SCHEME]['filesystem'];
35
    }
36
37
    /**
38
     * @param \drupol\phpvfs\Filesystem\Filesystem $filesystem
39
     * @param array $options
40
     */
41 6
    public static function register(Filesystem $filesystem, array $options = [])
42
    {
43
        $options = [
44 6
            static::SCHEME => [
45 6
                'filesystem' => $filesystem,
46
                'currentFile' => null,
47 6
            ] + $options,
48
        ];
49
50 6
        \stream_context_set_default($options);
51 6
        \stream_wrapper_register(self::SCHEME, __CLASS__);
52 6
    }
53
54
    /**
55
     * @param string $from
56
     * @param string $to
57
     *
58
     * @throws \Exception
59
     *
60
     * @return bool
61
     */
62 1
    public function rename(string $from, string $to): bool
63
    {
64 1
        if (!Exist::exec($this::fs(), $from)) {
65
            throw new \Exception('Source resource does not exist.');
66
        }
67
68 1
        $from = Get::exec($this::fs(), $from);
69
70 1
        if (Exist::exec($this::fs(), $to)) {
71 1
            throw new \Exception('Destination already exist.');
72
        }
73
74 1
        $toPath = Path::fromString($to);
75
76 1
        $this::fs()
77 1
            ->getCwd()
78 1
            ->mkdir($toPath->dirname());
79
80 1
        if (null !== $parent = $from->getParent()) {
81 1
            $parent->delete($from);
82
        }
83
84 1
        Cd::exec($this::fs(), $toPath->dirname());
85
86 1
        $from->setAttribute('id', $toPath->basename());
87
88 1
        if ($from instanceof FileInterface) {
89 1
            $from->setPosition(0);
90
        }
91
92 1
        $this::fs()
93 1
            ->getCwd()
94 1
            ->add($from);
95
96 1
        return true;
97
    }
98
99
    /**
100
     * @see http://php.net/streamwrapper.stream-close
101
     */
102 6
    public function stream_close(): void // phpcs:ignore
103
    {
104 6
        if (null !== $file = $this->getCurrentFile()) {
105 6
            $file->setPosition(0);
106
        }
107
108 6
        $this->setCurrentFile(null);
109 6
    }
110
111
    /**
112
     * @return bool
113
     *
114
     * @see http://php.net/streamwrapper.stream-eof
115
     */
116 1
    public function stream_eof(): bool // phpcs:ignore
117
    {
118 1
        return true;
119
    }
120
121
    /**
122
     * @param string $resource
123
     * @param mixed $mode
124
     * @param mixed $options
125
     * @param mixed $openedPath
126
     *
127
     * @throws \Exception
128
     *
129
     * @return bool
130
     */
131 6
    public function stream_open(string $resource, $mode, $options, &$openedPath) // phpcs:ignore
0 ignored issues
show
Unused Code introduced by
The parameter $openedPath is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

131
    public function stream_open(string $resource, $mode, $options, /** @scrutinizer ignore-unused */ &$openedPath) // phpcs:ignore

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
    {
133 6
        $mode = \str_split(\str_replace('b', '', $mode));
134
135 6
        $appendMode = \in_array('a', $mode, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $appendMode is dead and can be removed.
Loading history...
136 6
        $readMode = \in_array('r', $mode, true);
137 6
        $writeMode = \in_array('w', $mode, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $writeMode is dead and can be removed.
Loading history...
138 6
        $extended = \in_array('+', $mode, true);
0 ignored issues
show
Unused Code introduced by
The assignment to $extended is dead and can be removed.
Loading history...
139
140 6
        $resourcePath = Path::fromString($resource);
141
142 6
        if (!Exist::exec($this::fs(), $resource)) {
143
            if ($readMode || !Exist::exec($this::fs(), $resourcePath->dirname())) {
144
                if ($options & STREAM_REPORT_ERRORS) {
145
                    \trigger_error(\sprintf('%s: failed to open stream.', $resourcePath), E_USER_WARNING);
146
                }
147
148
                return false;
149
            }
150
151
            $file = File::create($resource);
152
153
            $this->setCurrentFile($file);
154
            $this::fs()
155
                ->getCwd()
156
                ->add($file->root());
157
        }
158
159 6
        if (null === $file = $this::fs()->get($resource)) {
160
            return false;
161
        }
162
163 6
        if (!($file instanceof FileInterface)) {
164
            return false;
165
        }
166
167 6
        $file->setPosition(0);
168
169 6
        $this->setCurrentFile($file);
170
171 6
        return true;
172
    }
173
174
    /**
175
     * @see http://php.net/streamwrapper.stream-read
176
     *
177
     * @param int $bytes
178
     *
179
     * @return mixed
180
     */
181
    public function stream_read(int $bytes) // phpcs:ignore
182
    {
183
        if (null !== $file = $this->getCurrentFile()) {
184
            return $file->read($bytes);
185
        }
186
187
        return false;
188
    }
189
190
    /**
191
     * @return array
192
     */
193 1
    public function stream_stat(): array // phpcs:ignore
194
    {
195 1
        if (null === $file = $this->getCurrentFile()) {
196 1
            return [];
197
        }
198
199 1
        return (array) $file->getAttributes();
200
    }
201
202
    /**
203
     * @param string $data
204
     *
205
     * @return int
206
     */
207 6
    public function stream_write(string $data) // phpcs:ignore
208
    {
209 6
        if (null !== $file = $this->getCurrentFile()) {
210 6
            return $file->write($data);
211
        }
212
213
        return 0;
214
    }
215
216
    /**
217
     * @param string $path
218
     *
219
     * @throws \Exception
220
     */
221 1
    public function unlink(string $path)
222
    {
223 1
        if (true === Exist::exec($this::fs(), $path)) {
224 1
            $file = Get::exec($this::fs(), $path);
225
226 1
            if (null !== $parent = $file->getParent()) {
227 1
                $parent->delete($file);
228
            }
229
        }
230 1
    }
231
232
    /**
233
     * @todo
234
     */
235 6
    public static function unregister()
236
    {
237 6
        \stream_wrapper_unregister(self::SCHEME);
238 6
    }
239
240
    /**
241
     * @return null|FileInterface
242
     */
243 6
    protected function getCurrentFile(): ?FileInterface
244
    {
245 6
        $options = \stream_context_get_options(
246 6
            \stream_context_get_default()
247
        );
248
249 6
        return $options[static::SCHEME]['currentFile'];
250
    }
251
252
    /**
253
     * @param null|FileInterface $file
254
     */
255 6
    protected function setCurrentFile(?FileInterface $file)
256
    {
257 6
        $options = \stream_context_get_options(
258 6
            \stream_context_get_default()
259
        );
260
261 6
        $options[static::SCHEME]['currentFile'] = $file;
262
263 6
        \stream_context_set_default($options);
264 6
    }
265
}
266