Passed
Pull Request — master (#1266)
by Matthew
04:08
created

ScrobblerController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 5
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
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 Matthew Wells
10
 * @copyright Matthew Wells 2025
11
 */
12
13
namespace OCA\Music\Controller;
14
15
use OCA\Music\Service\ScrobbleServiceException;
16
use OCA\Music\Service\ExternalScrobbler;
17
use OCP\AppFramework\Controller;
18
use OCP\AppFramework\Http\JSONResponse;
19
use OCP\AppFramework\Http\StandaloneTemplateResponse;
20
use OCP\IL10N;
21
use OCP\IRequest;
22
23
class ScrobblerController extends Controller {
24
	private IL10N $l10n;
25
26
	private ?string $userId;
27
28
	/** @var ExternalScrobbler[] $externalScrobblers */
29
	private array $externalScrobblers;
30
31
	public function __construct(string $appName,
32
								IRequest $request,
33
								IL10N $l10n,
34
								?string $userId,
35
								array $externalScrobblers) {
36
		parent::__construct($appName, $request);
37
		$this->l10n = $l10n;
38
		$this->userId = $userId;
39
		$this->appName = $appName;
40
		$this->externalScrobblers = $externalScrobblers;
41
	}
42
43
	/**
44
     * @NoAdminRequired
45
	 * @NoCSRFRequired
46
	 * @NoSameSiteCookieRequired
47
	 */
48
	public function handleToken(?string $serviceIdentifier = null, ?string $token) : StandaloneTemplateResponse {
49
		$params = [
50
			'lang' => $this->l10n->getLanguageCode(),
51
			'success' => false,
52
			'headline' => $this->l10n->t('Unexpected error'),
53
			'identifier' => $serviceIdentifier,
54
			'getsession_response' => '',
55
			'instructions' => $this->l10n->t('Please contact your server administrator for assistance.')
56
		];
57
		$response = new StandaloneTemplateResponse($this->appName, 'scrobble-getsession-result', [], 'base');
58
59
		$scrobbler = $this->getExternalScrobbler($serviceIdentifier);
60
61
		if (!$scrobbler) {
62
			$params['headline'] = $this->l10n->t('Unknown Service');
63
			$params['getsession_response'] = $this->l10n->t('Unknown service %s', [$serviceIdentifier]);
64
			$response->setParams($params);
65
			return $response;
66
		}
67
68
		try {
69
			$scrobbler->generateSession($token, $this->userId);
0 ignored issues
show
Bug introduced by
It seems like $token can also be of type null; however, parameter $token of OCA\Music\Service\Extern...bler::generateSession() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
			$scrobbler->generateSession(/** @scrutinizer ignore-type */ $token, $this->userId);
Loading history...
Bug introduced by
It seems like $this->userId can also be of type null; however, parameter $userId of OCA\Music\Service\Extern...bler::generateSession() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

69
			$scrobbler->generateSession($token, /** @scrutinizer ignore-type */ $this->userId);
Loading history...
70
			$params['success'] = true;
71
			$params['headline'] = $this->l10n->t('All Set!');
72
			$params['instructions'] = $this->l10n->t('Your streams will be scrobbled to %s.', [$scrobbler->getName()]);
73
			$params['getsession_response'] = '';
74
		} catch (ScrobbleServiceException $e) {
75
			$params['headline'] = $this->l10n->t('Authentication failure');
76
			$params['instructions'] = $this->l10n->t('Please review the error message prior to trying again.');
77
			$params['getsession_response'] = $e->getMessage();
78
		} catch (\Exception $t) {
79
			$params['getsession_response'] = $t->getMessage();
80
		} catch (\TypeError $t) {
81
			$params['getsession_response'] = $t->getMessage();
82
		} finally {
83
			$response->setParams($params);
84
			return $response;
85
		}
86
	}
87
88
	/**
89
     * @NoAdminRequired
90
	 * @NoCSRFRequired
91
	 */
92
	public function clearSession(?string $serviceIdentifier = null): JSONResponse {
93
		$response = new JSONResponse(['error' => [
94
			'message' => 'Unknown error'
95
		]]);
96
97
		$scrobbler = $this->getExternalScrobbler($serviceIdentifier);
98
		if (!$scrobbler) {
99
			$response->setData(['error' => [
100
				'message' => $this->l10n->t('Unknown service %s', [$serviceIdentifier])
101
			]]);
102
			return $response;
103
		}
104
105
		try {
106
			$scrobbler->clearSession($this->userId);
107
			$response->setData(['success' => true]);
108
		} catch (\InvalidArgumentException $e) {
109
			$response->setData(['error' => [
110
				'message' => $this->l10n->t('Check the error log for details.')
111
			]]);
112
		} finally {
113
			return $response;
114
		}
115
	}
116
117
	private function getExternalScrobbler(?string $serviceIdentifier) : ?ExternalScrobbler {
118
		foreach ($this->externalScrobblers as $scrobbler) {
119
			if ($scrobbler->getIdentifier() === $serviceIdentifier) {
120
				return $scrobbler;
121
			}
122
		}
123
		return null;
124
	}
125
}
126