HackedProjectWebDownloader::removeDir()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 5
nop 1
dl 0
loc 19
rs 8.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Cerbere\Model\Hacked;
4
5
use Cerbere\Model\Project;
6
7
/**
8
 * Base class for downloading remote versions of projects.
9
 */
10
class HackedProjectWebDownloader
11
{
12
    /**
13
     * @var Project
14
     */
15
    protected $project;
16
17
    /**
18
     * Constructor, pass in the project this downloaded is expected to download.
19
     * @param Project $project
20
     */
21
    public function __construct(Project $project)
22
    {
23
        $this->project = $project;
24
    }
25
26
    /**
27
     * Returns a temp directory to work in.
28
     *
29
     * @param string $namespace
30
     *   The optional namespace of the temp directory, defaults to the classname.
31
     * @return string
32
     */
33
    protected function getTempDirectory($namespace = null)
34
    {
35
        if (null === $namespace) {
36
            $namespace = get_class($this);
37
        }
38
39
        $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'hacked-cache';
40
41
        if (!empty($namespace)) {
42
            $dir .= DIRECTORY_SEPARATOR . preg_replace('/[^0-9A-Z\-_]/i', '', $namespace);
43
        }
44
45
        // Build the destination folder tree if it doesn't already exists.
46
        if (!is_dir($dir)) {
47
            mkdir($dir, 0775, true);
48
        }
49
50
        return $dir;
51
    }
52
53
    /**
54
     * Returns a directory to save the downloaded project into.
55
     * @return string
56
     */
57
    protected function getDestination()
58
    {
59
        $type = $this->project->getProjectType();
60
        $name = $this->project->getProject();
61
        $version = $this->project->getVersion();
62
63
        $dir = $this->getTempDirectory() . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . $name;
64
65
        // Build the destination folder tree if it doesn't already exists.
66
        if (!is_dir($dir)) {
67
            mkdir($dir, 0775, true);
68
        }
69
70
        return $dir . DIRECTORY_SEPARATOR . $version;
71
    }
72
73
    /**
74
     * Returns the final destination of the unpacked project.
75
     * @return string
76
     */
77
    public function getFinalDestination()
78
    {
79
        $dir = $this->getDestination();
80
81
        return $dir;
82
    }
83
84
    /**
85
     * Download the remote files to the local filesystem.
86
     * @return bool
87
     */
88
    public function downloadFile()
89
    {
90
        return true;
91
    }
92
93
    /**
94
     * Recursively delete all files and folders in the specified filepath, then
95
     * delete the containing folder.
96
     *
97
     * Note that this only deletes visible files with write permission.
98
     *
99
     * @param string $path
100
     *   A filepath relative to file_directory_path.
101
     */
102
    protected function removeDir($path)
103
    {
104
        if (is_file($path) || is_link($path)) {
105
            unlink($path);
106
        } elseif (is_dir($path)) {
107
            $d = dir($path);
108
109
            while (($entry = $d->read()) !== false) {
110
                if ($entry == '.' || $entry == '..') {
111
                    continue;
112
                }
113
                $entry_path = $path . DIRECTORY_SEPARATOR . $entry;
114
                $this->removeDir($entry_path);
115
            }
116
117
            $d->close();
118
            rmdir($path);
119
        }
120
    }
121
}
122