Skip to content
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
18 changes: 13 additions & 5 deletions api/src/Factory/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)) {
Expand All @@ -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();
}
}
Expand Down
8 changes: 5 additions & 3 deletions api/src/Factory/PhotoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
Expand All @@ -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/"
Expand Down
8 changes: 4 additions & 4 deletions api/src/Factory/PreferenceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 0 additions & 4 deletions api/src/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down