Directory::delete()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
/**
4
 * Platine Filesystem
5
 *
6
 * Platine Filesystem is file system abstraction layer extendable by adapters
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Filesystem
11
 * Copyright (c) 2019 Alex Sivka
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file Directory.php
34
 *
35
 *  The local directory class
36
 *
37
 *  @package    Platine\Filesystem\Adapter\Local
38
 *  @author Platine Developers Team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   https://www.platine-php.com
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Filesystem\Adapter\Local;
49
50
use InvalidArgumentException;
51
use Platine\Filesystem\Adapter\Local\Exception\NotFoundException;
52
use Platine\Filesystem\DirectoryInterface;
53
use Platine\Filesystem\FileInterface;
54
55
/**
56
 * @class Directory
57
 * @package Platine\Filesystem\Adapter\Local
58
 */
59
class Directory extends AbstractLocal implements DirectoryInterface
60
{
61
    /**
62
    * {@inheritdoc}
63
    */
64
    public function copyTo(
65
        string|DirectoryInterface $directory,
66
        int $mode = 0775
67
    ): FileInterface|DirectoryInterface {
68
        if (!$this->exists()) {
69
            throw new NotFoundException(sprintf(
70
                'Source path [%s] not found',
71
                $this->path
72
            ));
73
        }
74
75
        if (is_string($directory)) {
76
            $directory = $this->adapter->directory($directory);
77
        }
78
79
        if ($directory->getPath() === $this->getPath()) {
80
            throw new InvalidArgumentException(sprintf(
81
                'Source and destination path [%s] can not be same',
82
                $this->path
83
            ));
84
        }
85
86
        $destination = $directory->create($this->getName(), $mode);
87
        foreach ($this->read() as $item) {
88
            if ($item->isDir() && $item->getPath() === $directory->getPath()) {
89
                // not allow copying into self child directory
90
                continue;
91
            }
92
            $item->copyTo($destination);
93
        }
94
95
        return $destination;
96
    }
97
98
    /**
99
    * {@inheritdoc}
100
    */
101
    public function create(string $name, int $mode = 0775, bool $recursive = false): DirectoryInterface
102
    {
103
        if (!file_exists($this->path . DIRECTORY_SEPARATOR . $name)) {
104
            mkdir($this->path . DIRECTORY_SEPARATOR . $name, $mode, $recursive);
105
        } else {
106
            chmod($this->path . DIRECTORY_SEPARATOR . $name, $mode);
107
        }
108
109
        return $this->adapter->directory(
110
            $this->originalPath . DIRECTORY_SEPARATOR . $name
111
        );
112
    }
113
114
    /**
115
    * {@inheritdoc}
116
    */
117
    public function createFile(
118
        string $name,
119
        string $content = '',
120
        int $mode = 0775
121
    ): FileInterface {
122
        $file = new File('', $this->adapter);
123
        return $file->create(
124
            $this->originalPath . DIRECTORY_SEPARATOR . $name,
125
            $content,
126
            $mode
127
        );
128
    }
129
130
    /**
131
    * {@inheritdoc}
132
    */
133
    public function delete(): self
134
    {
135
        if (!$this->exists()) {
136
            return $this;
137
        }
138
139
        foreach ($this->read() as $item) {
140
            $item->delete();
141
        }
142
        rmdir($this->path);
143
144
        return $this;
145
    }
146
147
    /**
148
    * {@inheritdoc}
149
    */
150
    public function getType(): string
151
    {
152
        return 'dir';
153
    }
154
155
    /**
156
    * {@inheritdoc}
157
    */
158
    public function read(int $type = self::ALL): array
159
    {
160
        $files = [
161
            'dir' => [],
162
            'file' => [],
163
        ];
164
165
        $items = scandir($this->path);
166
        if ($items === false) {
167
            return [];
168
        }
169
170
        foreach ($items as $item) {
171
            if ($item === '.' || $item === '..') {
172
                continue;
173
            }
174
175
            $info = $this->adapter->get(
176
                $this->originalPath . DIRECTORY_SEPARATOR . $item
177
            );
178
            if ($info !== null) {
179
                $files[$info->getType()][] = $info;
180
            }
181
        }
182
183
        switch ($type) {
184
            case self::ALL:
185
                return array_merge($files['dir'], $files['file']);
186
            case self::FILE:
187
                return $files['file'];
188
            case self::DIR:
189
                return $files['dir'];
190
            default:
191
                throw new InvalidArgumentException(sprintf(
192
                    'Invalid filter value [%d] must be one of [%s]',
193
                    $type,
194
                    implode(', ', [self::ALL, self::FILE, self::DIR])
195
                ));
196
        }
197
    }
198
199
    /**
200
    * {@inheritdoc}
201
    */
202
    public function scan(): array
203
    {
204
        $list = [];
205
        $items = scandir($this->path);
206
        if ($items !== false) {
207
            foreach ($items as $item) {
208
                if ($item !== '.' && $item !== '..') {
209
                    $list[] = $item;
210
                }
211
            }
212
        }
213
214
        return $list;
215
    }
216
}
217