Completed
Push — fixversion ( 2bd98e )
by
unknown
04:16
created

Helper::cleanUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 53
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 53
ccs 12
cts 12
cp 1
rs 9.5797
cc 2
eloc 10
nc 2
nop 0
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Morris Jobke <[email protected]>
10
 * @copyright Morris Jobke 2014
11
 */
12
13
namespace OCA\Music\Utility;
14
15
use OCP\IDBConnection;
16
17
18
class Helper {
19
20
	/** @var IDBConnection */
21
	private $db;
22
23 3
	public function __construct(IDBConnection $db){
24 3
		$this->db = $db;
25 3
	}
26
27
	/**
28
	 * Removes orphaned data from the database
29
	 */
30 3
	public function cleanUp() {
31
		$sqls = array(
32
			'UPDATE `*PREFIX*music_albums` SET `cover_file_id` = NULL
33
				WHERE `cover_file_id` IS NOT NULL AND `cover_file_id` IN (
34
					SELECT `cover_file_id` FROM (
35
						SELECT `cover_file_id` FROM `*PREFIX*music_albums`
36
						LEFT JOIN `*PREFIX*filecache`
37
							ON `cover_file_id`=`fileid`
38
						WHERE `fileid` IS NULL
39
					) mysqlhack
40 3
				);',
41
			'DELETE FROM `*PREFIX*music_tracks` WHERE `file_id` IN (
42
				SELECT `file_id` FROM (
43
					SELECT `file_id` FROM `*PREFIX*music_tracks`
44
					LEFT JOIN `*PREFIX*filecache`
45
						ON `file_id`=`fileid`
46
					WHERE `fileid` IS NULL
47
					) mysqlhack
48 3
				);',
49
			'DELETE FROM `*PREFIX*music_albums` WHERE `id` IN (
50
				SELECT `id` FROM (
51
					SELECT `*PREFIX*music_albums`.`id`
52
					FROM `*PREFIX*music_albums`
53
					LEFT JOIN `*PREFIX*music_tracks`
54
						ON `*PREFIX*music_tracks`.`album_id` = `*PREFIX*music_albums`.`id`
55
					WHERE `*PREFIX*music_tracks`.`album_id` IS NULL
56
				) as tmp
57 3
			);',
58
			'DELETE FROM `*PREFIX*music_album_artists` WHERE `album_id` IN (
59
				SELECT `album_id` FROM (
60
					SELECT `*PREFIX*music_album_artists`.`album_id`
61
					FROM `*PREFIX*music_album_artists`
62
					LEFT JOIN `*PREFIX*music_albums`
63
						ON `*PREFIX*music_albums`.`id` = `*PREFIX*music_album_artists`.`album_id`
64
					WHERE `*PREFIX*music_albums`.`id` IS NULL
65
				) as tmp
66 3
			);',
67
			'DELETE FROM `*PREFIX*music_artists` WHERE `id` IN (
68
				SELECT `id` FROM (
69
					SELECT `*PREFIX*music_artists`.`id`
70
					FROM `*PREFIX*music_artists`
71
					LEFT JOIN `*PREFIX*music_album_artists`
72
						ON `*PREFIX*music_album_artists`.`artist_id` = `*PREFIX*music_artists`.`id`
73
					WHERE `*PREFIX*music_album_artists`.`artist_id` IS NULL
74
				) as tmp
75 3
			);',
76 3
		);
77
78 3
		foreach ($sqls as $sql) {
79 3
			$query = $this->db->prepare($sql);
80 3
			$query->execute();
81 3
		}
82 3
	}
83
}
84