Filesystem::getMediaFiles()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
nc 9
nop 0
dl 0
loc 20
ccs 14
cts 14
cp 1
crap 5
rs 9.4888
c 1
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\Loaders;
4
5
use Nip\Filesystem\File;
6
7
/**
8
 * Class Filesystem.
9
 */
10
class Filesystem extends AbstractLoader
11
{
12
    /**
13
     * @return File[]
14
     */
15 7
    public function getMediaFiles()
16
    {
17 7
        $path = $this->getBasePath();
18 7
        $contents = $this->getFilesystem()->listContents($path);
19 7
        $directories = [];
20 7
        $files = [];
21 7
        foreach ($contents as $object) {
22 5
            if ($object['type'] == 'dir') {
23 1
                $directories[] = $object;
24
            } else {
25 4
                $files[] = $object;
26
            }
27
        }
28 7
        if (count($directories) > 0) {
29 1
            return $this->scanDirectoryContents($directories[0]['path']);
30 6
        } elseif (count($files)) {
31 4
            return $this->hydrateFileContents($files);
32
        }
33
34 2
        return [];
35
    }
36
37
    /**
38
     * @param $path
39
     *
40
     * @return File[]
41
     */
42 1
    protected function scanDirectoryContents($path)
43
    {
44 1
        $contents = $this->getFilesystem()->listContents($path);
45
46 1
        return $this->hydrateFileContents($contents);
47
    }
48
49
    /**
50
     * @param $contents
51
     *
52
     * @return File[]
53
     */
54 5
    protected function hydrateFileContents($contents)
55
    {
56 5
        $files = [];
57 5
        foreach ($contents as $object) {
58 5
            if ($object['type'] == 'file') {
59 5
                $files[] = new File($this->getFilesystem(), $object['path']);
60
            }
61
        }
62
63 5
        return $files;
64
    }
65
}
66