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

ScrobblerController::handleToken()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 20
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 20
rs 9.6333
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 Morris Jobke <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2013, 2014
12
 * @copyright Pauli Järvinen 2019 - 2025
13
 */
14
15
namespace OCA\Music\Controller;
16
17
use OCA\Music\Service\ScrobblerService;
18
use OCP\AppFramework\Controller;
19
use OCP\AppFramework\Http\JSONResponse;
20
use OCP\AppFramework\Http\StandaloneTemplateResponse;
21
use OCP\IL10N;
22
use OCP\IRequest;
23
24
class ScrobblerController extends Controller {
25
	private IL10N $l10n;
26
27
	private ?string $userId;
28
29
	private ScrobblerService $scrobblerService;
30
31
	public function __construct(string $appName,
32
								IRequest $request,
33
								IL10N $l10n,
34
								?string $userId,
35
								ScrobblerService $scrobblerService) {
36
		parent::__construct($appName, $request);
37
		$this->l10n = $l10n;
38
		$this->userId = $userId;
39
		$this->appName = $appName;
40
		$this->scrobblerService = $scrobblerService;
41
	}
42
43
	/**
44
	 * @PublicPage
45
	 * @NoCSRFRequired
46
	 * @NoSameSiteCookieRequired
47
	 */
48
	public function handleToken(string $token) : StandaloneTemplateResponse {
49
		try {
50
			$this->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

50
			$this->scrobblerService->generateSession($token, /** @scrutinizer ignore-type */ $this->userId);
Loading history...
51
			$success = true;
52
			$headline = 'All Set!';
53
			$getSessionResponse = '';
54
			$instructions = 'You are now ready to scrobble.';
55
		} catch (\Throwable $t) {
56
			$success = false;
57
			$headline = 'Failed to authenticate.';
58
			$getSessionResponse = $t->getMessage();
59
			$instructions = 'Authentication failure. Please review the error message and try again.';
60
		} finally {
61
			return new StandaloneTemplateResponse($this->appName, 'scrobble-getsession-result', [
62
				'lang' => $this->l10n->getLanguageCode(),
63
				'success' => $success,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $success does not seem to be defined for all execution paths leading up to this point.
Loading history...
64
				'headline' => $this->l10n->t($headline),
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $headline does not seem to be defined for all execution paths leading up to this point.
Loading history...
65
				'getsession_response' => $getSessionResponse,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $getSessionResponse does not seem to be defined for all execution paths leading up to this point.
Loading history...
66
				'instructions' => $this->l10n->t($instructions)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $instructions does not seem to be defined for all execution paths leading up to this point.
Loading history...
67
			], 'base');
68
		}
69
	}
70
71
	/**
72
	 * @PublicPage
73
	 * @NoCSRFRequired
74
	 * @noSameSiteCookieRequired
75
	 * @throws \TypeError when $userId is null
76
	 */
77
	public function clearSession(): JSONResponse {
78
		try {
79
			$this->scrobblerService->clearSession($this->userId);
80
		} catch (\Throwable $t) {
81
			$exception = $t;
82
		}
83
		return new JSONResponse(
84
			empty($exception) ? true : [
0 ignored issues
show
Bug introduced by
It seems like empty($exception) ? true...ception->getMessage())) can also be of type true; however, parameter $data of OCP\AppFramework\Http\JSONResponse::__construct() does only seem to accept array|object, 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

84
			/** @scrutinizer ignore-type */ empty($exception) ? true : [
Loading history...
85
				'error' => $this->l10n->t($exception->getMessage())
86
			]
87
		);
88
	}
89
}
90