Skip to content

Commit 74027c4

Browse files
authored
Merge pull request #568 from leepeuker/fix-disappearing-person-data
Fix bug removing existing person data when adding new movie
2 parents c135aa7 + 99075f3 commit 74027c4

File tree

5 files changed

+53
-25
lines changed

5 files changed

+53
-25
lines changed

Diff for: src/Domain/Movie/MovieApi.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public function updateCast(int $movieId, TmdbCast $tmdbCast) : void
505505
$this->castApi->deleteByMovieId($movieId);
506506

507507
foreach ($tmdbCast as $position => $castMember) {
508-
$person = $this->personApi->createOrUpdatePersonByTmdbId(
508+
$person = $this->personApi->createOrUpdatePersonWithTmdbCreditsData(
509509
$castMember->getPerson()->getTmdbId(),
510510
$castMember->getPerson()->getName(),
511511
$castMember->getPerson()->getGender(),
@@ -522,7 +522,7 @@ public function updateCrew(int $movieId, TmdbCrew $tmdbCrew) : void
522522
$this->crewApi->deleteByMovieId($movieId);
523523

524524
foreach ($tmdbCrew as $position => $crewMember) {
525-
$person = $this->personApi->createOrUpdatePersonByTmdbId(
525+
$person = $this->personApi->createOrUpdatePersonWithTmdbCreditsData(
526526
$crewMember->getPerson()->getTmdbId(),
527527
$crewMember->getPerson()->getName(),
528528
$crewMember->getPerson()->getGender(),

Diff for: src/Domain/Person/PersonApi.php

+14-15
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function create(
4141
);
4242
}
4343

44-
public function createOrUpdatePersonByTmdbId(
44+
public function createOrUpdatePersonWithTmdbCreditsData(
4545
int $tmdbId,
4646
string $name,
4747
Gender $gender,
@@ -61,13 +61,12 @@ public function createOrUpdatePersonByTmdbId(
6161
}
6262

6363
if ($person->getName() !== $name ||
64-
$person->getGender() !== $gender ||
64+
$person->getGender()->isEqual($gender) === false ||
6565
$person->getKnownForDepartment() !== $knownForDepartment ||
6666
$person->getTmdbPosterPath() !== $posterPath
6767
) {
68-
$this->update(
68+
$this->repository->updateWithTmdbCreditsData(
6969
$person->getId(),
70-
$tmdbId,
7170
$name,
7271
$gender,
7372
$knownForDepartment,
@@ -103,24 +102,19 @@ public function findByTmdbId(int $tmdbId) : ?PersonEntity
103102
return $this->repository->findByTmdbId($tmdbId);
104103
}
105104

106-
public function updateHideInTopLists(int $userId, int $personId, bool $isHidden) : void
107-
{
108-
$this->repository->updateHideInTopLists($userId, $personId, $isHidden);
109-
}
110-
111105
public function update(
112106
int $id,
113107
int $tmdbId,
114108
string $name,
115109
Gender $gender,
116110
?string $knownForDepartment,
117111
?string $tmdbPosterPath,
118-
?string $biography = null,
119-
?Date $birthDate = null,
120-
?Date $deathDate = null,
121-
?string $placeOfBirth = null,
122-
?DateTime $updatedAtTmdb = null,
123-
?string $imdbId = null,
112+
?string $biography,
113+
?Date $birthDate,
114+
?Date $deathDate,
115+
?string $placeOfBirth,
116+
?DateTime $updatedAtTmdb,
117+
?string $imdbId,
124118
) : PersonEntity {
125119
return $this->repository->update(
126120
$id,
@@ -137,4 +131,9 @@ public function update(
137131
$imdbId,
138132
);
139133
}
134+
135+
public function updateHideInTopLists(int $userId, int $personId, bool $isHidden) : void
136+
{
137+
$this->repository->updateHideInTopLists($userId, $personId, $isHidden);
138+
}
140139
}

Diff for: src/Domain/Person/PersonRepository.php

+31-6
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ public function update(
114114
Gender $gender,
115115
?string $knownForDepartment,
116116
?string $tmdbPosterPath,
117-
?string $biography = null,
118-
?Date $birthDate = null,
119-
?Date $deathDate = null,
120-
?string $placeOfBirth = null,
121-
?DateTime $updatedAtTmdb = null,
122-
?string $imdbId = null,
117+
?string $biography,
118+
?Date $birthDate,
119+
?Date $deathDate,
120+
?string $placeOfBirth,
121+
?DateTime $updatedAtTmdb,
122+
?string $imdbId,
123123
) : PersonEntity {
124124
$payload = [
125125
'name' => $name,
@@ -165,6 +165,31 @@ public function updateHideInTopLists(int $userId, int $personId, bool $isHidden)
165165
);
166166
}
167167

168+
public function updateWithTmdbCreditsData(
169+
int $id,
170+
string $name,
171+
Gender $gender,
172+
?string $knownForDepartment,
173+
?string $tmdbPosterPath,
174+
) : PersonEntity {
175+
$updatedAt = (string)DateTime::create();
176+
177+
$payload = [
178+
'name' => $name,
179+
'gender' => $gender->asInt(),
180+
'known_for_department' => $knownForDepartment,
181+
'tmdb_poster_path' => $tmdbPosterPath,
182+
'updated_at' => $updatedAt,
183+
'updated_at_tmdb' => $updatedAt,
184+
];
185+
186+
$this->dbConnection->update(
187+
'person', $payload, ['id' => $id],
188+
);
189+
190+
return $this->fetchById($id);
191+
}
192+
168193
private function fetchById(int $id) : PersonEntity
169194
{
170195
$data = $this->dbConnection->fetchAssociative('SELECT * FROM `person` WHERE id = ?', [$id]);

Diff for: src/Service/Tmdb/PersonChangesCalculator.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
class PersonChangesCalculator
99
{
10-
1110
// phpcs:ignore Generic.Metrics.CyclomaticComplexity
1211
public function calculatePersonChanges(PersonEntity $person, TmdbPerson $tmdbPerson) : array
1312
{
@@ -62,7 +61,7 @@ public function calculatePersonChanges(PersonEntity $person, TmdbPerson $tmdbPer
6261
];
6362
}
6463

65-
if ($person->getGender()->asInt() !== $tmdbPerson->getGender()->asInt()) {
64+
if ($person->getGender()->isEqual($tmdbPerson->getGender()) === false) {
6665
$changes['gender'] = [
6766
'old' => $person->getGender()->getText(),
6867
'new' => $tmdbPerson->getGender()->getText(),

Diff for: src/ValueObject/Gender.php

+5
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,9 @@ public function getText() : string
8080

8181
return self::GENDER_TEXT[$this->asInt()];
8282
}
83+
84+
public function isEqual(Gender $gender) : bool
85+
{
86+
return $this->gender === $gender->gender;
87+
}
8388
}

0 commit comments

Comments
 (0)