forked from eatvio/eatvio-infra
52 lines
1.6 KiB
PHP
52 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Models\BlogPost;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
use Illuminate\Http\File;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Faker\Provider\Youtube;
|
|
use Faker\Generator;
|
|
|
|
/**
|
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\BlogPost>
|
|
*
|
|
* @property Generator|Youtube $faker
|
|
*/
|
|
class BlogPostFactory extends Factory
|
|
{
|
|
protected $model = BlogPost::class;
|
|
|
|
/**
|
|
* Define the model's default state.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function definition()
|
|
{
|
|
$faker = \Faker\Factory::create();
|
|
$this->faker->addProvider(new Youtube($faker));
|
|
|
|
return [
|
|
'state' => $this->faker->randomElement(['active', 'archived']),
|
|
'image' => $this->generateImageFile('blog_post'),
|
|
'description' => $this->faker->realText($this->faker->numberBetween(100, 200)),
|
|
'youtube_url' => $this->faker->youtubeRandomUri(),
|
|
'title' => $this->faker->realText($this->faker->numberBetween(10, 20)),
|
|
];
|
|
}
|
|
|
|
private function generateImageFile(string $name): 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/blogPosts/' . $images[$imageFileKey];
|
|
$file = new File($imageFilePath);
|
|
$fileLocalPath = Storage::putFile($name, $file, 'public');
|
|
|
|
return $fileLocalPath;
|
|
}
|
|
}
|