2022-08-28 18:44:30 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Core\PodcastData;
|
|
|
|
|
2022-09-17 16:12:13 +00:00
|
|
|
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeRepository;
|
2022-08-28 18:44:30 +00:00
|
|
|
use OCP\Http\Client\IClient;
|
|
|
|
use OCP\Http\Client\IClientService;
|
|
|
|
use OCP\ICache;
|
|
|
|
use OCP\ICacheFactory;
|
|
|
|
|
2022-09-17 16:12:13 +00:00
|
|
|
class PodcastDataReader {
|
2022-08-28 18:44:30 +00:00
|
|
|
private ?ICache $cache = null;
|
|
|
|
private IClient $httpClient;
|
2022-09-17 16:12:13 +00:00
|
|
|
private SubscriptionChangeRepository $subscriptionChangeRepository;
|
2022-08-28 18:44:30 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ICacheFactory $cacheFactory,
|
|
|
|
IClientService $httpClientService,
|
2022-09-17 16:12:13 +00:00
|
|
|
SubscriptionChangeRepository $subscriptionChangeRepository,
|
2022-08-28 18:44:30 +00:00
|
|
|
) {
|
|
|
|
if ($cacheFactory->isLocalCacheAvailable()) {
|
|
|
|
$this->cache = $cacheFactory->createLocal('GPodderSync-Podcasts');
|
|
|
|
}
|
|
|
|
$this->httpClient = $httpClientService->newClient();
|
2022-09-17 16:12:13 +00:00
|
|
|
$this->subscriptionChangeRepository = $subscriptionChangeRepository;
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 16:12:13 +00:00
|
|
|
public function getCachedOrFetchPodcastData(string $url, string $userId): ?PodcastData {
|
2022-08-28 18:44:30 +00:00
|
|
|
if ($this->cache == null) {
|
2022-09-17 16:12:13 +00:00
|
|
|
return $this->fetchPodcastData($url, $userId);
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
|
|
|
$oldData = $this->tryGetCachedPodcastData($url);
|
|
|
|
if ($oldData) {
|
|
|
|
return $oldData;
|
|
|
|
}
|
2022-09-17 16:12:13 +00:00
|
|
|
$newData = $this->fetchPodcastData($url, $userId);
|
2022-08-28 18:44:30 +00:00
|
|
|
$this->trySetCachedPodcastData($url, $newData);
|
|
|
|
return $newData;
|
|
|
|
}
|
|
|
|
|
2022-09-17 16:12:13 +00:00
|
|
|
private function userHasPodcast(string $url, string $userId): bool {
|
|
|
|
$subscriptionChanges = $this->subscriptionChangeRepository->findByUrl($url, $userId);
|
|
|
|
return $subscriptionChanges !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchPodcastData(string $url, string $userId): PodcastData {
|
|
|
|
if (!$this->userHasPodcast($url, $userId)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-08-28 18:44:30 +00:00
|
|
|
$resp = $this->httpClient->get($url);
|
|
|
|
$statusCode = $resp->getStatusCode();
|
|
|
|
if ($statusCode < 200 || $statusCode >= 300) {
|
|
|
|
throw new \ErrorException("Podcast RSS URL returned non-2xx status code: $statusCode");
|
|
|
|
}
|
|
|
|
$body = $resp->getBody();
|
2022-09-17 15:03:02 +00:00
|
|
|
return PodcastData::parseRssXml($body);
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tryGetCachedPodcastData(string $url): ?PodcastData {
|
|
|
|
$oldData = $this->cache->get($url);
|
|
|
|
if (!$oldData) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-17 15:03:02 +00:00
|
|
|
return PodcastData::fromArray($oldData);
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 16:12:13 +00:00
|
|
|
public function trySetCachedPodcastData(string $url, PodcastData $data): bool {
|
|
|
|
return $this->cache->set($url, $data->toArray());
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
|
|
|
}
|