Completed
Pull Request — master (#217)
by Victor
02:11
created

DownloadController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 21
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 116
ccs 55
cts 60
cp 0.9167
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A checkFeed() 0 14 3
D downloadOwncloud() 0 36 10
A checkIntegrity() 0 9 3
A getFeed() 0 6 3
A getDefaultResponse() 0 8 1
1
<?php
2
/**
3
 * @author Victor Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2015, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace Owncloud\Updater\Controller;
23
24
use Owncloud\Updater\Utils\Fetcher;
25
use Owncloud\Updater\Utils\Registry;
26
use Owncloud\Updater\Utils\FilesystemHelper;
27
28
class DownloadController {
29
	
30
	/**
31
	 * @var Fetcher 
32
	 */
33
	protected $fetcher;
34
35
	/**
36
	 * @var Registry
37
	 */
38
	protected $registry;
39
40
	/**
41
	 * @var FilesystemHelper
42
	 */
43
	protected $fsHelper;
44
45 4
	public function __construct(Fetcher $fetcher, Registry $registry, FilesystemHelper $fsHelper){
46 4
		$this->fetcher = $fetcher;
47 4
		$this->registry = $registry;
48 4
		$this->fsHelper = $fsHelper;
49 4
	}
50
51 2
	public function checkFeed(){
52 2
		$response = $this->getDefaultResponse();
53
		try {
54 2
			$feed = $this->fetcher->getFeed();
55 1
			if ($feed->isValid()){
56 1
				$response['success'] = true;
57 1
				$response['data']['feed'] = $feed;
58 1
			}
59 2
		} catch (\Exception $e){
60 1
			$response['exception'] = $e;
61
		}
62
63 2
		return $response;
64
	}
65
66 2
	public function downloadOwncloud($progressCallback = null){
67 2
		$response = $this->getDefaultResponse();
68 2
		if (is_null($progressCallback)){
69
			$progressCallback = function (){};
70 2
		}
71
		try {
72 2
			$feed = $this->getFeed();
73
			// Fixme: Daily channel has no checksum
74 2
			$isDailyChannel = $this->fetcher->getUpdateChannel() == 'daily';
75 2
			if (!$isDailyChannel){
76 2
				$md5 = $this->fetcher->getMd5($feed);
77
78 2
			}
79 2
			$path = $this->fetcher->getBaseDownloadPath($feed);
80
81 2
			if ($isDailyChannel){
82
				$this->fsHelper->removeIfExists($path);
83
			}
84 2
			if ($isDailyChannel || !$this->checkIntegrity($path, $md5)){
0 ignored issues
show
Bug introduced by
The variable $md5 does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
85 1
				$this->fetcher->getOwncloud($feed, $progressCallback);
86
			}
87
88 1
			if ($isDailyChannel || $this->checkIntegrity($path, $md5)){
89 1
				$response['success'] = true;
90 1
				$response['data']['path'] = $path;
91 1
			} else {
92
				$response['exception'] = new \Exception('Deleted ' . $feed->getDownloadedFileName() . ' due to wrong checksum');
93
			}
94 2
		} catch (\Exception $e) {
95 1
			if (isset($path)){
96 1
				$this->fsHelper->removeIfExists($path);
97 1
			}
98 1
			$response['exception'] = $e;
99
		}
100 2
		return $response;
101
	}
102
103
	/**
104
	 * Check if package is not corrupted on download
105
	 * @param string $path
106
	 * @param string $md5
107
	 * @return boolean
108
	 */
109 2
	protected function checkIntegrity($path, $md5){
110 2
			$fileExists = $this->fsHelper->fileExists($path);
111 2
			$checksumMatch = $md5 === $this->fsHelper->md5File($path);
112 2
			if ($fileExists && !$checksumMatch){
113 1
				$this->fsHelper->removeIfExists($path);
114 1
				$fileExists = false;
115 1
			}
116 2
			return $fileExists;
117
	}
118
119
	/**
120
	 * Get a Feed instance
121
	 * @param bool $useCache
122
	 * @return Owncloud\Updater\Utils\Feed
123
	 */
124 2
	protected function getFeed($useCache = true){
125 2
		if ($useCache && !is_null($this->registry->get('feed'))){
126 2
			return $this->registry->get('feed');
127
		}
128
		return $this->fetcher->getFeed();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->fetcher->getFeed(); (Owncloud\Updater\Utils\Feed) is incompatible with the return type documented by Owncloud\Updater\Control...loadController::getFeed of type Owncloud\Updater\Control...loud\Updater\Utils\Feed.

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...
129
	}
130
131
	/**
132
	 * Init response array
133
	 * @return array
134
	 */
135 4
	protected function getDefaultResponse(){
136
		return [
137 4
			'success' => false,
138 4
			'exception' => '',
139 4
			'details' => '',
140 4
			'data' => []
141 4
		];
142
	}
143
}
144