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

Artist   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 26.32%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
dl 0
loc 85
ccs 10
cts 38
cp 0.2632
rs 10
c 1
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getNameString() 0 10 3
A toCollection() 0 5 1
A coverToAPI() 0 7 2
A __construct() 0 2 1
A toAPI() 0 7 1
A getIndexingChar() 0 14 3
A getUri() 0 4 1
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\Db;
16
17
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...
18
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...
19
20
use \OCP\AppFramework\Db\Entity;
0 ignored issues
show
Bug introduced by
The type OCP\AppFramework\Db\Entity 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
/**
23
 * @method string getName()
24
 * @method void setName(string $name)
25
 * @method int getCoverFileId()
26
 * @method void setCoverFileId(int $coverFileId)
27
 * @method string getUserId()
28
 * @method void setUserId(string $userId)
29
 * @method string getMbid()
30
 * @method void setMbid(string $mbid)
31
 * @method string getHash()
32
 * @method void setHash(string $hash)
33
 * @method string getStarred()
34
 * @method void setStarred(string $timestamp)
35
 */
36
class Artist extends Entity {
37
	public $name;
38
	public $coverFileId;
39
	public $userId;
40
	public $mbid;
41
	public $hash;
42
	public $starred;
43
44 12
	public function __construct() {
45 12
		$this->addType('coverFileId', 'int');
46 12
	}
47
48
	public function getUri(IURLGenerator $urlGenerator) {
49
		return $urlGenerator->linkToRoute(
50
			'music.api.artist',
51
			['artistIdOrSlug' => $this->id]
52
		);
53
	}
54
55 2
	public function getNameString(IL10N $l10n) {
56 2
		$name = $this->getName();
57 2
		if ($name === null) {
0 ignored issues
show
introduced by
The condition $name === null is always false.
Loading history...
58 1
			$name = $l10n->t('Unknown artist');
59 1
			if (!\is_string($name)) {
60
				/** @var \OC_L10N_String $name */
61 1
				$name = $name->__toString();
62
			}
63
		}
64 2
		return $name;
65
	}
66
67
	/**
68
	 * Get initial character of the artist name in upper case.
69
	 * This is intended to be used as index in a list of aritsts.
70
	 */
71
	public function getIndexingChar() {
72
		// For unknown artists, use '?'
73
		$char = '?';
74
		$name = $this->getName();
75
76
		if (!empty($name)) {
77
			$char = \mb_convert_case(\mb_substr($name, 0, 1), MB_CASE_UPPER);
78
		}
79
		// Bundle all numeric characters together
80
		if (\is_numeric($char)) {
81
			$char = '#';
82
		}
83
84
		return $char;
85
	}
86
87
	/**
88
	 * Return the cover URL to be used in the Shiva API
89
	 * @param IURLGenerator $urlGenerator
90
	 * @return string|null
91
	 */
92
	public function coverToAPI(IURLGenerator $urlGenerator) {
93
		$coverUrl = null;
94
		if ($this->getCoverFileId() > 0) {
95
			$coverUrl = $urlGenerator->linkToRoute('music.api.artistCover',
96
					['artistIdOrSlug' => $this->getId()]);
97
		}
98
		return $coverUrl;
99
	}
100
101
	/**
102
	 * @param IL10N $l10n
103
	 * @param array $albums in the "toCollection" format
104
	 * @return array
105
	 */
106
	public function toCollection(IL10N $l10n, $albums) {
107
		return [
108
			'id' => $this->getId(),
109
			'name' => $this->getNameString($l10n),
110
			'albums' => $albums
111
		];
112
	}
113
114
	public function toAPI(IURLGenerator $urlGenerator, $l10n) {
115
		return [
116
			'id' => $this->getId(),
117
			'name' => $this->getNameString($l10n),
118
			'image' => $this->coverToAPI($urlGenerator),
119
			'slug' => $this->getId() . '-' . $this->slugify('name'),
120
			'uri' => $this->getUri($urlGenerator)
121
		];
122
	}
123
}
124