Completed
Push — master ( 82ab0a...a0493e )
by Paweł
10:40
created

Directory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 47.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 55
ccs 10
cts 21
cp 0.4762
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A checkDirectory() 0 20 3
B removeDir() 0 21 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
                    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