Files
eatvio-infra/database/factories/PlanFactory.php

86 lines
2.9 KiB
PHP
Executable File

<?php
namespace Database\Factories;
use FakerRestaurant\Provider\en_US\Restaurant;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\File;
use Faker\Generator;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Plan>
* @property Generator|Restaurant $faker
*/
class PlanFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*
*/
public function definition(): array
{
$faker = \Faker\Factory::create();
$this->faker->addProvider(new Restaurant($faker));
$active = $this->faker->boolean();
$typeBody = [
'fit' => $this->faker->boolean(),
'slim' => $this->faker->boolean(),
'behavior' => $this->faker->boolean(),
'wellness' => $this->faker->boolean(),
];
$wrongTypeBody = json_encode($typeBody);
$packetsOptions1 = [
'items' => [$this->faker->fruitName(), $this->faker->meatName(), $this->faker->sauceName()],
'price' => $this->faker->randomNumber(),
'short' => $this->faker->foodName(),
'description' => $this->faker->text(),
];
$packetsOptions2 = [
'items' => [$this->faker->fruitName(), $this->faker->meatName(), $this->faker->sauceName()],
'price' => $this->faker->randomNumber(),
'short' => $this->faker->foodName(),
'description' => $this->faker->text(),
];
$packetsOptions3 = [
'items' => [$this->faker->fruitName(), $this->faker->meatName(), $this->faker->sauceName()],
'price' => $this->faker->randomNumber(),
'short' => $this->faker->foodName(),
'description' => $this->faker->text(),
];
return [
'name' => $this->faker->foodName(),
'image' => $this->generateImageFile(),
'duration' => $this->faker->randomElement([7, 14, 21]),
'state' => $this->faker->randomElement(['active', 'draft', 'unactive']),
'type' => $typeBody,
'details' => $this->faker->text(),
'packets_count' => $active ? 3 : 1,
'packet1_options' => $packetsOptions1,
'packet2_options' => $active ? $packetsOptions2 : '',
'packet3_options' => $active ? $packetsOptions3 : '',
];
}
private function generateImageFile(string $name = 'plans'): string
{
$images = ['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '7.jpg', '8.jpg', '9.jpg', '10.jpg'];
Storage::fake($name);
$imageFileKey = $this->faker->numberBetween(0, 9);
$imageFilePath = 'database/assets/images/plans/' . $images[$imageFileKey];
$file = new File($imageFilePath);
$fileLocalPath = Storage::putFile($name, $file, 'public');
return $fileLocalPath;
}
}