Completed
Push — master ( 82ab0a...209992 )
by Paweł
09:02 queued 06:16
created

Directory::removeDir()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 11
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 21
ccs 0
cts 11
cp 0
crap 56
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Helpers;
15
16
use Exception;
17
18
/**
19
 * Class Directory.
20
 *
21
 * @package Wszetko\Sitemap\Helpers
22
 */
23
class Directory
24
{
25
    /**
26
     * @param string $directory
27
     *
28
     * @return string
29
     *
30
     * @throws \Exception
31
     */
32 10
    public static function checkDirectory(string $directory): string
33
    {
34 10
        $dir = realpath($directory);
35
36 10
        if (false === $dir) {
37 10
            mkdir(
38 10
                $directory,
39 10
                0777,
40 10
                true
41
            );
42 10
            $dir = realpath($directory);
43
        }
44
45 10
        if (false === $dir) {
46
            // @codeCoverageIgnoreStart
47
            throw new Exception("Can't get directory $directory.");
48
            // @codeCoverageIgnoreEnd
49
        }
50
51 10
        return $dir;
52
    }
53
54
    /**
55
     * @param string $dir
56
     */
57
    public static function removeDir(string $dir): void
58
    {
59
        if (is_dir($dir)) {
60
            return;
61
        }
62
63
        $objects = scandir($dir);
64
65
        if (false !== $objects) {
66
            foreach ($objects as $object) {
67
                if ('.' != $object && '..' != $object) {
68
                    if ('dir' == filetype($dir . '/' . $object)) {
69
                        self::removeDir($dir . '/' . $object);
70
                    } else {
71
                        unlink($dir . '/' . $object);
72
                    }
73
                }
74
            }
75
        }
76
77
        rmdir($dir);
78
    }
79
}
80