From 23ae1ed3fee84090a0f511d15557626c8ef10138 Mon Sep 17 00:00:00 2001 From: ltuffery <123221865+ltuffery@users.noreply.github.com> Date: Thu, 22 May 2025 16:51:34 +0200 Subject: [PATCH] fix: photo warning error && duplicate users --- api/src/Factory/Factory.php | 18 +++++++++++++----- api/src/Factory/PhotoFactory.php | 8 +++++--- api/src/Factory/PreferenceFactory.php | 8 ++++---- api/src/Model/User.php | 4 ---- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/api/src/Factory/Factory.php b/api/src/Factory/Factory.php index 2ebbd9a..e70a9b9 100644 --- a/api/src/Factory/Factory.php +++ b/api/src/Factory/Factory.php @@ -8,7 +8,7 @@ abstract class Factory { private string $model; private int $count = 1; - private array $states = []; + protected array $states = []; private array $child = []; public function __construct(string $model) @@ -38,12 +38,20 @@ public function create(): array|Model for ($i = 0; $i < $this->count; $i++) { $model = new $this->model(); - $data = array_merge($this->define(), $this->states); + $states = []; - foreach ($data as $key => $value) { - $model->{$key} = $value; + foreach ($this->states as $key => $state) { + if ($state instanceof Model) { + $states[$key . '_id'] = $state->id; + } else { + $states[$key] = $state; + } } + $data = array_merge($this->define(), $states); + + $model->fill($data); + $saved = $model->save(); if (!empty($this->child)) { @@ -55,7 +63,7 @@ public function create(): array|Model */ foreach ($this->child as $child) { $child->state([ - strtolower($className) . '_id' => $saved->id, + strtolower($className) => $saved, ])->create(); } } diff --git a/api/src/Factory/PhotoFactory.php b/api/src/Factory/PhotoFactory.php index ba37200..4e7ca71 100644 --- a/api/src/Factory/PhotoFactory.php +++ b/api/src/Factory/PhotoFactory.php @@ -12,8 +12,10 @@ protected function define(): array { $name = $this->generatePhoto(); + $user = $this->states['user'] ?? User::factory()->create(); + return [ - 'user_id' => User::factory()->create()->id, + 'user_id' => $user->id, 'name' => $name, ]; } @@ -29,10 +31,10 @@ private function generatePhoto(): string if ($this->userFaker) { $url = faker()->imageUrl(); } else { - $photos = array_diff( + $photos = array_values(array_diff( scandir(BASE_PATH . "/database/data_preset/photos/"), [".", ".."] // For remove "." and ".." directory (errno=21) - ); + )); $url = BASE_PATH . "/database/data_preset/photos/" diff --git a/api/src/Factory/PreferenceFactory.php b/api/src/Factory/PreferenceFactory.php index 796d7ad..a8403dd 100644 --- a/api/src/Factory/PreferenceFactory.php +++ b/api/src/Factory/PreferenceFactory.php @@ -8,12 +8,12 @@ class PreferenceFactory extends Factory { protected function define(): array { - $newUser = User::factory()->create(); + $user = $this->states['user'] ?? User::factory()->create(); return [ - "user_id" => $newUser->id, - "age_maximum" => rand($newUser->getAge(), 60), - "age_minimum" => rand(18, $newUser->getAge()), + "user_id" => $user->id, + "age_maximum" => rand($user->getAge(), 60), + "age_minimum" => rand(18, $user->getAge()), "sexual_preferences" => faker()->randomElement(['A', 'M', 'F', 'O']), "distance_maximum" => rand(1, 20), "by_tags" => (bool)rand(0, 1), diff --git a/api/src/Model/User.php b/api/src/Model/User.php index 173b9f6..981d3a3 100644 --- a/api/src/Model/User.php +++ b/api/src/Model/User.php @@ -245,10 +245,6 @@ public function getPhotosUrl(): array public function getAge(): ?int { - if (is_null($this->birthday)) { - return null; - } - $birthDate = explode("-", $this->birthday); return (date("md", date("U", mktime(0, 0, 0, $birthDate[2], $birthDate[1], $birthDate[0]))) > date("md")