• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

valkyrjaio / valkyrja / 13047041070

30 Jan 2025 06:43AM UTC coverage: 47.621% (+0.2%) from 47.422%
13047041070

push

github

MelechMizrachi
PHPStan level 7 and 8.

168 of 1038 new or added lines in 111 files covered. (16.18%)

444 existing lines in 45 files now uncovered.

5195 of 10909 relevant lines covered (47.62%)

18.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.75
/src/Valkyrja/Filesystem/Adapter/InMemoryAdapter.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <melechmizrachi@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Valkyrja\Filesystem\Adapter;
15

16
use Valkyrja\Exception\RuntimeException;
17
use Valkyrja\Filesystem\Adapter\Contract\Adapter as Contract;
18
use Valkyrja\Filesystem\Data\InMemoryFile;
19
use Valkyrja\Filesystem\Data\InMemoryMetadata;
20
use Valkyrja\Filesystem\Enum\Visibility;
21
use Valkyrja\Filesystem\Exception\UnableToReadContentsException;
22

23
use function fread;
24
use function str_starts_with;
25
use function time;
26

27
/**
28
 * Class InMemoryAdapter.
29
 *
30
 * @author Melech Mizrachi
31
 */
32
class InMemoryAdapter implements Contract
33
{
34
    /**
35
     * @var InMemoryFile[]
36
     */
37
    protected array $files = [];
38

39
    public function __construct(
40
        InMemoryFile ...$files
41
    ) {
42
        $this->files = $files;
4✔
43
    }
44

45
    /**
46
     * @inheritDoc
47
     */
48
    public function exists(string $path): bool
49
    {
50
        return isset($this->files[$path]);
4✔
51
    }
52

53
    /**
54
     * @inheritDoc
55
     */
56
    public function read(string $path): string
57
    {
58
        return $this->files[$path]->contents
4✔
59
            ?? throw new UnableToReadContentsException("Error reading file contents for $path");
4✔
60
    }
61

62
    /**
63
     * @inheritDoc
64
     */
65
    public function write(string $path, string $contents): bool
66
    {
67
        $this->files[$path] = new InMemoryFile($path, $contents, timestamp: time());
4✔
68

69
        return true;
4✔
70
    }
71

72
    /**
73
     * @inheritDoc
74
     *
75
     * @param resource $resource The resource
76
     */
77
    public function writeStream(string $path, $resource): bool
78
    {
NEW
79
        $pathContents = fread($resource, 4096);
×
80

NEW
81
        if ($pathContents === false) {
×
NEW
82
            throw new RuntimeException('Failed to read provided resource');
×
83
        }
84

NEW
85
        $this->files[$path] = new InMemoryFile($path, $pathContents, timestamp: time());
×
86

87
        return true;
×
88
    }
89

90
    /**
91
     * @inheritDoc
92
     */
93
    public function update(string $path, string $contents): bool
94
    {
95
        return $this->write($path, $contents);
×
96
    }
97

98
    /**
99
     * @inheritDoc
100
     */
101
    public function updateStream(string $path, $resource): bool
102
    {
103
        return $this->writeStream($path, $resource);
×
104
    }
105

106
    /**
107
     * @inheritDoc
108
     */
109
    public function put(string $path, string $contents): bool
110
    {
111
        return $this->write($path, $contents);
×
112
    }
113

114
    /**
115
     * @inheritDoc
116
     */
117
    public function putStream(string $path, $resource): bool
118
    {
119
        return $this->writeStream($path, $resource);
×
120
    }
121

122
    /**
123
     * @inheritDoc
124
     */
125
    public function rename(string $path, string $newPath): bool
126
    {
127
        if ($this->exists($newPath) || ! $this->exists($path)) {
×
128
            return false;
×
129
        }
130

131
        $this->files[$newPath] = $this->files[$path];
×
132

133
        $this->delete($path);
×
134

135
        return true;
×
136
    }
137

138
    /**
139
     * @inheritDoc
140
     */
141
    public function copy(string $path, string $newPath): bool
142
    {
143
        if ($this->exists($newPath) || ! $this->exists($path)) {
×
144
            return false;
×
145
        }
146

147
        $this->files[$newPath] = $this->files[$path];
×
148

149
        return true;
×
150
    }
151

152
    /**
153
     * @inheritDoc
154
     */
155
    public function delete(string $path): bool
156
    {
157
        unset($this->files[$path]);
4✔
158

159
        return true;
4✔
160
    }
161

162
    /**
163
     * @inheritDoc
164
     */
165
    public function metadata(string $path): array|null
166
    {
167
        return $this->getMetadataInternal($path)?->toArray();
×
168
    }
169

170
    /**
171
     * @inheritDoc
172
     */
173
    public function mimetype(string $path): string|null
174
    {
175
        return $this->getMetadataInternal($path)->mimetype ?? null;
×
176
    }
177

178
    /**
179
     * @inheritDoc
180
     */
181
    public function size(string $path): int|null
182
    {
183
        return $this->getMetadataInternal($path)->size ?? null;
×
184
    }
185

186
    /**
187
     * @inheritDoc
188
     */
189
    public function timestamp(string $path): int|null
190
    {
191
        return $this->files[$path]->timestamp ?? null;
4✔
192
    }
193

194
    /**
195
     * @inheritDoc
196
     */
197
    public function visibility(string $path): string|null
198
    {
199
        return $this->getMetadataInternal($path)->visibility ?? null;
×
200
    }
201

202
    /**
203
     * @inheritDoc
204
     */
205
    public function setVisibility(string $path, Visibility $visibility): bool
206
    {
207
        if (! $this->exists($path)) {
×
208
            return false;
×
209
        }
210

211
        $this->files[$path]->metadata->visibility = $visibility->value;
×
212

213
        return true;
×
214
    }
215

216
    /**
217
     * @inheritDoc
218
     */
219
    public function setVisibilityPublic(string $path): bool
220
    {
221
        return $this->setVisibility($path, Visibility::PUBLIC);
×
222
    }
223

224
    /**
225
     * @inheritDoc
226
     */
227
    public function setVisibilityPrivate(string $path): bool
228
    {
229
        return $this->setVisibility($path, Visibility::PRIVATE);
×
230
    }
231

232
    /**
233
     * @inheritDoc
234
     */
235
    public function createDir(string $path): bool
236
    {
237
        $this->files[$path] = new InMemoryFile($path, timestamp: time());
×
238

239
        return true;
×
240
    }
241

242
    /**
243
     * @inheritDoc
244
     */
245
    public function deleteDir(string $path): bool
246
    {
247
        foreach ($this->files as $filePath => $file) {
4✔
248
            if (str_starts_with($filePath, $path)) {
×
249
                unset($this->files[$filePath]);
×
250
            }
251
        }
252

253
        return true;
4✔
254
    }
255

256
    /**
257
     * @inheritDoc
258
     */
259
    public function listContents(?string $directory = null, bool $recursive = false): array
260
    {
261
        $directory ??= '';
×
262

263
        $contents = [];
×
264

265
        foreach ($this->files as $filePath => $file) {
×
266
            if (str_starts_with($filePath, $directory)) {
×
267
                $contents[] = [
×
UNCOV
268
                    'path'     => $filePath,
×
UNCOV
269
                    'contents' => $file->contents,
×
UNCOV
270
                ];
×
271
            }
272
        }
273

UNCOV
274
        return $contents;
×
275
    }
276

277
    protected function getMetadataInternal(string $path): InMemoryMetadata|null
278
    {
UNCOV
279
        return $this->files[$path]->metadata ?? null;
×
280
    }
281
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc