Directory::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace FlyCrud;
5
6
use League\Flysystem\FilesystemInterface;
7
use League\Flysystem\Filesystem;
8
use League\Flysystem\Adapter\Local;
9
use ArrayAccess;
10
use RuntimeException;
11
12
class Directory implements ArrayAccess
13
{
14
    protected $path;
15
    protected $format;
16
    protected $filesystem;
17
    protected $documents = [];
18
    protected $directories = [];
19
20
    /**
21
     * Creates a new directory instance.
22
     */
23
    public static function make(string $path, FormatInterface $format): Directory
24
    {
25
        return new static(new Filesystem(new Local($path)), '', $format);
26
    }
27
28
    public function __construct(FilesystemInterface $filesystem, string $path, FormatInterface $format)
29
    {
30
        $this->filesystem = $filesystem;
31
        $this->format = $format;
32
        $this->path = $path;
33
    }
34
35
    /**
36
     * Read and return a document.
37
     */
38
    public function getDocument(string $id): Document
39
    {
40
        if (isset($this->documents[$id])) {
41
            return $this->documents[$id];
42
        }
43
44
        if ($this->hasDocument($id)) {
45
            $path = $this->getDocumentPath($id);
46
            $source = $this->filesystem->read($path);
47
48
            if (is_string($source)) {
49
                return $this->documents[$id] = new Document($this->format->parse($source));
50
            }
51
52
            throw new RuntimeException(sprintf('Format error in the file "%s"', $path));
53
        }
54
55
        throw new RuntimeException(sprintf('File "%s" not found', $id));
56
    }
57
58
    /**
59
     * Read and return a directory.
60
     */
61
    public function getDirectory(string $id): Directory
62
    {
63
        if (isset($this->directories[$id])) {
64
            return $this->directories[$id];
65
        }
66
67
        if ($this->hasDirectory($id)) {
68
            return $this->directories[$id] = new static($this->filesystem, $this->getDirectoryPath($id), $this->format);
69
        }
70
71
        throw new RuntimeException(sprintf('Directory "%s" not found', $id));
72
    }
73
74
    /**
75
     * Check whether a document exists.
76
     */
77 View Code Duplication
    public function hasDocument(string $id): bool
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...
78
    {
79
        if (isset($this->documents[$id])) {
80
            return true;
81
        }
82
83
        $path = $this->getDocumentPath($id);
84
85
        if ($this->filesystem->has($path)) {
86
            $info = $this->filesystem->getMetadata($path);
87
88
            return $info['type'] === 'file';
89
        }
90
91
        return false;
92
    }
93
94
    /**
95
     * Check whether a document or directory exists.
96
     */
97 View Code Duplication
    public function hasDirectory(string $id): bool
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...
98
    {
99
        if (isset($this->directories[$id])) {
100
            return true;
101
        }
102
103
        $path = $this->getDirectoryPath($id);
104
105
        if ($this->filesystem->has($path)) {
106
            $info = $this->filesystem->getMetadata($path);
107
108
            return $info['type'] === 'dir';
109
        }
110
111
        return false;
112
    }
113
114
    /**
115
     * Saves a document.
116
     */
117
    public function saveDocument(string $id, Document $document): self
118
    {
119
        $this->documents[$id] = $document;
120
        $this->filesystem->put($this->getDocumentPath($id), $this->format->stringify($document->getArrayCopy()));
121
122
        return $this;
123
    }
124
125
    /**
126
     * Creates a new directory.
127
     */
128
    public function createDirectory(string $id): Directory
129
    {
130
        $path = $this->getDirectoryPath($id);
131
        $this->filesystem->createDir($path);
132
133
        return $this->directories[$id] = new static($this->filesystem, $path, $this->format);
134
    }
135
136
    /**
137
     * Deletes a document.
138
     */
139
    public function deleteDocument(string $id): self
140
    {
141
        $this->filesystem->delete($this->getDocumentPath($id));
142
        unset($this->documents[$id]);
143
144
        return $this;
145
    }
146
147
    /**
148
     * Deletes a directory.
149
     */
150
    public function deleteDirectory(string $id): self
151
    {
152
        $this->filesystem->deleteDir($this->getDirectoryPath($id));
153
        unset($this->directories[$id]);
154
155
        return $this;
156
    }
157
158
    /**
159
     * Returns all documents.
160
     */
161 View Code Duplication
    public function getAllDocuments(): 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...
162
    {
163
        $documents = [];
164
165
        foreach ($this->filesystem->listContents('/'.$this->path) as $info) {
166
            $id = $info['filename'];
167
168
            if ($this->hasDocument($id)) {
169
                $documents[$id] = $this->getDocument($id);
170
            }
171
        }
172
173
        return $documents;
174
    }
175
176
    /**
177
     * Returns all directories.
178
     */
179 View Code Duplication
    public function getAllDirectories(): 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...
180
    {
181
        $directories = [];
182
183
        foreach ($this->filesystem->listContents('/'.$this->path) as $info) {
184
            $id = $info['filename'];
185
186
            if ($this->hasDirectory($id)) {
187
                $directories[$id] = $this->getDirectory($id);
188
            }
189
        }
190
191
        return $directories;
192
    }
193
194
    /**
195
     * Returns a file path.
196
     */
197
    private function getDocumentPath(string $id): string
198
    {
199
        return $this->getDirectoryPath($id).'.'.$this->format->getExtension();
200
    }
201
202
    /**
203
     * Returns a directory path.
204
     */
205
    private function getDirectoryPath(string $id): string
206
    {
207
        if ($this->path === '') {
208
            return "/{$id}";
209
        }
210
211
        return "/{$this->path}/{$id}";
212
    }
213
214
    /**
215
     * ArrayAccess used to documents.
216
     *
217
     * @param string $id
218
     *
219
     * @return bool
220
     */
221
    public function offsetExists($id)
222
    {
223
        return $this->hasDocument($id);
224
    }
225
226
    /**
227
     * ArrayAccess used to documents.
228
     *
229
     * @param string $id
230
     *
231
     * @return Document
232
     */
233
    public function offsetGet($id)
234
    {
235
        return $this->getDocument($id);
236
    }
237
238
    /**
239
     * ArrayAccess used to documents.
240
     *
241
     * @param string   $id
242
     * @param Document $document
243
     */
244
    public function offsetSet($id, $document)
245
    {
246
        $this->saveDocument($id, $document);
247
    }
248
249
    /**
250
     * ArrayAccess used to documents.
251
     *
252
     * @param string $id
253
     */
254
    public function offsetUnset($id)
255
    {
256
        $this->deleteDocument($id);
257
    }
258
259
    /**
260
     * Property magic method used to directories.
261
     */
262
    public function __get(string $id): Directory
263
    {
264
        return $this->getDirectory($id);
265
    }
266
267
    /**
268
     * Property magic method used to directories.
269
     */
270
    public function __isset(string $id): bool
271
    {
272
        return $this->hasDirectory($id);
273
    }
274
275
    /**
276
     * Property magic method used to directories.
277
     */
278
    public function __unset(string $id)
279
    {
280
        $this->deleteDirectory($id);
281
    }
282
}
283