DownloadManager   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 0
dl 0
loc 262
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setBranch() 0 6 1
A setUpdate() 0 7 1
A getAvailableUpdateFromVersions() 0 4 1
A generatePackages() 0 10 1
A getPackagesJsonData() 0 21 2
D generatePackageList() 0 159 17
A getHash() 0 25 3
1
<?php
2
/**
3
 *
4
 * @copyright (c) 2014 phpBB Group
5
 * @license http://opensource.org/licenses/gpl-3.0.php GNU General Public License v3
6
 * @author MichaelC
7
 *
8
 */
9
10
namespace AppBundle\Utilities;
11
12
class DownloadManager
13
{
14
	protected $branch;
15
	protected $selectedVersion;
16
	protected $update;
17
	protected $packages;
18
	protected $fromVersions;
19
	protected $kernel;
20
	protected $cache;
21
22
	public function __construct($kernel, $cache, $fileLocator)
0 ignored issues
show
Unused Code introduced by
The parameter $fileLocator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
	{
24
		$this->kernel = $kernel;
25
		$this->cache = $cache;
26
	}
27
28
	public function setBranch($branch)
29
	{
30
		$this->branch = $branch;
31
32
		return;
33
	}
34
35
	public function setUpdate($selectedVersion)
36
	{
37
		$this->selectedVersion = $selectedVersion;
38
		$this->update = true;
39
40
		return;
41
	}
42
43
	public function getAvailableUpdateFromVersions()
44
	{
45
		return $this->fromVersions;
46
	}
47
48
	public function generatePackages()
49
	{
50
		$this->generatePackageList();
51
52
		return array(
53
			'packages' => $this->packages,
54
			'caching' => $this->caching,
0 ignored issues
show
Bug introduced by
The property caching does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
			'updateFromVersions' => $this->fromVersions,
56
		);
57
	}
58
59
	private function getPackagesJsonData()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
60
	{
61
		if ($this->cache->contains('packages_json_downloads') !== FALSE)
62
		{
63
			// If we have it in cache, get the packages.json file
64
			$packagesDataJson 	= $this->cache->fetch('packages_json_downloads');
65
			$cacheStatus 		= 'Hit';
0 ignored issues
show
Unused Code introduced by
$cacheStatus is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
		}
67
		else
68
		{
69
			// If we don't have it in cache, find it & load it
70
			$locator = new FileLocator($this->kernel->getRootDir());
71
			$locator->locate('packages.json', null, true);
72
			$packagesDataJson = $locator->getContents();
73
			$this->cache->save('packages_json_downloads', $packagesDataJson, 86400);
74
			$cacheStatus = 'Miss';
0 ignored issues
show
Unused Code introduced by
$cacheStatus is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
		}
76
77
		// Parse JSON response and discard irrelevant branches
78
		return json_decode($packagesDataJson, true);
79
	}
80
81
	private function generatePackageList()
82
	{
83
		// Get packages
84
		$packagesData 		= $this->getPackagesJsonData;
0 ignored issues
show
Bug introduced by
The property getPackagesJsonData does not seem to exist. Did you mean packages?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
85
86
		// Discard those not on this branch
87
		$relevantPackages 	= $packagesData[$this->branch];
88
89
		// Latest release in this branch is...
90
		$release = $relevantPackages['release'];
91
92
		// Generate from versions
93
		$this->fromVersions = explode(',', $relevantPackages['updates']['from']);
94
95
		// Check selected version is a valid version
96
		if (!in_array($this->selectedVersion, $this->fromVersions))
97
		{
98
			$update = false;
0 ignored issues
show
Unused Code introduced by
$update is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
99
		}
100
101
		// Link to the packages for this release. Add filenames on here for download urls.
102
		$download_base_link = 'https://download.phpbb.com/pub/release/' . $this->branch . '/' . $release . '/';
103
104
		$hashCaches = 0;
105
		$packagesTotal = 0;
106
107
		// If we haven't established it's an update (and have an update from version)
108
		if (!$this->update)
109
		{
110
			// Discard irrlevant data
111
			$packages = array(
112
				'package' 		=> $relevantPackages['package']['release'],
113
				'patch' 		=> $relevantPackages['updates']['patch'],
114
				'changed-files' => $relevantPackages['updates']['changed-files'],
115
			);
116
117
			foreach ($packages as $package)
118
			{
119
				// URL to this specific package
120
				$url = $download_base_link . $package['filename'];
121
122
				// Generate sha256/md5 hashes for packages
123
				$hash = $this->gethash($packages[$package]['filename'], $url);
124
125
				// Make use of the stuff we just generated by putting it back in ready for templates
126
				$packages[$package]['url'] 		= $url;
127
				$packages[$package]['hash'] 	= $hash['hash'];
128
				$packages[$package]['hashType'] = $hash['hashType'];
129
130
				// Counts
131
				$packagesTotal++;
132
				($hash['hashCacheStatus'] == 'Hit') ? $hashCaches++ : null;
133
			}
134
		}
135
		else
136
		{
137
			// Discard irrlevant data
138
			$packages = array(
139
				'package' 		=> $relevantPackages['package']['release'],
140
			);
141
142
			foreach ($relevantPackages['updates']['changed-files'] as $changedFilesPackage)
143
			{
144
				if ($changedFilesPackage['from'] == $this->selectedVersion)
145
				{
146
					// URL to this specific package
147
					$url = $download_base_link . $changedFilesPackage['filename'];
148
149
					// Generate sha256/md5 hashes for packages
150
					$hash = $this->gethash($changedFilesPackage['filename'], $url);
151
152
					// Make use of the stuff we just generated by putting it back in ready for templates
153
					$changedFilesPackage['url'] 		= $url;
154
					$changedFilesPackage['hash'] 		= $hash['hash'];
155
					$changedFilesPackage['hashType'] 	= $hash['hashType'];
156
157
					// Counts
158
					$packagesTotal++;
159
					($hash['hashCacheStatus'] == 'Hit') ? $hashCaches++ : null;
160
161
					$packages['changed_files'] = $changedFilesPackage;
162
				}
163
			}
164
165
			foreach ($relevantPackages['updates']['patch'] as $patchFilesPackage)
166
			{
167
				if ($patchFilesPackage['from'] == $this->selectedVersion)
168
				{
169
					// URL to this specific package
170
					$url = $download_base_link . $patchFilesPackage['filename'];
171
172
					// Generate sha256/md5 hashes for packages
173
					$hash = $this->gethash($patchFilesPackage['filename'], $url);
174
175
					// Make use of the stuff we just generated by putting it back in ready for templates
176
					$patchFilesPackage['url'] 		= $url;
177
					$patchFilesPackage['hash'] 		= $hash['hash'];
178
					$patchFilesPackage['hashType'] 	= $hash['hashType'];
179
180
					// Counts
181
					$packagesTotal++;
182
					($hash['hashCacheStatus'] == 'Hit') ? $hashCaches++ : null;
183
184
					$packages['patch'] = $patchFilesPackage;
185
				}
186
			}
187
188
			foreach ($relevantPackages['updates']['code-changes'] as $ccFilesPackage)
189
			{
190
				if ($ccFilesPackage['from'] == $this->selectedVersion)
191
				{
192
					// URL to this specific package
193
					$url = $download_base_link . $ccFilesPackage['filename'];
194
195
					// Generate sha256/md5 hashes for packages
196
					$hash = $this->gethash($ccFilesPackage['filename'], $url);
197
198
					// Make use of the stuff we just generated by putting it back in ready for templates
199
					$ccFilesPackage['url'] 		= $url;
200
					$ccFilesPackage['hash'] 	= $hash['hash'];
201
					$ccFilesPackage['hashType']	= $hash['hashType'];
202
203
					// Counts
204
					$packagesTotal++;
205
					($hash['hashCacheStatus'] == 'Hit') ? $hashCaches++ : null;
206
207
					$packages['code-changes'] = $ccFilesPackage;
208
				}
209
			}
210
211
			foreach ($relevantPackages['updates']['automatic-updaters'] as $automaticFilesPackage)
212
			{
213
				if ($automaticFilesPackage['from'] == $this->selectedVersion)
214
				{
215
					// URL to this specific package
216
					$url = $download_base_link . $automaticFilesPackage['filename'];
217
218
					// Generate sha256/md5 hashes for packages
219
					$hash = $this->gethash($automaticFilesPackage['filename'], $url);
220
221
					// Make use of the stuff we just generated by putting it back in ready for templates
222
					$automaticFilesPackage['url'] 		= $url;
223
					$automaticFilesPackage['hash'] 		= $hash['hash'];
224
					$automaticFilesPackage['hashType'] 	= $hash['hashType'];
225
226
					// Counts
227
					$packagesTotal++;
228
					($hash['hashCacheStatus'] == 'Hit') ? $hashCaches++ : null;
229
230
					$packages['automatic-updater'] = $automaticFilesPackage;
231
				}
232
			}
233
		}
234
235
		$this->packages = $packages;
236
		$this->caching = array($cacheStatus, $hashCaches, $packagesTotal);
0 ignored issues
show
Bug introduced by
The variable $cacheStatus does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
237
238
		return;
239
	}
240
241
	/**
242
	 * Get the MD5 or SHA256 hash
243
	 *
244
	 * @param  string $packageName  Package Filename
245
	 * @param  string $url		  Url to the package
246
	 * @return array 				hash, hashType (md5 or sha356), hashCacheStatus (Hit or Miss)
247
	 */
248
	private function getHash($packageName, $url)
249
	{
250
		$cacheName = 'packages_hash' . $packageName;
251
		$hashType = ($this->branch == '3.0') ? 'md5' : 'sha256';
252
253
		if ($this->cache->contains($cacheName) !== FALSE)
254
		{
255
			// See if we've cached the hash before grabbing an external file
256
			$hash = $this->cache->fetch($cacheName);
257
			$hashCacheStatus = 'Hit';
258
		}
259
		else
260
		{
261
			// It seems we have no choice, grab the file from the external server
262
			$hash = @file_get_contents($url . '.' . $hashType);
263
			$this->cache->save($cacheName, $hash, 86400);
264
			$hashCacheStatus = 'Miss';
265
		}
266
267
		return array(
268
			'hash' 				=> $hash,
269
			'hashType' 			=> $hashType,
270
			'hashCacheStatus' 	=> $hashCacheStatus
271
		);
272
	}
273
}
274