HackedProjectWebFilesDownloader::getFile()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 5
nop 1
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Cerbere\Model\Hacked;
4
5
use splitbrain\PHPArchive\Archive;
6
use splitbrain\PHPArchive\Tar;
7
8
/**
9
 * Downloads a project using a standard Drupal method.
10
 */
11
class HackedProjectWebFilesDownloader extends HackedProjectWebDownloader
12
{
13
    /**
14
     * @return string|false
15
     */
16
    public function getDownloadLink()
17
    {
18
        $version = $this->project->getVersion();
19
20
        // Remove 'dev' tailing flag from version name.
21
        if (preg_match('/(\+\d+\-dev)$/', $version)) {
22
            $version = preg_replace('/(\+\d+\-dev)$/', '', $version);
23
        }
24
25
        if ($release = $this->project->getRelease($version)) {
26
            return $release->getDownloadLink();
27
        }
28
29
        return false;
30
    }
31
32
    /**
33
     * @return string|boolean
34
     */
35
    public function downloadFile()
36
    {
37
        $destination = $this->getDestination();
38
39
        if (!($release_url = $this->getDownloadLink())) {
40
            return false;
41
        }
42
43
        // If our directory already exists, we can just return the path to this cached version
44
        $whiteList = array('.', '..', 'CVS', '.svn', '.git');
45
        if (file_exists($destination) && count(HackedFileGroup::scanDirectory($destination, '/.*/', $whiteList))
46
        ) {
47
            return $destination;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $destination; (string) is incompatible with the return type of the parent method Cerbere\Model\Hacked\Hac...ownloader::downloadFile of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
48
        }
49
50
        // Build the destination folder tree if it doesn't already exists.
51
        if (!is_dir($destination)) {
52
            mkdir($destination, 0775, true);
53
        }
54
55
        if (!($local_file = $this->getFile($release_url))) {
56
            return false;
57
        }
58
59
        try {
60
            $this->extractArchive($local_file, $destination);
61
        } catch (\Exception $e) {
62
            echo $e->getMessage() . "\n";
63
64
            return false;
65
        }
66
67
        return true;
68
    }
69
70
    /**
71
     * Copies a file from $url to the temporary directory for updates.
72
     *
73
     * If the file has already been downloaded, returns the the local path.
74
     *
75
     * @param string $url
76
     *   The URL of the file on the server.
77
     *
78
     * @return string
79
     *   Path to local file.
80
     */
81
    protected function getFile($url)
82
    {
83
        $parsed_url = parse_url($url);
84
        $remote_schemes = array('http', 'https', 'ftp', 'ftps', 'smb', 'nfs');
85
86
        if (!in_array($parsed_url['scheme'], $remote_schemes)) {
87
            // This is a local file, just return the path.
88
            return realpath($url);
89
        }
90
91
        // Todo: use Symfony's cache objects
92
        // Check the cache and download the file if needed.
93
        $cache_directory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'hacked-cache';
94
        $local = $cache_directory . DIRECTORY_SEPARATOR . basename($parsed_url['path']);
95
96
        if (!file_exists($cache_directory)) {
97
            @mkdir($cache_directory, 0775, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
98
        }
99
100
        // Todo: use guzzle.
101
        $content = file_get_contents($url);
102
103
        if ($content !== false && file_put_contents($local, $content)) {
104
            return $local;
105
        }
106
107
        return false;
108
    }
109
110
    /**
111
     * Unpack a downloaded archive file.
112
     *
113
     * @param string $file
114
     *   The filename of the archive you wish to extract.
115
     * @param string $directory
116
     *   The directory you wish to extract the archive into.
117
     * @return Archive
118
     *   The Archiver object used to extract the archive.
119
     * @throws \Exception on failure.
120
     */
121
    protected function extractArchive($file, $directory)
122
    {
123
        $archive = new Tar();
124
125
        // Remove the directory if it exists, otherwise it might contain a mixture of
126
        // old files mixed with the new files (e.g. in cases where files were removed
127
        // from a later release).
128
        $archive->open($file);
129
        $files = $archive->contents();
130
131
        // First entry contains the root folder.
132
        $project_path = $files[0]->getPath();
133
134
        if (file_exists($directory)) {
135
            $this->removeDir($directory);
136
        }
137
138
        // Reopen archive to extract all files.
139
        $archive->open($file);
140
        // Strip first folder level.
141
        $archive->extract($directory, $project_path);
142
143
        return $archive;
144
    }
145
}
146