InMemoryFileSystem::createDirRaw()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 3
nop 1
crap 1
1
<?php
2
3
namespace Hgraca\FileSystem;
4
5
use Hgraca\Helper\StringHelper;
6
7
class InMemoryFileSystem extends FileSystemAbstract
8
{
9
    const DIR_DISCRIMINATOR = 'dir';
10
    const LINK_DISCRIMINATOR = 'link';
11
    const FILE_DISCRIMINATOR = 'file';
12
13
    const KEY_TYPE = 'type';
14
    const KEY_CONTENT = 'content';
15
    const KEY_CREATION_TIME = 'creation_time';
16
17
    /** @var array */
18
    private $fileSystem = [];
19
20 30 View Code Duplication
    protected function dirExistsRaw(string $path): bool
21
    {
22 30
        return array_key_exists(
23
                $path,
24 30
                $this->fileSystem
25 30
            ) && $this->fileSystem[$path][self::KEY_TYPE] === self::DIR_DISCRIMINATOR;
26
    }
27
28 35 View Code Duplication
    protected function linkExistsRaw(string $path): bool
29
    {
30 35
        return array_key_exists(
31
                $path,
32 35
                $this->fileSystem
33 35
            ) && $this->fileSystem[$path][self::KEY_TYPE] === self::LINK_DISCRIMINATOR;
34
    }
35
36 38 View Code Duplication
    protected function fileExistsRaw(string $path): bool
37
    {
38 38
        return array_key_exists(
39
                $path,
40 38
                $this->fileSystem
41 38
            ) && $this->fileSystem[$path][self::KEY_TYPE] === self::FILE_DISCRIMINATOR;
42
    }
43
44 1
    protected function getFileCreationTimestampRaw(string $path): int
45
    {
46 1
        return $this->fileSystem[$path][self::KEY_CREATION_TIME];
47
    }
48
49 10
    protected function readFileRaw(string $path): string
50
    {
51 10
        return $this->fileSystem[$path][self::KEY_CONTENT];
52
    }
53
54 7
    protected function writeFileRaw(string $path, string $content)
55
    {
56 7
        $this->fileSystem[$path] = [
57 7
            self::KEY_TYPE => self::FILE_DISCRIMINATOR,
58 7
            self::KEY_CONTENT => $content,
59 7
            self::KEY_CREATION_TIME => time(),
60
        ];
61 7
    }
62
63 4
    protected function copyFileRaw(string $sourcePath, string $destinationPath)
64
    {
65 4
        $this->writeFileRaw($destinationPath, $this->readFileRaw($sourcePath));
66 4
    }
67
68 2
    protected function deleteFileRaw(string $path)
69
    {
70 2
        unset($this->fileSystem[$path]);
71 2
    }
72
73 3
    protected function createLinkRaw(string $path, string $targetPath)
74
    {
75 3
        $this->fileSystem[$path] = [
76 3
            self::KEY_TYPE => self::LINK_DISCRIMINATOR,
77 3
            self::KEY_CONTENT => $targetPath,
78
        ];
79 3
    }
80
81 7
    protected function getLinkTargetRaw(string $path): string
82
    {
83 7
        return $this->fileSystem[$path][self::KEY_CONTENT];
84
    }
85
86 5
    protected function createDirRaw(string $path)
87
    {
88 5
        $this->fileSystem[$path] = [
89 5
            self::KEY_TYPE => self::DIR_DISCRIMINATOR,
90
        ];
91 5
    }
92
93 3
    protected function deleteDirRaw(string $path)
94
    {
95 3
        foreach ($this->fileSystem as $existingPath => $content) {
96 3
            if (StringHelper::hasBeginning($path, $existingPath)) {
97 3
                unset($this->fileSystem[$existingPath]);
98
            }
99
        }
100 3
    }
101
102
    /**
103
     * @return string[] The array with the file and dir names
104
     */
105 5
    protected function readDirRaw(string $path): array
106
    {
107 5
        $dirContent = [];
108 5
        foreach ($this->fileSystem as $existingPath => $content) {
109 5
            if (StringHelper::hasBeginning($path, $existingPath)) {
110 5
                $dirContent[] = current(
111 5
                    explode(DIRECTORY_SEPARATOR, StringHelper::removeFromBeginning($path, $existingPath), 2)
112
                );
113
            }
114
        }
115
116 5
        return array_values(
117
            array_filter(
118
                array_unique($dirContent),
119 5
                function ($v) {
120 5
                    return $v !== '';
121 5
                },
122 5
                ARRAY_FILTER_USE_BOTH
123
            )
124
        );
125
    }
126
}
127