Skip to content

Commit

Permalink
Merge pull request #52 from leepeuker/add-tmdb-language-cache
Browse files Browse the repository at this point in the history
Add local db caching for tmdb iso 693-1 language mappings
  • Loading branch information
leepeuker authored Jul 14, 2022
2 parents dabddf0 + 6a1bdec commit b06a3fa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 15 deletions.
24 changes: 24 additions & 0 deletions db/migrations/20220714094745_AddCacheTableForTmdbLanguages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddCacheTableForTmdbLanguages extends AbstractMigration
{
public function down() : void
{
$this->execute('DROP TABLE `cache_tmdb_languages`');
}

public function up() : void
{
$this->execute(
<<<SQL
CREATE TABLE `cache_tmdb_languages` (
`iso_639_1` CHAR(2) NOT NULL,
`english_name` VARCHAR(256) NOT NULL,
PRIMARY KEY (`iso_639_1`)
) COLLATE="utf8mb4_unicode_ci" ENGINE=InnoDB
SQL
);
}
}
21 changes: 6 additions & 15 deletions src/Api/Tmdb/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Movary\Api\Tmdb;

use Movary\Api\Tmdb\Cache\Iso6931;
use Movary\Api\Tmdb\Dto\Credits;
use Movary\Api\Tmdb\Dto\Movie;

class Api
{
private array $languages = [];

public function __construct(private readonly Client $client)
{
public function __construct(
private readonly Client $client,
private readonly Iso6931 $iso6931,
) {
}

public function fetchMovieCredits(int $movieId) : Credits
Expand All @@ -29,17 +30,7 @@ public function fetchMovieDetails(int $movieId) : Movie

public function getLanguageByCode(string $languageCode) : string
{
if ($this->languages === []) {
$this->languages = $this->client->get('/configuration/languages');
}

foreach ($this->languages as $language) {
if ($language['iso_639_1'] === $languageCode) {
return $language['english_name'];
}
}

throw new \RuntimeException('Language code not handled: ' . $languageCode);
return $this->iso6931->getLanguageByCode($languageCode);
}

public function searchMovie(string $searchTerm) : array
Expand Down
54 changes: 54 additions & 0 deletions src/Api/Tmdb/Cache/Iso6931.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);

namespace Movary\Api\Tmdb\Cache;

use Doctrine\DBAL\Connection;
use Movary\Api\Tmdb\Client;

class Iso6931
{
private array $languages = [];

public function __construct(private readonly Connection $dbConnection, private readonly Client $client)
{
}

public function getLanguageByCode(string $languageCode) : string
{
if ($this->languages === []) {
$this->loadFromDatabase();
}

if ($this->languages === []) {
$this->loadFromTmdb();
}

foreach ($this->languages as $iso6931 => $englishName) {
if ($iso6931 === $languageCode) {
return $englishName;
}
}

throw new \RuntimeException('Language code not handled: ' . $languageCode);
}

private function loadFromDatabase() : bool
{
$this->languages = $this->dbConnection->fetchAllKeyValue('SELECT iso_639_1, english_name FROM cache_tmdb_languages');

return empty($this->languages);
}

private function loadFromTmdb() : bool
{
$languages = $this->client->get('/configuration/languages');

foreach ($languages as $language) {
$this->dbConnection->insert('cache_tmdb_languages', ['iso_639_1' => $language['iso_639_1'], 'english_name' => $language['english_name']]);
}

$this->loadFromDatabase();

return true;
}
}

0 comments on commit b06a3fa

Please sign in to comment.