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

ScrobblerController::clearSession()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 22
rs 9.7666
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\ScrobblerService;
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 ScrobblerService[] $scrobblerServices */
29
	private array $scrobblerServices;
30
31
	public function __construct(string $appName,
32
								IRequest $request,
33
								IL10N $l10n,
34
								?string $userId,
35
								array $scrobblerServices) {
36
		parent::__construct($appName, $request);
37
		$this->l10n = $l10n;
38
		$this->userId = $userId;
39
		$this->appName = $appName;
40
		$this->scrobblerServices = $scrobblerServices;
41
	}
42
43
	/**
44
	 * @PublicPage
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
		$scrobblerService = $this->getScrobblerService($serviceIdentifier);
60
61
		if (!$scrobblerService) {
62
			$params['headline'] = $this->l10n->t('Unknown Service');
63
			$params['getsession_response'] = $this->l10n->t('Unkonwn service %s', [$serviceIdentifier]);
64
			$response->setParams($params);
65
			return $response;
66
		}
67
68
		try {
69
			$scrobblerService->generateSession($token, $this->userId);
0 ignored issues
show
Bug introduced by
It seems like $this->userId can also be of type null; however, parameter $userId of OCA\Music\Service\Scrobb...vice::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
			$scrobblerService->generateSession($token, /** @scrutinizer ignore-type */ $this->userId);
Loading history...
Bug introduced by
It seems like $token can also be of type null; however, parameter $token of OCA\Music\Service\Scrobb...vice::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
			$scrobblerService->generateSession(/** @scrutinizer ignore-type */ $token, $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.', [$scrobblerService->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
	 * @PublicPage
90
	 * @NoCSRFRequired
91
	 * @noSameSiteCookieRequired
92
	 */
93
	public function clearSession(string $serviceIdentifier): JSONResponse {
94
		$response = new JSONResponse(['error' => [
95
			'message' => 'Unknown error'
96
		]]);
97
98
		$scrobblerService = $this->getScrobblerService($serviceIdentifier);
99
		if (!$scrobblerService) {
100
			$response->setData(['error' => [
101
				'message' => $this->l10n->t('Unknown service %s', [$serviceIdentifier])
102
			]]);
103
			return $response;
104
		}
105
106
		try {
107
			$scrobblerService->clearSession($this->userId);
108
			$response->setData(['success' => true]);
109
		} catch (\InvalidArgumentException $e) {
110
			$response->setData(['error' => [
111
				'message' => $this->l10n->t('Check the error log for details.')
112
			]]);
113
		} finally {
114
			return $response;
115
		}
116
	}
117
118
	private function getScrobblerService(string $serviceIdentifier) : ?ScrobblerService {
119
		foreach ($this->scrobblerServices as $scrobblerService) {
120
			if ($scrobblerService->getIdentifier() === $serviceIdentifier) {
121
				return $scrobblerService;
122
			}
123
		}
124
		return null;
125
	}
126
}
127