Passed
Push — master ( de702b...3e9331 )
by Pauli
02:18
created

ArtistBusinessLayer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 27.03%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 105
ccs 10
cts 37
cp 0.2703
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCover() 0 12 2
A findAllHavingAlbums() 0 2 1
A findAllByGenre() 0 2 1
A __construct() 0 3 1
A removeCovers() 0 2 1
A addOrUpdateArtist() 0 6 1
A updateCovers() 0 21 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 Morris Jobke <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2013, 2014
12
 * @copyright Pauli Järvinen 2017 - 2020
13
 */
14
15
namespace OCA\Music\BusinessLayer;
16
17
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayer;
18
use \OCA\Music\AppFramework\Core\Logger;
19
20
use \OCA\Music\Db\Artist;
21
use \OCA\Music\Db\ArtistMapper;
22
use \OCA\Music\Db\SortBy;
23
24
use \OCA\Music\Utility\Util;
25
26
class ArtistBusinessLayer extends BusinessLayer {
27
	private $logger;
28
29 3
	public function __construct(ArtistMapper $artistMapper, Logger $logger) {
30 3
		parent::__construct($artistMapper);
31 3
		$this->logger = $logger;
32 3
	}
33
34
	/**
35
	 * Finds all artists who have at least one album
36
	 * @param string $userId the name of the user
37
	 * @param integer $sortBy sort order of the result set
38
	 * @return \OCA\Music\Db\Artist[] artists
39
	 */
40
	public function findAllHavingAlbums($userId, $sortBy=SortBy::None) {
41
		return $this->mapper->findAllHavingAlbums($userId, $sortBy);
42
	}
43
44
	/**
45
	 * Returns all artists filtered by genre
46
	 * @param int $genreId the genre to include
47
	 * @param string $userId the name of the user
48
	 * @param int|null $limit
49
	 * @param int|null $offset
50
	 * @return \OCA\Music\Db\Artist[] artists
51
	 */
52
	public function findAllByGenre($genreId, $userId, $limit=null, $offset=null) {
53
		return $this->mapper->findAllByGenre($genreId, $userId, $limit, $offset);
54
	}
55
56
	/**
57
	 * Adds an artist if it does not exist already or updates an existing artist
58
	 * @param string $name the name of the artist
59
	 * @param string $userId the name of the user
60
	 * @return \OCA\Music\Db\Artist The added/updated artist
61
	 */
62 1
	public function addOrUpdateArtist($name, $userId) {
63 1
		$artist = new Artist();
64 1
		$artist->setName(Util::truncate($name, 256)); // some DB setups can't truncate automatically to column max size
65 1
		$artist->setUserId($userId);
66 1
		$artist->setHash(\hash('md5', \mb_strtolower($name)));
67 1
		return $this->mapper->insertOrUpdate($artist);
68
	}
69
70
	/**
71
	 * Use the given file as cover art for an artist if there exists an artist
72
	 * with name matching the file name.
73
	 * @param File $imageFile
0 ignored issues
show
Bug introduced by
The type OCA\Music\BusinessLayer\File 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...
74
	 * @param string $userId
75
	 * @return artistId of the modified artist if the file was set as cover for an artist;
0 ignored issues
show
Bug introduced by
The type OCA\Music\BusinessLayer\artistId 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...
76
	 *         false if no artist was modified
77
	 */
78
	public function updateCover($imageFile, $userId) {
79
		$name = \pathinfo($imageFile->getName(), PATHINFO_FILENAME);
80
		$matches = $this->findAllByName($name, $userId);
81
82
		if (!empty($matches)) {
83
			$artist = $matches[0];
84
			$artist->setCoverFileId($imageFile->getId());
85
			$this->mapper->update($artist);
86
			return $artist->getId();
87
		}
88
89
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type OCA\Music\BusinessLayer\artistId.
Loading history...
90
	}
91
92
	/**
93
	 * Match the given files by file name to the artist names. If there is a matching
94
	 * artist with no cover image already set, the matched file is set to be used as
95
	 * cover for this artist.
96
	 * @param File[] $imageFiles
97
	 * @param string $userId
98
	 * @return true if any artist covers were updated
99
	 */
100
	public function updateCovers($imageFiles, $userId) {
101
		$updated = false;
102
103
		// construct a lookup table for the images as there may potentially be
104
		// a huge amount of them
105
		$imageLut = [];
106
		foreach ($imageFiles as $imageFile) {
107
			$imageLut[\pathinfo($imageFile->getName(), PATHINFO_FILENAME)] = $imageFile;
108
		}
109
110
		$artists = $this->findAll($userId);
111
112
		foreach ($artists as $artist) {
113
			if ($artist->getCoverFileId() === null && isset($imageLut[$artist->getName()])) {
114
				$artist->setCoverFileId($imageLut[$artist->getName()]->getId());
115
				$this->mapper->update($artist);
116
				$updated = true;
117
			}
118
		}
119
120
		return $updated;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $updated returns the type boolean which is incompatible with the documented return type true.
Loading history...
121
	}
122
123
	/**
124
	 * removes the given cover art files from artists
125
	 * @param integer[] $coverFileIds the file IDs of the cover images
126
	 * @param string[]|null $userIds the users whose music library is targeted; all users are targeted if omitted
127
	 * @return Artist[] artists which got modified, empty array if none
128
	 */
129
	public function removeCovers($coverFileIds, $userIds=null) {
130
		return $this->mapper->removeCovers($coverFileIds, $userIds);
131
	}
132
}
133