Apply suggestions from code review

Co-authored-by: Jonathan Flueren <11487762+JonOfUs@users.noreply.github.com>
This commit is contained in:
kalle (jag) 2022-10-27 09:10:16 +02:00 committed by thrillfall
parent 5a52efea07
commit 3a84b2506b
5 changed files with 21 additions and 22 deletions

View File

@ -56,7 +56,7 @@ class PersonalSettingsController extends Controller {
return new JSONResponse([
'message' => "Missing query parameter 'url'.",
'data' => null,
], statusCode: Http::STATUS_BAD_REQUEST);
], Http::STATUS_BAD_REQUEST);
}
return new JsonResponse([
'data' => $this->dataReader->getCachedOrFetchPodcastData($url, $this->userId),

View File

@ -42,18 +42,17 @@ class PodcastData implements JsonSerializable {
$xml = new SimpleXMLElement($xmlString);
$channel = $xml->channel;
return new PodcastData(
title: self::stringOrNull($channel->title),
author: self::getXPathContent($xml, '/rss/channel/itunes:author'),
link: self::stringOrNull($channel->link),
description: self::stringOrNull($channel->description),
imageUrl:
self::getXPathContent($xml, '/rss/channel/image/url')
self::stringOrNull($channel->title),
self::getXPathContent($xml, '/rss/channel/itunes:author'),
self::stringOrNull($channel->link),
self::stringOrNull($channel->description),
self::getXPathContent($xml, '/rss/channel/image/url')
?? self::getXPathAttribute($xml, '/rss/channel/itunes:image/@href'),
fetchedAtUnix: $fetchedAtUnix ?? (new DateTime())->getTimestamp(),
$fetchedAtUnix ?? (new DateTime())->getTimestamp()
);
}
private static function stringOrNull(mixed $value): ?string {
private static function stringOrNull($value): ?string {
if ($value) {
return (string)$value;
}
@ -159,7 +158,7 @@ class PodcastData implements JsonSerializable {
/**
* @return array<string,mixed>
*/
public function jsonSerialize(): mixed {
public function jsonSerialize(): array {
return $this->toArray();
}
@ -168,13 +167,13 @@ class PodcastData implements JsonSerializable {
*/
public static function fromArray(array $data): PodcastData {
return new PodcastData(
title: $data['title'],
author: $data['author'],
link: $data['link'],
description: $data['description'],
imageUrl: $data['imageUrl'],
fetchedAtUnix: $data['fetchedAtUnix'],
imageBlob: $data['imageBlob'],
$data['title'],
$data['author'],
$data['link'],
$data['description'],
$data['imageUrl'],
$data['fetchedAtUnix'],
$data['imageBlob']
);
}
}

View File

@ -69,7 +69,7 @@ class PodcastDataReader {
$body = $resp->getBody();
$bodyBase64 = base64_encode($body);
return "data:$contentType;base64,$bodyBase64";
} catch (Exception) {
} catch (Exception $e) {
return null;
}
}

View File

@ -63,7 +63,7 @@ class PodcastMetrics implements JsonSerializable {
/**
* @return array<string,mixed>
*/
public function jsonSerialize(): mixed {
public function jsonSerialize(): array {
return $this->toArray();
}
}

View File

@ -65,9 +65,9 @@ class PodcastMetricsReader {
private function createMetricsForUrl(string $url): PodcastMetrics {
return new PodcastMetrics(
url: $url,
listenedSeconds: 0,
actionCounts: new PodcastActionCounts(),
$url,
0,
new PodcastActionCounts()
);
}