Issues (18)

php-src/Wrappers/Fsp/File.php (1 issue)

Severity
1
<?php
2
3
namespace kalanis\RemoteRequest\Wrappers\Fsp;
4
5
6
use kalanis\RemoteRequest;
7
use kalanis\RemoteRequest\Protocols\Fsp as Protocol;
8
9
10
/**
11
 * Class File
12
 * @package kalanis\RemoteRequest\Wrappers\Fsp
13
 * Wrapper to plug FSP info into PHP - files part
14
 */
15
class File extends AOperations
16
{
17
    protected string $path = '';
18
    protected int $size = 0;
19
    protected int $position = 0;
20
    /**
21
     * @var bool
22
     * read - false, write - true
23
     */
24
    protected bool $writeMode = false;
25
26
    /**
27
     * @param int $cast_as
28
     * @return resource|bool
29
     */
30
    public function stream_cast(/** @scrutinizer ignore-unused */ int $cast_as)
31
    {
32
        return false;
33
    }
34
35
    /**
36
     * @throws RemoteRequest\RequestException
37
     */
38
    public function stream_close(): void
39
    {
40
        if ($this->writeMode) {
41
            $inFile = new Protocol\Query\Install($this->runner->getQuery());
42
            $inFile->setFilePath($this->parsePath($this->path));
43
            $answer = $this->runner->setActionQuery($inFile)->process();
44
            if (!$answer instanceof Protocol\Answer\Nothing) {
45
                throw new RemoteRequest\RequestException($this->getRRLang()->rrFspBadResponsePublish(get_class($answer)));
46
            }
47
        }
48
    }
49
50
    public function stream_eof(): bool
51
    {
52
        return (!$this->writeMode) && ($this->position >= $this->size);
53
    }
54
55
    public function stream_flush(): bool
56
    {
57
        return false;
58
    }
59
60
    public function stream_lock(/** @scrutinizer ignore-unused */ int $operation): bool
61
    {
62
        return false;
63
    }
64
65
    /**
66
     * @param string $path
67
     * @param int $option
68
     * @param mixed $var
69
     * @return bool
70
     */
71
    public function stream_metadata(/** @scrutinizer ignore-unused */ string $path, /** @scrutinizer ignore-unused */ int $option, $var): bool
0 ignored issues
show
The parameter $var 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

71
    public function stream_metadata(/** @scrutinizer ignore-unused */ string $path, /** @scrutinizer ignore-unused */ int $option, /** @scrutinizer ignore-unused */ $var): bool

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...
72
    {
73
        return false;
74
    }
75
76
    /**
77
     * @param Dir $libDir
78
     * @param string $path
79
     * @param string $mode
80
     * @throws RemoteRequest\RequestException
81
     * @return bool
82
     */
83
    public function stream_open(Dir $libDir, string $path, string $mode): bool
84
    {
85
        $this->path = $path;
86
        $this->writeMode = $this->parseWriteMode($mode);
87
88
        if (!$this->writeMode) {
89
            $stat = $this->stream_stat($libDir);
90
            $this->size = $stat[7]; // stats - max available size
91
        }
92
        $this->position = 0;
93
        return true;
94
    }
95
96
    /**
97
     * @param string $mode
98
     * @throws RemoteRequest\RequestException
99
     * @return bool
100
     */
101
    protected function parseWriteMode(string $mode): bool
102
    {
103
        $mod = strtolower(substr(strtr($mode, ['+' => '', 'b' => '', 'e' => '']), 0, 1));
104
        if ('r' == $mod) {
105
            return false;
106
        }
107
        if (in_array($mod, ['w', 'a', 'x', 'c'])) {
108
            return true;
109
        }
110
        throw new RemoteRequest\RequestException($this->getRRLang()->rrFspBadFileMode($mode));
111
    }
112
113
    /**
114
     * @param int $count
115
     * @throws RemoteRequest\RequestException
116
     * @return string
117
     */
118
    public function stream_read(int $count): string
119
    {
120
        $readFile = new Protocol\Query\GetFile($this->runner->getQuery());
121
        $readFile->setFilePath($this->parsePath($this->path))->setOffset($this->position)->setLimit($count);
122
        $answer = $this->runner->setActionQuery($readFile)->process();
123
        if (!$answer instanceof Protocol\Answer\GetFile) {
124
            throw new RemoteRequest\RequestException($this->getRRLang()->rrFspBadResponseRead(get_class($answer)));
125
        }
126
        if ($answer->getSeek() != $this->position) {
127
            throw new RemoteRequest\RequestException($this->getRRLang()->rrFspReadWrongSeek($this->position, $answer->getSeek()));
128
        }
129
        $ret = $answer->getContent();
130
        $this->position += strlen($ret);
131
        return $ret;
132
    }
133
134
    public function stream_seek(int $offset, int $whence = SEEK_SET): bool
135
    {
136
        switch ($whence) {
137
            case SEEK_SET:
138
                if ($offset < $this->size && 0 <= $offset) {
139
                    $this->position = $offset;
140
                    return true;
141
                } else {
142
                    return false;
143
                }
144
145
            case SEEK_CUR:
146
                if (0 <= $offset) {
147
                    $this->position += $offset;
148
                    return true;
149
                } else {
150
                    return false;
151
                }
152
153
            case SEEK_END:
154
                if (0 <= $this->size + $offset) {
155
                    $this->position = $this->size + $offset;
156
                    return true;
157
                } else {
158
                    return false;
159
                }
160
161
            default:
162
                return false;
163
        }
164
    }
165
166
    public function stream_set_option(/** @scrutinizer ignore-unused */ int $option, /** @scrutinizer ignore-unused */ int $arg1, /** @scrutinizer ignore-unused */ int $arg2): bool
167
    {
168
        return false;
169
    }
170
171
    /**
172
     * @param Dir $libDir
173
     * @throws RemoteRequest\RequestException
174
     * @return array<int, int>
175
     */
176
    public function stream_stat(Dir $libDir): array
177
    {
178
        return $libDir->stats($this->path, 0);
179
    }
180
181
    public function stream_tell(): int
182
    {
183
        return $this->position;
184
    }
185
186
    public function stream_truncate(/** @scrutinizer ignore-unused */ int $new_size): bool
187
    {
188
        return false;
189
    }
190
191
    /**
192
     * @param string $data
193
     * @throws RemoteRequest\RequestException
194
     * @return int
195
     */
196
    public function stream_write(string $data): int
197
    {
198
        if (!$this->writeMode) {
199
            throw new RemoteRequest\RequestException($this->getRRLang()->rrFspFileCannotWrite());
200
        }
201
        $upFile = new Protocol\Query\Upload($this->runner->getQuery());
202
        $upFile->setFilePath($this->parsePath($this->path))->setOffset($this->position)->setData($data);
203
        $answer = $this->runner->setActionQuery($upFile)->process();
204
        if (!$answer instanceof Protocol\Answer\Upload) {
205
            throw new RemoteRequest\RequestException($this->getRRLang()->rrFspBadResponseUpload(get_class($answer)));
206
        }
207
        $dataLen = strlen($data);
208
        if ($answer->getSeek() != $this->position + $dataLen) {
209
            throw new RemoteRequest\RequestException($this->getRRLang()->rrFspWriteWrongSeek($this->position + $dataLen, $answer->getSeek()));
210
        }
211
        $this->position = $answer->getSeek();
212
        return $dataLen;
213
    }
214
215
    /**
216
     * @param string $path
217
     * @throws RemoteRequest\RequestException
218
     * @return bool
219
     */
220
    public function unlink(string $path): bool
221
    {
222
        $delFile = new Protocol\Query\DelFile($this->runner->getQuery());
223
        $delFile->setFilePath($this->parsePath($path));
224
        $answer = $this->runner->setActionQuery($delFile)->process();
225
        if (!$answer instanceof Protocol\Answer\Nothing) {
226
            throw new RemoteRequest\RequestException($this->getRRLang()->rrFspBadResponseUnlink(get_class($answer)));
227
        }
228
        return true;
229
    }
230
}
231