Skip to content

Commit a8f50ed

Browse files
committed
Refactor session generation
1 parent 0cfa5ae commit a8f50ed

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

lib/Controller/ScrobblerController.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,28 @@ public function __construct(string $appName,
4444
* @PublicPage
4545
* @NoCSRFRequired
4646
* @NoSameSiteCookieRequired
47-
* @throws \TypeError when $userId is null
4847
*/
49-
public function handleToken(string $token) : ?StandaloneTemplateResponse {
50-
$sessionResponse = $this->scrobblerService->generateSession($token, $this->userId);
51-
$success = $sessionResponse === 'ok';
52-
return new StandaloneTemplateResponse($this->appName, 'scrobble-getsession-result', [
53-
'lang' => $this->l10n->getLanguageCode(),
54-
'success' => $success,
55-
'headline' => $this->l10n->t($success ? 'All set!' : 'Failed to authenticate.'),
56-
'getsession_response' => $sessionResponse,
57-
'instructions' => $this->l10n->t(
58-
$success ?
59-
'You are now ready to scrobble.' :
60-
'Authentication failure. Please review the error message and try again.'
61-
)
62-
], 'base');
48+
public function handleToken(string $token) : StandaloneTemplateResponse {
49+
try {
50+
$this->scrobblerService->generateSession($token, $this->userId);
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,
64+
'headline' => $this->l10n->t($headline),
65+
'getsession_response' => $getSessionResponse,
66+
'instructions' => $this->l10n->t($instructions)
67+
], 'base');
68+
}
6369
}
6470

6571
/**

lib/Service/ScrobblerService.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public function __construct(
5757
$this->appName = $appName;
5858
}
5959

60-
public function generateSession(string $token, string $userId): string {
60+
/**
61+
* @throws \Throwable when unable to generate or save a session
62+
*/
63+
public function generateSession(string $token, string $userId) : void {
6164
$scrobbleService = $this->getApiService();
6265
$ch = $this->makeCurlHandle($scrobbleService);
6366
$params = $this->generateBaseMethodParams('auth.getSession');
@@ -69,7 +72,7 @@ public function generateSession(string $token, string $userId): string {
6972

7073
$status = (string)$xml['status'];
7174
if ($status !== 'ok') {
72-
return \sprintf('Error %d: %s', (int)$xml->error['code'], (string)$xml->error);
75+
throw new \Exception((string)$xml->error, (int)$xml->error['code']);
7376
}
7477

7578
try {
@@ -79,10 +82,9 @@ public function generateSession(string $token, string $userId): string {
7982
);
8083
$this->config->setUserValue($userId, $this->appName, 'scrobbleSessionKey', $encryptedKey);
8184
} catch (\Throwable $e) {
82-
$this->logger->error("Unable to save session key");
83-
return $e->getMessage();
85+
$this->logger->error('Unable to save session key ' . $e->getMessage());
86+
throw $e;
8487
}
85-
return 'ok';
8688
}
8789

8890
public function getApiKey() : ?string {
@@ -211,13 +213,14 @@ private function generateBaseMethodParams(string $method) : array {
211213
}
212214

213215
/**
214-
* @return false|resource in PHP8+ false|\CurlHandle
216+
* @return resource in PHP8+ \CurlHandle
217+
* @throws \RuntimeException when unable to initialize a cURL handle
215218
*/
216219
private function makeCurlHandle(string $scrobblerServiceIdentifier) {
217220
$endpoint = self::SCROBBLE_SERVICES[$scrobblerServiceIdentifier]['endpoint'];
218221
$ch = \curl_init($endpoint);
219222
if (!$ch) {
220-
return false;
223+
throw new \RuntimeException('Unable to initialize a cURL handle');
221224
}
222225
\curl_setopt($ch, \CURLOPT_CONNECTTIMEOUT, 10);
223226
\curl_setopt($ch, \CURLOPT_POST, true);

0 commit comments

Comments
 (0)