Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine tmdb api requests to reduce total amount of requests #431

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Api/Tmdb/Dto/TmdbCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
*/
class TmdbCast extends AbstractList
{
public static function create() : self
{
return new self();
}

public static function createFromArray(array $data) : self
{
$cast = new self();
$cast = self::create();

foreach ($data as $castMember) {
$cast->add(TmdbCastMember::createFromArray($castMember));
Expand Down
8 changes: 8 additions & 0 deletions src/Api/Tmdb/Dto/TmdbCredits.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ private function __construct(
) {
}

public static function create() : self
{
return new self(
TmdbCast::create(),
TmdbCrew::create(),
);
}

public static function createFromArray(array $data) : self
{
return new self(
Expand Down
7 changes: 6 additions & 1 deletion src/Api/Tmdb/Dto/TmdbCrew.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
*/
class TmdbCrew extends AbstractList
{
public static function create() : self
{
return new self();
}

public static function createFromArray(array $data) : self
{
$cast = new self();
$cast = self::create();

foreach ($data as $crewMember) {
$cast->add(TmdbCrewMember::createFromArray($crewMember));
Expand Down
7 changes: 7 additions & 0 deletions src/Api/Tmdb/Dto/TmdbMovie.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private function __construct(
private readonly ?string $posterPath,
private readonly ?string $backdropPath,
private readonly ?string $imdbId,
private readonly TmdbCredits $credits,
) {
}

Expand All @@ -41,6 +42,7 @@ public static function createFromArray(array $data) : self
$data['poster_path'],
$data['backdrop_path'],
empty($data['imdb_id']) === true ? null : $data['imdb_id'],
empty($data['credits']) === true ? TmdbCredits::create() : TmdbCredits::createFromArray($data['credits']),
);
}

Expand All @@ -49,6 +51,11 @@ public function getBackdropPath() : ?string
return $this->backdropPath;
}

public function getCredits() : TmdbCredits
{
return $this->credits;
}

public function getGenres() : TmdbGenreList
{
return $this->genres;
Expand Down
9 changes: 1 addition & 8 deletions src/Api/Tmdb/TmdbApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,9 @@ public function fetchCompany(int $companyId) : TmdbCompany
return TmdbCompany::createFromArray($data);
}

public function fetchMovieCredits(int $movieId) : TmdbCredits
{
$data = $this->client->get('/movie/' . $movieId . '/credits');

return TmdbCredits::createFromArray($data);
}

public function fetchMovieDetails(int $movieId) : TmdbMovie
{
$data = $this->client->get('/movie/' . $movieId);
$data = $this->client->get('/movie/' . $movieId, ['append_to_response' => 'credits']);

return TmdbMovie::createFromArray($data);
}
Expand Down
11 changes: 2 additions & 9 deletions src/Service/Tmdb/SyncMovie.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public function syncMovie(int $tmdbId) : MovieEntity
$this->movieApi->updateGenres($movie->getId(), $this->genreConverter->getMovaryGenresFromTmdbMovie($tmdbMovie));
$this->movieApi->updateProductionCompanies($movie->getId(), $this->productionCompanyConverter->getMovaryProductionCompaniesFromTmdbMovie($tmdbMovie));

$this->updateCredits($movie->getId(), $tmdbId);
$this->movieApi->updateCast($movie->getId(), $tmdbMovie->getCredits()->getCast());
$this->movieApi->updateCrew($movie->getId(), $tmdbMovie->getCredits()->getCrew());

$this->dbConnection->commit();
} catch (\Exception $e) {
Expand All @@ -105,12 +106,4 @@ public function syncMovie(int $tmdbId) : MovieEntity

return $movie;
}

private function updateCredits(int $movieId, int $tmdbId) : void
{
$credits = $this->tmdbApi->fetchMovieCredits($tmdbId);

$this->movieApi->updateCast($movieId, $credits->getCast());
$this->movieApi->updateCrew($movieId, $credits->getCrew());
}
}
1 change: 1 addition & 0 deletions tests/unit/Api/Tmdb/Dto/TmdbMovieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function testWithAllPossibleValuesEmptyOrNull() : void
'poster_path' => null,
'backdrop_path' => null,
'imdb_id' => null,
'credits' => null,
];

$subject = TmdbMovie::createFromArray($testData);
Expand Down