Completed
Push — master ( 209992...5259ee )
by Paweł
02:43
created

Directory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 45.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 57
ccs 10
cts 22
cp 0.4545
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkDirectory() 0 20 3
B removeDir() 0 23 7
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
                    continue;
69
                }
70
71
                if (is_dir($dir . '/' . $object)) {
72
                    self::removeDir($dir . '/' . $object);
73
                } else {
74
                    unlink($dir . '/' . $object);
75
                }
76
            }
77
        }
78
79
        rmdir($dir);
80
    }
81
}
82