Completed
Push — master ( 91746d...c63af8 )
by ReliQ
09:16
created

Filesystem::deleteDirectory()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.1768
c 0
b 0
f 0
cc 5
nc 7
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Docweaver\Service;
6
7
use Illuminate\Filesystem\Filesystem as IlluminateFilesystem;
8
use ReliqArts\Docweaver\Contract\Filesystem as FilesystemContract;
9
10
final class Filesystem extends IlluminateFilesystem implements FilesystemContract
11
{
12
    /**
13
     * @param string $directory
14
     * @param bool   $preserve
15
     *
16
     * @return bool
17
     */
18
    public function deleteDirectory($directory, $preserve = false)
19
    {
20
        if (!$this->isDirectory($directory)) {
21
            return false;
22
        }
23
24
        /**
25
         * @var string Pattern for finding all contained files/directories
26
         *             inclusive of hidden directories such as `.git`.
27
         *
28
         * @see https://stackoverflow.com/a/49031383/3466460 Pattern Source
29
         */
30
        $pattern = sprintf('%s/{*,.[!.]*,..?*}', $directory);
31
        $files = glob($pattern, GLOB_BRACE);
32
33
        foreach ($files as $file) {
34
            if (is_dir($file)) {
35
                $this->deleteDirectory($file);
36
37
                continue;
38
            }
39
            chmod($file, 0777);
40
            unlink($file);
41
        }
42
43
        return $preserve ?: rmdir($directory);
44
    }
45
}
46