Passed
Push — feature/playlist_improvements ( 2a690a...faf9ee )
by Pauli
14:31
created

Library   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 89
ccs 0
cts 43
cp 0
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getTracksAlbumsAndArtists() 0 31 3
A toCollection() 0 29 5
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 Pauli Järvinen <[email protected]>
10
 * @copyright Pauli Järvinen 2018 - 2020
11
 */
12
13
namespace OCA\Music\BusinessLayer;
14
15
use \OCA\Music\AppFramework\Core\Logger;
16
use \OCA\Music\Utility\CoverHelper;
17
use \OCA\Music\Utility\Util;
18
19
use \OCP\IL10N;
0 ignored issues
show
Bug introduced by
The type OCP\IL10N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use \OCP\IURLGenerator;
0 ignored issues
show
Bug introduced by
The type OCP\IURLGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
class Library {
23
	private $albumBusinessLayer;
24
	private $artistBusinessLayer;
25
	private $trackBusinessLayer;
26
	private $coverHelper;
27
	private $urlGenerator;
28
	private $l10n;
29
	private $logger;
30
31
	public function __construct(
32
			AlbumBusinessLayer $albumBusinessLayer,
33
			ArtistBusinessLayer $artistBusinessLayer,
34
			TrackBusinessLayer $trackBusinessLayer,
35
			CoverHelper $coverHelper,
36
			IURLGenerator $urlGenerator,
37
			IL10N $l10n,
38
			Logger $logger) {
39
		$this->albumBusinessLayer = $albumBusinessLayer;
40
		$this->artistBusinessLayer = $artistBusinessLayer;
41
		$this->trackBusinessLayer = $trackBusinessLayer;
42
		$this->coverHelper = $coverHelper;
43
		$this->urlGenerator = $urlGenerator;
44
		$this->l10n = $l10n;
45
		$this->logger = $logger;
46
	}
47
48
	public function getTracksAlbumsAndArtists($userId) {
49
		// Get all the entities from the DB first. The order of queries is important if we are in
50
		// the middle of a scanning process: we don't want to get tracks which do not yet have
51
		// an album entry or albums which do not yet have artist entry.
52
		/** @var Track[] $allTracks */
53
		$tracks = $this->trackBusinessLayer->findAll($userId);
54
		/** @var Album[] $allAlbums */
55
		$albums = $this->albumBusinessLayer->findAll($userId);
56
		/** @var Artist[] $allArtists */
57
		$artists = $this->artistBusinessLayer->findAll($userId);
58
59
		$artistsById = Util::createIdLookupTable($artists);
60
		$albumsById = Util::createIdLookupTable($albums);
61
62
		foreach ($tracks as $idx => $track) {
63
			$album = $albumsById[$track->getAlbumId()];
64
65
			if (empty($album)) {
66
				$this->logger->log("DB error on track {$track->id} '{$track->title}': ".
67
				"album with ID {$track->albumId} not found. Skipping the track.", 'warn');
68
				unset($tracks[$idx]);
69
			}
70
			else {
71
				$track->setAlbum($album);
72
			}
73
		}
74
75
		return [
76
			'tracks' => $tracks,
77
			'albums' => $albumsById,
78
			'artists' => $artistsById
79
		];
80
	}
81
82
	public function toCollection($userId) {
83
		$entities = $this->getTracksAlbumsAndArtists($userId);
84
		$coverHashes = $this->coverHelper->getAllCachedAlbumCoverHashes($userId);
85
86
		// Create a multi-level dictionary of tracks where each track can be found
87
		// by addressing like $trackDict[artistId][albumId][ordinal]. The tracks are
88
		// in the dictionary in the "toCollection" format.
89
		$trackDict = [];
90
		foreach ($entities['tracks'] as $track) {
91
			$trackDict[$track->getAlbum()->getAlbumArtistId()][$track->getAlbumId()][]
92
				= $track->toCollection($this->l10n);
93
		}
94
95
		// Then create the actual collection by iterating over the previusly created
96
		// dictionary and creating artists and albums in the "toCollection" format.
97
		$collection = [];
98
		foreach ($trackDict as $artistId => $artistTracksByAlbum) {
99
100
			$artistAlbums = [];
101
			foreach ($artistTracksByAlbum as $albumId => $albumTracks) {
102
				$coverHash = isset($coverHashes[$albumId]) ? $coverHashes[$albumId] : null;
103
				$artistAlbums[] = $entities['albums'][$albumId]->toCollection(
104
						$this->urlGenerator, $this->l10n, $coverHash, $albumTracks);
105
			}
106
107
			$collection[] = $entities['artists'][$artistId]->toCollection($this->l10n, $artistAlbums);
108
		}
109
110
		return $collection;
111
	}
112
113
}
114