Passed
Push — master ( 79d05c...01efb7 )
by Pauli
02:15
created

LastfmService::getAlbumInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 6
cp 0
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 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\AlbumBusinessLayer;
17
use \OCA\Music\BusinessLayer\ArtistBusinessLayer;
18
use \OCA\Music\BusinessLayer\TrackBusinessLayer;
19
20
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...
21
use OCA\Music\AppFramework\BusinessLayer\BusinessLayerException;
22
23
24
class LastfmService {
25
	private $albumBusinessLayer;
26
	private $artistBusinessLayer;
27
	private $trackBusinessLayer;
28
	private $logger;
29
	private $apiKey;
30
31
	const LASTFM_URL = 'http://ws.audioscrobbler.com/2.0/';
32
33
	public function __construct(
34
			AlbumBusinessLayer $albumBusinessLayer,
35
			ArtistBusinessLayer $artistBusinessLayer,
36
			TrackBusinessLayer $trackBusinessLayer,
37
			IConfig $config,
38
			Logger $logger) {
39
		$this->albumBusinessLayer = $albumBusinessLayer;
40
		$this->artistBusinessLayer = $artistBusinessLayer;
41
		$this->trackBusinessLayer = $trackBusinessLayer;
42
		$this->logger = $logger;
43
		$this->apiKey = $config->getSystemValue('music.lastfm_api_key');
44
	}
45
46
	/**
47
	 * @param integer $artistId
48
	 * @param string $userId
49
	 * @return array
50
	 * @throws BusinessLayerException if artist with the given ID is not found
51
	 */
52
	public function getArtistInfo($artistId, $userId) {
53
		$artist = $this->artistBusinessLayer->find($artistId, $userId);
54
55
		return $this->getInfoFromLastFm([
56
				'method' => 'artist.getInfo',
57
				'artist' => $artist->getName()
58
		]);
59
	}
60
61
	/**
62
	 * @param integer $albumId
63
	 * @param string $userId
64
	 * @return array
65
	 * @throws BusinessLayerException if album with the given ID is not found
66
	 */
67
	public function getAlbumInfo($albumId, $userId) {
68
		$album = $this->albumBusinessLayer->find($albumId, $userId);
69
70
		return $this->getInfoFromLastFm([
71
				'method' => 'album.getInfo',
72
				'artist' => $album->getAlbumArtistName(),
73
				'album' => $album->getName()
74
		]);
75
	}
76
77
	/**
78
	 * @param integer $trackId
79
	 * @param string $userId
80
	 * @return array
81
	 * @throws BusinessLayerException if track with the given ID is not found
82
	 */
83
	public function getTrackInfo($trackId, $userId) {
84
		$track= $this->trackBusinessLayer->find($trackId, $userId);
85
86
		return $this->getInfoFromLastFm([
87
				'method' => 'track.getInfo',
88
				'artist' => $track->getArtistName(),
89
				'track' => $track->getTitle()
90
		]);
91
	}
92
93
	private function getInfoFromLastFm($args) {
94
		if (empty($this->apiKey)) {
95
			return ['api_key_set' => false];
96
		}
97
		else {
98
			// append the standard args
99
			$args['api_key'] = $this->apiKey;
100
			$args['format'] = 'json';
101
102
			// glue arg keys and values together ...
103
			$args= \array_map(function($key, $value) {
104
				return $key . '=' . \urlencode($value);
105
			}, \array_keys($args), $args);
106
			// ... and form the final query string
107
			$queryString = '?' . \implode('&', $args);
108
109
			$info = \file_get_contents(self::LASTFM_URL . $queryString);
110
111
			if ($info === false) {
112
				$info = ['connection_ok' => false];
113
			} else {
114
				$info = \json_decode($info, true);
115
				$info['connection_ok'] = true;
116
			}
117
			$info['api_key_set'] = true;
118
			return $info;
119
		}
120
	}
121
}
122