Folder::getFiles()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace IQParts\Content\Object;
4
5
use League\Flysystem\Directory;
6
7
/**
8
 * Class Folder
9
 * @package IQParts\Content\Object
10
 */
11
final class Folder extends Item
12
{
13
    /**
14
     * @var Directory
15
     */
16
    private $directory;
17
    /**
18
     * @var string
19
     */
20
    private $path;
21
    /**
22
     * @var string|null
23
     */
24
    private $parent;
25
    /**
26
     * @var string
27
     */
28
    private $basename;
29
    /**
30
     * @var array
31
     */
32
    private $dirs;
33
    /**
34
     * @var array
35
     */
36
    private $files;
37
38
    /**
39
     * Folder constructor.
40
     * @param Directory $directory
41
     */
42
    public function __construct(Directory $directory = null)
43
    {
44
        if ($directory) {
45
            $this->directory = $directory;
46
            $this->path = $directory->getPath();
47
            $filesystem = $directory->getFilesystem();
48
49
            $parentSep = strrpos($this->path, '/');
50
            if ($parentSep !== false) {
51
                $parent = substr($this->path, 0, $parentSep);
52
            } else $parent = '/';
53
            $this->parent = base64_encode($parent);
54
            $this->basename = basename($this->path);
55
56
            $paths = $filesystem->listContents($this->path);
57
            $dirs = [];
58
            $files = [];
59
60
            foreach ($paths as $path) {
61
                $item = $filesystem->get($path['path']);
62
                if ($item instanceof Directory) {
63
                    $dirs[] = base64_encode($item->getPath());
64
                } else {
65
                    $files[] = base64_encode($item->getPath());
66
                }
67
            }
68
            $this->dirs = $dirs;
69
            $this->files = $files;
70
        }
71
    }
72
73
    /**
74
     * @return Directory
75
     */
76
    public function getDirectory(): Directory
77
    {
78
        return $this->directory;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getId(): string
85
    {
86
        return base64_encode($this->getPath());
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getPath(): string
93
    {
94
        return $this->path;
95
    }
96
97
    /**
98
     * @return string|null
99
     */
100
    public function getParent()
101
    {
102
        return $this->parent;
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getBasename(): string
109
    {
110
        return $this->basename;
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function getDirs(): array
117
    {
118
        return $this->dirs;
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    public function getFiles(): array
125
    {
126
        return $this->files;
127
    }
128
129
    /**
130
     * @return array
131
     */
132
    public function toArray(): array
133
    {
134
        return [
135
            'id' => $this->getId(),
136
            'path' => $this->getPath(),
137
            'basename' => $this->getBasename(),
138
            'parent' => $this->getParent(),
139
            'dirs' => $this->getDirs(),
140
            'files' => $this->getFiles()
141
        ];
142
    }
143
144
    /**
145
     * @return bool
146
     */
147
    function isDir(): bool
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
148
    {
149
        return true;
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    function isFile(): bool
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
156
    {
157
        return false;
158
    }
159
160
    /**
161
     * @param string $id
162
     * @param string $path
163
     * @param string $basename
164
     * @param string|null $parent
165
     * @param array $dirs
166
     * @param array $files
167
     * @return Folder
168
     */
169
    public static function fromVariables(
170
        string $id,
171
        string $path,
172
        string $basename,
173
        $parent = null,
174
        array $dirs = [],
175
        array $files = []
176
    ): Folder
177
    {
178
        $dir = new self();
179
        $dir->id = $id;
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
180
        $dir->path = $path;
181
        $dir->basename = $basename;
182
        $dir->parent = $parent;
183
        $dir->dirs = $dirs;
184
        $dir->files = $files;
185
        return $dir;
186
    }
187
}