Completed
Push — master ( 014884...42845c )
by Pauli
17s queued 12s
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, ?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
		if (!$this->userId) {
60
			$params['getsession_response'] = $this->l10n->t('Not logged in');
61
			$params['instructions'] = $this->l10n->t('Please log in before attempting to authorize a scrobbler');
62
			$response->setParams($params);
63
			return $response;
64
		}
65
66
		$scrobbler = $this->getExternalScrobbler($serviceIdentifier);
67
68
		if (!$scrobbler) {
69
			$params['headline'] = $this->l10n->t('Unknown Service');
70
			$params['getsession_response'] = $this->l10n->t('Unknown service %s', [$serviceIdentifier]);
71
			$response->setParams($params);
72
			return $response;
73
		}
74
75
		try {
76
			$scrobbler->generateSession($token ?? '', $this->userId);
77
			$params['success'] = true;
78
			$params['headline'] = $this->l10n->t('All Set!');
79
			$params['instructions'] = $this->l10n->t('Your streams will be scrobbled to %s.', [$scrobbler->getName()]);
80
			$params['getsession_response'] = '';
81
		} catch (ScrobbleServiceException $e) {
82
			$params['headline'] = $this->l10n->t('Authentication failure');
83
			$params['instructions'] = $this->l10n->t('Please review the error message prior to trying again.');
84
			$params['getsession_response'] = $e->getMessage();
85
		} catch (\Exception $t) {
86
			$params['getsession_response'] = $t->getMessage();
87
		} catch (\TypeError $t) {
88
			$params['getsession_response'] = $t->getMessage();
89
		} finally {
90
			$response->setParams($params);
91
			return $response;
92
		}
93
	}
94
95
	/**
96
	 * @NoAdminRequired
97
	 * @NoCSRFRequired
98
	 */
99
	public function clearSession(?string $serviceIdentifier): JSONResponse {
100
		$response = new JSONResponse(['error' => [
101
			'message' => 'Unknown error'
102
		]]);
103
104
		if (!$this->userId) {
105
			$response->setData(['error' => [
106
				'message' => $this->l10n->t('Not logged in')
107
			]]);
108
			return $response;
109
		}
110
111
		$scrobbler = $this->getExternalScrobbler($serviceIdentifier);
112
		if (!$scrobbler) {
113
			$response->setData(['error' => [
114
				'message' => $this->l10n->t('Unknown service %s', [$serviceIdentifier])
115
			]]);
116
			return $response;
117
		}
118
119
		try {
120
			$scrobbler->clearSession($this->userId);
121
			$response->setData(['success' => true]);
122
		} catch (\InvalidArgumentException $e) {
123
			$response->setData(['error' => [
124
				'message' => $this->l10n->t('Check the error log for details.')
125
			]]);
126
		} finally {
127
			return $response;
128
		}
129
	}
130
131
	private function getExternalScrobbler(?string $serviceIdentifier) : ?ExternalScrobbler {
132
		foreach ($this->externalScrobblers as $scrobbler) {
133
			if ($scrobbler->getIdentifier() === $serviceIdentifier) {
134
				return $scrobbler;
135
			}
136
		}
137
		return null;
138
	}
139
}
140