Skip to content

Commit

Permalink
Refactor Faker class and add new methods for seeding and instance cre…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
ewilan-riviere committed Jul 4, 2024
1 parent 423e279 commit 72cb14a
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/Utils/Faker.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public function __construct(
protected ?FakerMediaLocal $mediaLocal = null,
protected ?FakerMediaDownloader $mediaDownloader = null,
protected ?FakerJson $json = null,
) {}
) {
}

/**
* Clear `storage/app/public` and `storage/app/download` directories before seeding.
*/
public static function beforeSeed(): bool
{
$paths = [
Expand All @@ -50,12 +54,18 @@ public static function beforeSeed(): bool
return true;
}

/**
* Run `scout:fresh` and `media:clean` commands after seeding.
*/
public static function afterSeed(): void
{
Artisan::call(ScoutFreshCommand::class);
Artisan::call(MediaCleanCommand::class, ['--force' => true]);
}

/**
* Create a new instance of the service.
*/
public static function make(string|\UnitEnum|null $mediaPath = null): self
{
$generator = \Faker\Factory::create();
Expand All @@ -70,6 +80,9 @@ public static function make(string|\UnitEnum|null $mediaPath = null): self
return $service;
}

/**
* Create a new instance of the service without search.
*/
public static function noSearch(string $model, Closure $closure): mixed
{
$item = ClassParserItem::make($model);
Expand All @@ -81,6 +94,9 @@ public static function noSearch(string $model, Closure $closure): mixed
return $model::withoutSyncingToSearch(fn () => $closure());
}

/**
* `text()` and `richText()` factories.
*/
public function useText(?FakerTextEnum $type = null): self
{
$this->text = $this->setFakerText($type);
Expand All @@ -89,21 +105,41 @@ public function useText(?FakerTextEnum $type = null): self
return $this;
}

/**
* Get the generator instance.
*/
public function generator(): Generator
{
return $this->generator;
}

/**
* @deprecated Use `generator()` method instead.
*/
public function faker(): Generator
{
return $this->generator();
}

/**
* Get the text factory instance.
*/
public function text(): FakerText
{
return $this->text;
}

/**
* Get the rich text factory instance.
*/
public function richText(): FakerRichText
{
return $this->richText;
}

/**
* Get the date time factory instance.
*/
public function dateTime(): FakerDateTime
{
return $this->dateTime;
Expand Down

0 comments on commit 72cb14a

Please sign in to comment.