DMSFilesystemTestHelper::delete()   B
last analyzed

Complexity

Conditions 6
Paths 5

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.9297
c 0
b 0
f 0
cc 6
nc 5
nop 1
1
<?php
2
3
class DMSFilesystemTestHelper
4
{
5
    /**
6
     * Files that are added to the DMS asset directory by the DMS instance. They will be removed after running tests.
7
     *
8
     * @var array
9
     */
10
    protected static $dmsFiles = array('.htaccess', 'web.config');
11
12
    /**
13
     * Deletes a directory and all files within it, or a file. Will automatically prepend the base path.
14
     *
15
     * This only work while a unit test is running for safety reasons.
16
     *
17
     * @param string $path
18
     */
19
    public static function delete($path)
20
    {
21
        if (!SapphireTest::is_running_test() || !file_exists($path)) {
22
            return false;
23
        }
24
25
        $path = BASE_PATH . DIRECTORY_SEPARATOR . $path;
26
        if (is_dir($path)) {
27
            $files = new RecursiveIteratorIterator(
28
                new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS),
29
                RecursiveIteratorIterator::CHILD_FIRST
30
            );
31
32
            foreach ($files as $fileinfo) {
33
                $action = $fileinfo->isDir() ? 'rmdir' : 'unlink';
34
                $action($fileinfo->getRealPath());
35
            }
36
37
            rmdir($path);
38
        } else {
39
            unlink($path);
40
        }
41
    }
42
}
43