Skip to content

Commit

Permalink
Merge pull request #522 from leepeuker/add-external-links-to-person
Browse files Browse the repository at this point in the history
Add external provider links always to movie and person detail pages
  • Loading branch information
leepeuker authored Oct 1, 2023
2 parents a4035e7 + fbe3f6c commit 5bd1b99
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 22 deletions.
24 changes: 24 additions & 0 deletions db/migrations/mysql/20231001110211_AddImdbIdToPersonTable.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 AddImdbIdToPersonTable extends AbstractMigration
{
public function down() : void
{
$this->execute(
<<<SQL
ALTER TABLE person DROP COLUMN imdb_id;
SQL,
);
}

public function up() : void
{
$this->execute(
<<<SQL
ALTER TABLE person ADD COLUMN imdb_id VARCHAR(10) DEFAULT NULL AFTER tmdb_id;
SQL,
);
}
}
129 changes: 129 additions & 0 deletions db/migrations/sqlite/20231001110211_AddImdbIdToPersonTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

final class AddImdbIdToPersonTable extends AbstractMigration
{
public function down()
{
$this->execute(
<<<SQL
CREATE TABLE `tmp_person` (
`id` INTEGER,
`name` TEXT NOT NULL,
`gender` TEXT NOT NULL,
`known_for_department` TEXT DEFAULT NULL,
`poster_path` TEXT DEFAULT NULL,
`biography` TEXT DEFAULT NULL,
`birth_date` TEXT DEFAULT NULL,
`place_of_birth` TEXT DEFAULT NULL,
`death_date` TEXT DEFAULT NULL,
`tmdb_id` INTEGER NOT NULL,
`tmdb_poster_path` TEXT DEFAULT NULL,
`created_at` TEXT NOT NULL,
`updated_at` TEXT DEFAULT NULL,
`updated_at_tmdb` TEXT DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE (`tmdb_id`)
)
SQL,
);
$this->execute(
'INSERT INTO `tmp_person` (
`id`,
`name`,
`gender`,
`known_for_department`,
`poster_path`,
`biography`,
`birth_date`,
`place_of_birth`,
`death_date`,
`tmdb_id`,
`tmdb_poster_path`,
`created_at`,
`updated_at`,
`updated_at_tmdb`
) SELECT
`id`,
`name`,
`gender`,
`known_for_department`,
`poster_path`,
`biography`,
`birth_date`,
`place_of_birth`,
`death_date`,
`tmdb_id`,
`tmdb_poster_path`,
`created_at`,
`updated_at`,
`updated_at_tmdb`
FROM `person`',
);
$this->execute('DROP TABLE `person`');
$this->execute('ALTER TABLE `tmp_person` RENAME TO `person`');
}

public function up()
{
$this->execute(
<<<SQL
CREATE TABLE `tmp_person` (
`id` INTEGER,
`name` TEXT NOT NULL,
`gender` TEXT NOT NULL,
`known_for_department` TEXT DEFAULT NULL,
`poster_path` TEXT DEFAULT NULL,
`biography` TEXT DEFAULT NULL,
`birth_date` TEXT DEFAULT NULL,
`place_of_birth` TEXT DEFAULT NULL,
`death_date` TEXT DEFAULT NULL,
`tmdb_id` INTEGER NOT NULL,
`imdb_id` TEXT DEFAULT NULL,
`tmdb_poster_path` TEXT DEFAULT NULL,
`created_at` TEXT NOT NULL,
`updated_at` TEXT DEFAULT NULL,
`updated_at_tmdb` TEXT DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE (`tmdb_id`)
)
SQL,
);
$this->execute(
'INSERT INTO `tmp_person` (
`id`,
`name`,
`gender`,
`known_for_department`,
`poster_path`,
`biography`,
`birth_date`,
`place_of_birth`,
`death_date`,
`tmdb_id`,
`tmdb_poster_path`,
`created_at`,
`updated_at`,
`updated_at_tmdb`
) SELECT
`id`,
`name`,
`gender`,
`known_for_department`,
`poster_path`,
`biography`,
`birth_date`,
`place_of_birth`,
`death_date`,
`tmdb_id`,
`tmdb_poster_path`,
`created_at`,
`updated_at`,
`updated_at_tmdb`
FROM `person`',
);
$this->execute('DROP TABLE `person`');
$this->execute('ALTER TABLE `tmp_person` RENAME TO `person`');
}
}
5 changes: 5 additions & 0 deletions src/Api/Imdb/ImdbUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ public function buildMovieUrl(string $imdbId) : string
{
return "https://www.imdb.com/title/$imdbId";
}

public function generatePersonUrl(string $imdbId) : string
{
return "https://www.imdb.com/name/$imdbId";
}
}
7 changes: 7 additions & 0 deletions src/Api/Tmdb/Dto/TmdbPerson.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TmdbPerson
{
private function __construct(
private readonly int $tmdbId,
private readonly ?string $imdbId,
private readonly string $name,
private readonly ?string $biography,
private readonly ?Date $birthDate,
Expand All @@ -25,6 +26,7 @@ public static function createFromArray(array $data) : self
{
return new self(
$data['id'],
$data['imdb_id'],
$data['name'],
empty($data['biography']) === true ? null : $data['biography'],
empty($data['birthday']) === true ? null : Date::createFromString($data['birthday']),
Expand Down Expand Up @@ -57,6 +59,11 @@ public function getGender() : Gender
return $this->gender;
}

public function getImdbId() : ?string
{
return $this->imdbId;
}

public function getKnownForDepartment() : ?string
{
return $this->knownForDepartment;
Expand Down
5 changes: 5 additions & 0 deletions src/Api/Tmdb/TmdbUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public function generateMovieUrl(int $tmdbId) : Url
{
return Url::createFromString("https://www.themoviedb.org/movie/$tmdbId/");
}

public function generatePersonUrl(int $tmdbId) : Url
{
return Url::createFromString("https://www.themoviedb.org/person/$tmdbId/");
}
}
4 changes: 4 additions & 0 deletions src/Domain/Person/PersonApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function create(
?Date $deathDate = null,
?string $placeOfBirth = null,
?DateTime $updatedAtTmdb = null,
?string $imdbId = null,
) : PersonEntity {
return $this->repository->create(
$tmdbId,
Expand All @@ -36,6 +37,7 @@ public function create(
$deathDate,
$placeOfBirth,
$updatedAtTmdb,
$imdbId,
);
}

Expand Down Expand Up @@ -113,6 +115,7 @@ public function update(
?Date $deathDate = null,
?string $placeOfBirth = null,
?DateTime $updatedAtTmdb = null,
?string $imdbId = null,
) : PersonEntity {
return $this->repository->update(
$id,
Expand All @@ -126,6 +129,7 @@ public function update(
$deathDate,
$placeOfBirth,
$updatedAtTmdb,
$imdbId,
);
}
}
7 changes: 7 additions & 0 deletions src/Domain/Person/PersonEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ private function __construct(
private readonly Gender $gender,
private readonly ?string $knownForDepartment,
private readonly int $tmdbId,
private readonly ?string $imdbId,
private readonly ?string $posterPath,
private readonly ?string $tmdbPosterPath,
private readonly ?string $biography,
Expand All @@ -32,6 +33,7 @@ public static function createFromArray(array $data) : self
Gender::createFromInt((int)$data['gender']),
$data['known_for_department'],
$data['tmdb_id'],
$data['imdb_id'],
$data['poster_path'],
$data['tmdb_poster_path'],
empty($data['biography']) === true ? null : $data['biography'],
Expand Down Expand Up @@ -67,6 +69,11 @@ public function getId() : int
return $this->id;
}

public function getImdbId() : ?string
{
return $this->imdbId;
}

public function getKnownForDepartment() : ?string
{
return $this->knownForDepartment;
Expand Down
4 changes: 4 additions & 0 deletions src/Domain/Person/PersonRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function create(
?Date $deathDate = null,
?string $placeOfBirth = null,
?DateTime $updatedAtTmdb = null,
?string $imdbId = null,
) : PersonEntity {
$this->dbConnection->insert(
'person',
Expand All @@ -33,6 +34,7 @@ public function create(
'gender' => $gender->asInt(),
'known_for_department' => $knownForDepartment,
'tmdb_id' => $tmdbId,
'imdb_id' => $imdbId,
'tmdb_poster_path' => $tmdbPosterPath,
'biography' => $biography === null ? null : $biography,
'birth_date' => $birthDate === null ? null : (string)$birthDate,
Expand Down Expand Up @@ -117,12 +119,14 @@ public function update(
?Date $deathDate = null,
?string $placeOfBirth = null,
?DateTime $updatedAtTmdb = null,
?string $imdbId = null,
) : PersonEntity {
$payload = [
'name' => $name,
'gender' => $gender->asInt(),
'known_for_department' => $knownForDepartment,
'tmdb_id' => $tmdbId,
'imdb_id' => $imdbId,
'tmdb_poster_path' => $tmdbPosterPath,
'biography' => $biography === null ? null : $biography,
'birth_date' => $birthDate === null ? null : (string)$birthDate,
Expand Down
13 changes: 13 additions & 0 deletions src/HttpController/Web/PersonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Movary\HttpController\Web;

use Movary\Api\Imdb;
use Movary\Api\Tmdb;
use Movary\Domain\Movie\MovieApi;
use Movary\Domain\Person;
use Movary\Domain\User\Service\UserPageAuthorizationChecker;
Expand All @@ -20,6 +22,8 @@ public function __construct(
private readonly Environment $twig,
private readonly UserPageAuthorizationChecker $userPageAuthorizationChecker,
private readonly UrlGenerator $urlGenerator,
private readonly Imdb\ImdbUrlGenerator $imdbUrlGenerator,
private readonly Tmdb\TmdbUrlGenerator $tmdbUrlGenerator,
) {
}

Expand Down Expand Up @@ -50,6 +54,13 @@ public function renderPage(Request $request) : Response
}
}

$imdbId = $person->getImdbId();

$imdbUrl = null;
if ($imdbId !== null) {
$imdbUrl = $this->imdbUrlGenerator->generatePersonUrl($imdbId);
}

return Response::create(
StatusCode::createOk(),
$this->twig->render('page/person.html.twig', [
Expand All @@ -64,6 +75,8 @@ public function renderPage(Request $request) : Response
'birthDate' => $person->getBirthDate(),
'deathDate' => $person->getDeathDate(),
'placeOfBirth' => $person->getPlaceOfBirth(),
'tmdbUrl' => $this->tmdbUrlGenerator->generatePersonUrl($person->getTmdbId()),
'imdbUrl' => $imdbUrl,
],
'moviesAsActor' => $this->movieApi->fetchWithActor($personId, $userId),
'moviesAsDirector' => $this->movieApi->fetchWithDirector($personId, $userId),
Expand Down
2 changes: 2 additions & 0 deletions src/Service/Tmdb/SyncPerson.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function syncPerson(int $tmdbId) : void
$tmdbPerson->getDeathDate(),
$tmdbPerson->getPlaceOfBirth(),
updatedAtTmdb: DateTime::create(),
imdbId: $tmdbPerson->getImdbId()
);

$this->logger->debug('TMDB: Created person meta data', ['personId' => $person->getId(), 'tmdbId' => $person->getTmdbId()]);
Expand All @@ -72,6 +73,7 @@ public function syncPerson(int $tmdbId) : void
$tmdbPerson->getDeathDate(),
$tmdbPerson->getPlaceOfBirth(),
DateTime::create(),
$tmdbPerson->getImdbId()
);

$this->logger->debug('TMDB: Updated person meta data', ['personId' => $person->getId(), 'tmdbId' => $person->getTmdbId()]);
Expand Down
Loading

0 comments on commit 5bd1b99

Please sign in to comment.