FileSystem::scanFolder()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 5
cts 5
cp 1
rs 9.7
c 0
b 0
f 0
cc 4
nc 2
nop 1
crap 4
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Application\FileSystem;
4
5
/**
6
 * Copyright 2015-2020 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use DirectoryIterator;
22
use Limoncello\Application\Exceptions\FileSystemException;
23
use Limoncello\Contracts\FileSystem\FileSystemInterface;
24
use function call_user_func;
25
use function file_exists;
26
use function file_get_contents;
27
use function file_put_contents;
28
use function is_dir;
29
use function is_writable;
30
use function iterator_to_array;
31
use function mkdir;
32
use function rmdir;
33 5
use function symlink;
34
use function unlink;
35 5
36
/**
37
 * @package Limoncello\Application
38
 *
39
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
40
 */
41 3
class FileSystem implements FileSystemInterface
42
{
43 3
    /**
44 3
     * @inheritdoc
45
     */
46 3
    public function exists(string $path): bool
47
    {
48
        return file_exists($path);
49
    }
50
51
    /**
52 2
     * @inheritdoc
53
     */
54 2
    public function read(string $filePath): string
55 2
    {
56
        $content = file_get_contents($filePath);
57
        $content !== false ?: $this->throwEx(new FileSystemException());
58
59
        return $content;
60
    }
61 5
62
    /**
63 5
     * @inheritdoc
64 5
     */
65
    public function write(string $filePath, string $contents): void
66
    {
67
        $bytesWritten = file_put_contents($filePath, $contents);
68
        $bytesWritten !== false ?: $this->throwEx(new FileSystemException());
69
    }
70 2
71
    /**
72 2
     * @inheritdoc
73
     */
74
    public function delete(string $filePath): void
75 2
    {
76
        $isDeleted = file_exists($filePath) === false || unlink($filePath) === true;
77 2
        $isDeleted === true ?: $this->throwEx(new FileSystemException());
78 2
    }
79
80
    /**
81 2
     * @inheritdoc
82
     */
83 2
    public function scanFolder(string $folderPath): array
84
    {
85 2
        is_dir($folderPath) === true ?: $this->throwEx(new FileSystemException());
86
87
        $iterator = call_user_func(function () use ($folderPath) {
88
            foreach (new DirectoryIterator($folderPath) as $directoryIterator) {
89
                /** @var DirectoryIterator $directoryIterator */
90
                if ($directoryIterator->isDot() === false) {
91 3
                    yield $directoryIterator->getFilename() => $directoryIterator->getRealPath();
92
                }
93 3
            }
94
        });
95
96
        $result = iterator_to_array($iterator);
97
98
        return $result;
99 1
    }
100
101 1
    /**
102
     * @inheritdoc
103
     */
104
    public function isFolder(string $path): bool
105
    {
106
        return is_dir($path);
107 1
    }
108
109 1
    /**
110 1
     * @inheritdoc
111
     */
112
    public function isWritable(string $path): bool
113
    {
114
        return is_writable($path);
115
    }
116 3
117
    /**
118 3
     * @inheritdoc
119 3
     */
120
    public function createFolder(string $folderPath): void
121
    {
122
        $isCreated = mkdir($folderPath);
123
        $isCreated === true ?: $this->throwEx(new FileSystemException());
124
    }
125 3
126
    /**
127 3
     * @inheritdoc
128 3
     */
129
    public function deleteFolder(string $folderPath): void
130
    {
131 3
        $isDeleted = is_dir($folderPath) === true && rmdir($folderPath) === true;
132
        $isDeleted === true ?: $this->throwEx(new FileSystemException());
133
    }
134
135
    /**
136
     * @inheritdoc
137 1
     */
138
    public function deleteFolderRecursive(string $folderPath): void
139 1
    {
140 1
        foreach ($this->scanFolder($folderPath) as $path) {
141
            $this->isFolder($path) === true ? $this->deleteFolderRecursive($path) : $this->delete($path);
142
        }
143
144
        $this->deleteFolder($folderPath);
145
    }
146 2
147
    /**
148
     * @inheritdoc
149 2
     */
150
    public function symlink(string $targetPath, string $linkPath): void
151 2
    {
152
        $isCreated = symlink($targetPath, $linkPath) === true;
153
        $isCreated === true ?: $this->throwEx(new FileSystemException());
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159 2
    public function requireFile(string $path)
160
    {
161 2
        /** @noinspection PhpIncludeInspection */
162
        $result = require $path;
163
164
        return $result;
165
    }
166
167
    /**
168
     * @param FileSystemException $exception
169
     *
170
     * @return void
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
171
     */
172
    protected function throwEx(FileSystemException $exception): void
173
    {
174
        throw $exception;
175
    }
176
}
177