Skip to content

Commit a0fb280

Browse files
committed
Refactor scrobbling
1 parent a8f50ed commit a0fb280

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

lib/Service/ScrobblerService.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,13 @@ public function getTokenRequestUrl(): ?string {
125125
}
126126
}
127127

128+
/**
129+
* @param array<int,mixed> $trackIds
130+
*/
128131
public function scrobbleTrack(array $trackIds, string $userId, \DateTime $timeOfPlay) : bool {
129132
$sessionKey = $this->getApiSession($userId);
130-
if (!$sessionKey) {
133+
$scrobbleService = $this->getApiService();
134+
if (!$sessionKey || !$scrobbleService) {
131135
return false;
132136
}
133137

@@ -147,18 +151,18 @@ public function scrobbleTrack(array $trackIds, string $userId, \DateTime $timeOf
147151
}
148152
$scrobbleData['api_sig'] = $this->generateSignature($scrobbleData);
149153

150-
$scrobbleService = $this->getApiService();
151-
$ch = $this->makeCurlHandle($scrobbleService);
152-
$postFields = \http_build_query($scrobbleData);
153-
\curl_setopt($ch, \CURLOPT_POSTFIELDS, $postFields);
154-
$xml = \simplexml_load_string(\curl_exec($ch));
155-
156-
$status = (string)$xml['status'];
157-
if ($status !== 'ok') {
158-
return false;
154+
try {
155+
$ch = $this->makeCurlHandle($scrobbleService);
156+
$postFields = \http_build_query($scrobbleData);
157+
\curl_setopt($ch, \CURLOPT_POSTFIELDS, $postFields);
158+
$xml = \simplexml_load_string(\curl_exec($ch));
159+
$status = (string)$xml['status'] === 'ok';
160+
} catch (\Throwable $t) {
161+
$status = false;
162+
$this->logger->error($t->getMessage());
163+
} finally {
164+
return $status;
159165
}
160-
161-
return true;
162166
}
163167

164168
public function clearSession(?string $userId) : void {

0 commit comments

Comments
 (0)