-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from leepeuker/add-tmdb-language-cache
Add local db caching for tmdb iso 693-1 language mappings
- Loading branch information
Showing
3 changed files
with
84 additions
and
15 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
db/migrations/20220714094745_AddCacheTableForTmdbLanguages.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |