Passed
Push — master ( bb8aaf...c7e527 )
by Vsevolods
03:11
created

Filesystem::createDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.125

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 4
cp 0.5
crap 1.125
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Venta\Filesystem;
4
5
use League\Flysystem\FilesystemInterface;
6
use League\Flysystem\UnreadableFileException;
7
use Venta\Contracts\Filesystem\Filesystem as FilesystemContract;
8
use Venta\Contracts\Filesystem\Metadata as MetadataContract;
9
10
/**
11
 * Class Filesystem
12
 *
13
 * @package Venta\Filesystem
14
 */
15
class Filesystem implements FilesystemContract
16
{
17
18
    /**
19
     * @var FilesystemInterface
20
     */
21
    private $flysystem;
22
23
    /**
24
     * Filesystem constructor.
25
     *
26
     * @param FilesystemInterface $flysystem
27
     */
28 6
    public function __construct(FilesystemInterface $flysystem)
29
    {
30 6
        $this->flysystem = $flysystem;
31 6
    }
32
33
    /**
34
     * @inheritDoc
35
     */
36 1
    public function append(string $path, string $contents, array $config = []): bool
37
    {
38 1
        if ($this->exists($path)) {
39 1
            $contents = $this->read($path) . $contents;
40
        }
41
42 1
        return $this->write($path, $contents);
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48 1
    public function copy(string $path, string $newPath): bool
49
    {
50 1
        return $this->flysystem->copy($path, $newPath);
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56 1
    public function createDir(string $dirname, array $config = []): bool
57
    {
58 1
        return $this->flysystem->createDir($dirname, $config);
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64 1
    public function delete(string $path): bool
65
    {
66 1
        return $this->flysystem->delete($path);
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72 1
    public function deleteDir(string $dirname): bool
73
    {
74 1
        return $this->flysystem->deleteDir($dirname);
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 2
    public function exists(string $path): bool
81
    {
82 2
        return $this->flysystem->has($path);
83
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88 1
    public function listAll(string $path = '.', bool $recursive = false): array
89
    {
90 1
        return array_map(
91
            function ($item) {
92 1
                return new Metadata($item);
93 1
            },
94 1
            $this->flysystem->listContents($path, $recursive)
95
        );
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101 1 View Code Duplication
    public function listDirectories(string $path = '.', bool $recursive = false): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103 1
        return array_map(
104
            function ($item) {
105 1
                return new Metadata($item);
106 1
            },
107
            array_filter(
108 1
                $this->flysystem->listContents($path, $recursive),
109
                function ($item) {
110 1
                    return $item['type'] === MetadataContract::TYPE_DIR;
111 1
                }
112
            )
113
        );
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119 1 View Code Duplication
    public function listFiles(string $path = '.', bool $recursive = false): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121 1
        return array_map(
122
            function ($item) {
123 1
                return new Metadata($item);
124 1
            },
125
            array_filter(
126 1
                $this->flysystem->listContents($path, $recursive),
127 1
                function ($item) {
128 1
                    return $item['type'] === MetadataContract::TYPE_FILE;
129 1
                }
130
            )
131
        );
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137 1
    public function metadata($path)
138
    {
139 1
        $metadata = $this->flysystem->getMetadata($path);
140 1
        if (is_array($metadata)) {
141 1
            return new Metadata($metadata);
142
        }
143
144
        return null;
145
    }
146
147
    /**
148
     * @inheritDoc
149
     */
150 1
    public function move(string $path, string $newPath): bool
151
    {
152 1
        return $this->flysystem->rename($path, $newPath);
153
    }
154
155
    /**
156
     * @inheritDoc
157
     */
158 1
    public function prepend(string $path, string $contents, array $config = []): bool
159
    {
160 1
        if ($this->exists($path)) {
161 1
            $contents .= $this->read($path);
162
        }
163
164 1
        return $this->write($path, $contents);
165
    }
166
167
    /**
168
     * @inheritDoc
169
     */
170 2
    public function read(string $path): string
171
    {
172 2
        $content = $this->flysystem->read($path);
173 2
        if ($content === false) {
174
            throw new UnreadableFileException(sprintf('Unable to read file "%s".', $path));
175
        }
176
177 2
        return $content;
178
    }
179
180
    /**
181
     * @inheritDoc
182
     */
183 2
    public function write(string $path, string $contents, array $config = []): bool
184
    {
185 2
        return $this->flysystem->put($path, $contents, $config);
186
    }
187
188
}