Passed
Push — master ( 9f6f8b...b68386 )
by Pauli
03:11 queued 16s
created

LastfmService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 24
c 3
b 0
f 0
dl 0
loc 43
ccs 0
cts 20
cp 0
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getArtistInfo() 0 21 3
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 2020
11
 */
12
13
namespace OCA\Music\Utility;
14
15
use \OCA\Music\AppFramework\Core\Logger;
16
use \OCA\Music\BusinessLayer\ArtistBusinessLayer;
17
18
use \OCP\IConfig;
0 ignored issues
show
Bug introduced by
The type OCP\IConfig 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
use OCA\Music\AppFramework\BusinessLayer\BusinessLayerException;
20
21
22
class LastfmService {
23
	private $artistBusinessLayer;
24
	private $logger;
25
	private $apiKey;
26
27
	const LASTFM_URL = 'http://ws.audioscrobbler.com/2.0/';
28
29
	public function __construct(
30
			ArtistBusinessLayer $artistBusinessLayer,
31
			IConfig $config,
32
			Logger $logger) {
33
		$this->artistBusinessLayer = $artistBusinessLayer;
34
		$this->logger = $logger;
35
		$this->apiKey = $config->getSystemValue('music.lastfm_api_key');
36
	}
37
38
	/**
39
	 * @param integer $artistId
40
	 * @param string $userId
41
	 * @return array
42
	 * @throws BusinessLayerException if artist with the given ID is not found
43
	 */
44
	public function getArtistInfo($artistId, $userId) {
45
		$artist = $this->artistBusinessLayer->find($artistId, $userId);
46
47
		if (empty($this->apiKey)) {
48
			return ['api_key_set' => false];
49
		}
50
		else {
51
			$info = \file_get_contents(self::LASTFM_URL .
52
					'?method=artist.getInfo' .
53
					'&artist=' . \urlencode($artist->getName()) .
54
					'&api_key=' . $this->apiKey .
55
					'&format=json');
56
57
			if ($info === false) {
58
				$info = ['connection_ok' => false];
59
			} else {
60
				$info = \json_decode($info, true);
61
				$info['connection_ok'] = true;
62
			}
63
			$info['api_key_set'] = true;
64
			return $info;
65
		}
66
	}
67
68
}
69