feat(*.php): refactor, clean up and abstract dynamic page code
This commit is contained in:
parent
2f9038dccb
commit
60be0ed01d
30 changed files with 318 additions and 401 deletions
19
api/Classes/ArtistFetcher.php
Normal file
19
api/Classes/ArtistFetcher.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class ArtistFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $url): ?array
|
||||
{
|
||||
$cacheKey = "artist_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$artist = $this->fetchSingleFromApi("optimized_artists", $url);
|
||||
if (!$artist) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $artist);
|
||||
return $artist;
|
||||
}
|
||||
}
|
19
api/Classes/BookFetcher.php
Normal file
19
api/Classes/BookFetcher.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class BookFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $url): ?array
|
||||
{
|
||||
$cacheKey = "book_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$book = $this->fetchSingleFromApi("optimized_books", $url);
|
||||
if (!$book) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $book);
|
||||
return $book;
|
||||
}
|
||||
}
|
19
api/Classes/GenreFetcher.php
Normal file
19
api/Classes/GenreFetcher.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class GenreFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $url): ?array
|
||||
{
|
||||
$cacheKey = "genre_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$genre = $this->fetchSingleFromApi("optimized_genres", $url);
|
||||
if (!$genre) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $genre);
|
||||
return $genre;
|
||||
}
|
||||
}
|
19
api/Classes/MovieFetcher.php
Normal file
19
api/Classes/MovieFetcher.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class MovieFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $url): ?array
|
||||
{
|
||||
$cacheKey = "movie_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$movie = $this->fetchSingleFromApi("optimized_movies", $url);
|
||||
if (!$movie) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $movie);
|
||||
return $movie;
|
||||
}
|
||||
}
|
29
api/Classes/PageFetcher.php
Normal file
29
api/Classes/PageFetcher.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
use App\Classes\BaseHandler;
|
||||
|
||||
abstract class PageFetcher extends BaseHandler
|
||||
{
|
||||
protected function cacheGet(string $key): mixed
|
||||
{
|
||||
return $this->cache && $this->cache->exists($key) ? json_decode($this->cache->get($key), true) : null;
|
||||
}
|
||||
|
||||
protected function cacheSet(string $key, mixed $value, int $ttl = 3600): void
|
||||
{
|
||||
if ($this->cache) $this->cache->setex($key, $ttl, json_encode($value));
|
||||
}
|
||||
|
||||
protected function fetchSingleFromApi(string $endpoint, string $url): ?array
|
||||
{
|
||||
$data = $this->fetchFromApi($endpoint, "url=eq./{$url}");
|
||||
return $data[0] ?? null;
|
||||
}
|
||||
|
||||
protected function fetchPostRpc(string $endpoint, array $body): ?array
|
||||
{
|
||||
return $this->makeRequest("POST", $endpoint, ['json' => $body]);
|
||||
}
|
||||
}
|
19
api/Classes/ShowFetcher.php
Normal file
19
api/Classes/ShowFetcher.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class ShowFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $url): ?array
|
||||
{
|
||||
$cacheKey = "show_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$show = $this->fetchSingleFromApi("optimized_shows", $url);
|
||||
if (!$show) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $show);
|
||||
return $show;
|
||||
}
|
||||
}
|
26
api/Classes/TagFetcher.php
Normal file
26
api/Classes/TagFetcher.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class TagFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $tag, int $page = 1, int $pageSize = 20): ?array
|
||||
{
|
||||
$offset = ($page - 1) * $pageSize;
|
||||
$cacheKey = "tag_" . md5("{$tag}_{$page}");
|
||||
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$results = $this->fetchPostRpc("rpc/get_tagged_content", [
|
||||
"tag_query" => $tag,
|
||||
"page_size" => $pageSize,
|
||||
"page_offset" => $offset
|
||||
]);
|
||||
|
||||
if (!$results || count($results) === 0) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $results);
|
||||
return $results;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue