diff --git a/src/Domain/Movie/MovieApi.php b/src/Domain/Movie/MovieApi.php index 479ea42a2..37b176b90 100644 --- a/src/Domain/Movie/MovieApi.php +++ b/src/Domain/Movie/MovieApi.php @@ -505,7 +505,7 @@ public function updateCast(int $movieId, TmdbCast $tmdbCast) : void $this->castApi->deleteByMovieId($movieId); foreach ($tmdbCast as $position => $castMember) { - $person = $this->personApi->createOrUpdatePersonByTmdbId( + $person = $this->personApi->createOrUpdatePersonWithTmdbCreditsData( $castMember->getPerson()->getTmdbId(), $castMember->getPerson()->getName(), $castMember->getPerson()->getGender(), @@ -522,7 +522,7 @@ public function updateCrew(int $movieId, TmdbCrew $tmdbCrew) : void $this->crewApi->deleteByMovieId($movieId); foreach ($tmdbCrew as $position => $crewMember) { - $person = $this->personApi->createOrUpdatePersonByTmdbId( + $person = $this->personApi->createOrUpdatePersonWithTmdbCreditsData( $crewMember->getPerson()->getTmdbId(), $crewMember->getPerson()->getName(), $crewMember->getPerson()->getGender(), diff --git a/src/Domain/Person/PersonApi.php b/src/Domain/Person/PersonApi.php index 4103287d3..f319ad5c9 100644 --- a/src/Domain/Person/PersonApi.php +++ b/src/Domain/Person/PersonApi.php @@ -41,7 +41,7 @@ public function create( ); } - public function createOrUpdatePersonByTmdbId( + public function createOrUpdatePersonWithTmdbCreditsData( int $tmdbId, string $name, Gender $gender, @@ -61,13 +61,12 @@ public function createOrUpdatePersonByTmdbId( } if ($person->getName() !== $name || - $person->getGender() !== $gender || + $person->getGender()->isEqual($gender) === false || $person->getKnownForDepartment() !== $knownForDepartment || $person->getTmdbPosterPath() !== $posterPath ) { - $this->update( + $this->repository->updateWithTmdbCreditsData( $person->getId(), - $tmdbId, $name, $gender, $knownForDepartment, @@ -103,11 +102,6 @@ public function findByTmdbId(int $tmdbId) : ?PersonEntity return $this->repository->findByTmdbId($tmdbId); } - public function updateHideInTopLists(int $userId, int $personId, bool $isHidden) : void - { - $this->repository->updateHideInTopLists($userId, $personId, $isHidden); - } - public function update( int $id, int $tmdbId, @@ -115,12 +109,12 @@ public function update( Gender $gender, ?string $knownForDepartment, ?string $tmdbPosterPath, - ?string $biography = null, - ?Date $birthDate = null, - ?Date $deathDate = null, - ?string $placeOfBirth = null, - ?DateTime $updatedAtTmdb = null, - ?string $imdbId = null, + ?string $biography, + ?Date $birthDate, + ?Date $deathDate, + ?string $placeOfBirth, + ?DateTime $updatedAtTmdb, + ?string $imdbId, ) : PersonEntity { return $this->repository->update( $id, @@ -137,4 +131,9 @@ public function update( $imdbId, ); } + + public function updateHideInTopLists(int $userId, int $personId, bool $isHidden) : void + { + $this->repository->updateHideInTopLists($userId, $personId, $isHidden); + } } diff --git a/src/Domain/Person/PersonRepository.php b/src/Domain/Person/PersonRepository.php index 8570bcc6a..0795434f4 100644 --- a/src/Domain/Person/PersonRepository.php +++ b/src/Domain/Person/PersonRepository.php @@ -114,12 +114,12 @@ public function update( Gender $gender, ?string $knownForDepartment, ?string $tmdbPosterPath, - ?string $biography = null, - ?Date $birthDate = null, - ?Date $deathDate = null, - ?string $placeOfBirth = null, - ?DateTime $updatedAtTmdb = null, - ?string $imdbId = null, + ?string $biography, + ?Date $birthDate, + ?Date $deathDate, + ?string $placeOfBirth, + ?DateTime $updatedAtTmdb, + ?string $imdbId, ) : PersonEntity { $payload = [ 'name' => $name, @@ -165,6 +165,31 @@ public function updateHideInTopLists(int $userId, int $personId, bool $isHidden) ); } + public function updateWithTmdbCreditsData( + int $id, + string $name, + Gender $gender, + ?string $knownForDepartment, + ?string $tmdbPosterPath, + ) : PersonEntity { + $updatedAt = (string)DateTime::create(); + + $payload = [ + 'name' => $name, + 'gender' => $gender->asInt(), + 'known_for_department' => $knownForDepartment, + 'tmdb_poster_path' => $tmdbPosterPath, + 'updated_at' => $updatedAt, + 'updated_at_tmdb' => $updatedAt, + ]; + + $this->dbConnection->update( + 'person', $payload, ['id' => $id], + ); + + return $this->fetchById($id); + } + private function fetchById(int $id) : PersonEntity { $data = $this->dbConnection->fetchAssociative('SELECT * FROM `person` WHERE id = ?', [$id]); diff --git a/src/Service/Tmdb/PersonChangesCalculator.php b/src/Service/Tmdb/PersonChangesCalculator.php index 5e3ac3752..5509e7afe 100644 --- a/src/Service/Tmdb/PersonChangesCalculator.php +++ b/src/Service/Tmdb/PersonChangesCalculator.php @@ -7,7 +7,6 @@ class PersonChangesCalculator { - // phpcs:ignore Generic.Metrics.CyclomaticComplexity public function calculatePersonChanges(PersonEntity $person, TmdbPerson $tmdbPerson) : array { @@ -62,7 +61,7 @@ public function calculatePersonChanges(PersonEntity $person, TmdbPerson $tmdbPer ]; } - if ($person->getGender()->asInt() !== $tmdbPerson->getGender()->asInt()) { + if ($person->getGender()->isEqual($tmdbPerson->getGender()) === false) { $changes['gender'] = [ 'old' => $person->getGender()->getText(), 'new' => $tmdbPerson->getGender()->getText(), diff --git a/src/ValueObject/Gender.php b/src/ValueObject/Gender.php index 4cc7c9130..4d45512cc 100644 --- a/src/ValueObject/Gender.php +++ b/src/ValueObject/Gender.php @@ -80,4 +80,9 @@ public function getText() : string return self::GENDER_TEXT[$this->asInt()]; } + + public function isEqual(Gender $gender) : bool + { + return $this->gender === $gender->gender; + } }