Completed
Push — master ( 3fdc21...2574b5 )
by ReliQ
01:51
created

Filesystem   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 36
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A deleteDirectory() 0 27 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Docweaver\Services;
6
7
use Illuminate\Filesystem\Filesystem as IlluminateFilesystem;
8
use ReliqArts\Docweaver\Contracts\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