Passed
Pull Request — master (#1266)
by Matthew
03:51
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 Matthew Wells
10
 * @copyright Matthew Wells 2025
11
 */
12
13
namespace OCA\Music\Controller;
14
15
use OCA\Music\Service\ScrobblerService;
16
use OCP\AppFramework\Controller;
17
use OCP\AppFramework\Http\JSONResponse;
18
use OCP\AppFramework\Http\StandaloneTemplateResponse;
19
use OCP\IL10N;
20
use OCP\IRequest;
21
22
class ScrobblerController extends Controller {
23
	private IL10N $l10n;
24
25
	private ?string $userId;
26
27
	private ScrobblerService $scrobblerService;
28
29
	public function __construct(string $appName,
30
								IRequest $request,
31
								IL10N $l10n,
32
								?string $userId,
33
								ScrobblerService $scrobblerService) {
34
		parent::__construct($appName, $request);
35
		$this->l10n = $l10n;
36
		$this->userId = $userId;
37
		$this->appName = $appName;
38
		$this->scrobblerService = $scrobblerService;
39
	}
40
41
	/**
42
	 * @PublicPage
43
	 * @NoCSRFRequired
44
	 * @NoSameSiteCookieRequired
45
	 */
46
	public function handleToken(string $token) : StandaloneTemplateResponse {
47
		try {
48
			$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

48
			$this->scrobblerService->generateSession($token, /** @scrutinizer ignore-type */ $this->userId);
Loading history...
49
			$success = true;
50
			$headline = 'All Set!';
51
			$getSessionResponse = '';
52
			$instructions = 'You are now ready to scrobble.';
53
		} catch (\Throwable $t) {
54
			$success = false;
55
			$headline = 'Failed to authenticate.';
56
			$getSessionResponse = $t->getMessage();
57
			$instructions = 'Authentication failure. Please review the error message and try again.';
58
		} finally {
59
			return new StandaloneTemplateResponse($this->appName, 'scrobble-getsession-result', [
60
				'lang' => $this->l10n->getLanguageCode(),
61
				'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...
62
				'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...
63
				'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...
64
				'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...
65
			], 'base');
66
		}
67
	}
68
69
	/**
70
	 * @PublicPage
71
	 * @NoCSRFRequired
72
	 * @noSameSiteCookieRequired
73
	 * @throws \TypeError when $userId is null
74
	 */
75
	public function clearSession(): JSONResponse {
76
		try {
77
			$this->scrobblerService->clearSession($this->userId);
78
		} catch (\Throwable $t) {
79
			$exception = $t;
80
		}
81
		return new JSONResponse(
82
			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

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