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

Artist::unknownNameString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
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 9
	public function getUri(IURLGenerator $urlGenerator) {
49 9
		return $urlGenerator->linkToRoute(
50 9
			'music.api.artist',
51 9
			['artistIdOrSlug' => $this->id]
52
		);
53
	}
54
55 11
	public function getNameString(IL10N $l10n) {
56 11
		return $this->getName() ?: self::unknownNameString($l10n);
57 11
	}
58 1
59 1
	/**
60
	 * Get initial character of the artist name in upper case.
61 1
	 * This is intended to be used as index in a list of aritsts.
62
	 */
63
	public function getIndexingChar() {
64 11
		// For unknown artists, use '?'
65
		$char = '?';
66
		$name = $this->getName();
67
68
		if (!empty($name)) {
69
			$char = \mb_convert_case(\mb_substr($name, 0, 1), MB_CASE_UPPER);
70
		}
71
		// Bundle all numeric characters together
72
		if (\is_numeric($char)) {
73
			$char = '#';
74
		}
75
76
		return $char;
77
	}
78
79
	/**
80
	 * Return the cover URL to be used in the Shiva API
81
	 * @param IURLGenerator $urlGenerator
82
	 * @return string|null
83
	 */
84
	public function coverToAPI(IURLGenerator $urlGenerator) {
85
		$coverUrl = null;
86
		if ($this->getCoverFileId() > 0) {
87
			$coverUrl = $urlGenerator->linkToRoute('music.api.artistCover',
88
					['artistIdOrSlug' => $this->getId()]);
89
		}
90
		return $coverUrl;
91
	}
92 9
93 9
	/**
94 9
	 * @param IL10N $l10n
95 7
	 * @param array $albums in the "toCollection" format
96 7
	 * @return array
97
	 */
98 9
	public function toCollection(IL10N $l10n, $albums) {
99
		return [
100
			'id' => $this->getId(),
101
			'name' => $this->getNameString($l10n),
102
			'albums' => $albums
103
		];
104
	}
105
106
	public function toAPI(IURLGenerator $urlGenerator, IL10N $l10n) {
107
		return [
108
			'id' => $this->getId(),
109
			'name' => $this->getNameString($l10n),
110
			'image' => $this->coverToAPI($urlGenerator),
111
			'slug' => $this->getId() . '-' . $this->slugify('name'),
112
			'uri' => $this->getUri($urlGenerator)
113
		];
114 9
	}
115
116 9
	public static function unknownNameString(IL10N $l10n) {
117 9
		$name = $l10n->t('Unknown artist');
118 9
		if (!\is_string($name)) {
119 9
			/** @var \OC_L10N_String $name */
120 9
			$name = $name->__toString();
121
		}
122
		return $name;
123
	}
124
}
125