Skip to content

Commit

Permalink
Add basic support for creating users
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Sep 11, 2024
1 parent ba5ffad commit 337b00f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
35 changes: 35 additions & 0 deletions src/Factories/Concerns/CreatesUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Aerni\Factory\Factories\Concerns;

use Illuminate\Support\Arr;
use Statamic\Contracts\Auth\User;
use Statamic\Facades\User as UserFacade;

trait CreatesUser
{
protected $model = User::class;

public function newModel(array $attributes = []): User
{
$user = UserFacade::make();

if ($id = Arr::pull($attributes, 'id')) {
$user->id($id);
}

if ($email = Arr::pull($attributes, 'email')) {
$user->email($email);
}

if ($password = Arr::pull($attributes, 'password')) {
$user->password($password);
}

if ($preferences = Arr::pull($attributes, 'preferences')) {
$user->preferences($preferences);
}

return $user->data($attributes);
}
}
14 changes: 9 additions & 5 deletions src/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Aerni\Factory\Factories\Concerns\WithSites;
use Illuminate\Database\Eloquent\Factories\Sequence;
use Illuminate\Database\Eloquent\Factories\CrossJoinSequence;
use Statamic\Contracts\Auth\User;

abstract class Factory
{
Expand Down Expand Up @@ -69,12 +70,12 @@ public function raw($attributes = []): array
}, range(1, $this->count));
}

public function makeOne($attributes = []): Entry|Term
public function makeOne($attributes = []): Entry|Term|User
{
return $this->count(null)->make($attributes);
}

public function make(array $attributes = []): Collection|Entry|Term
public function make(array $attributes = []): Collection|Entry|Term|User
{
if (! empty($attributes)) {
return $this->state($attributes)->make([]);
Expand All @@ -99,7 +100,7 @@ public function make(array $attributes = []): Collection|Entry|Term
return $instances;
}

public function createOne($attributes = []): Entry|Term
public function createOne($attributes = []): Entry|Term|User
{
return $this->count(null)->create($attributes);
}
Expand All @@ -123,7 +124,7 @@ public function createMany(int|iterable|null $records = null)

// TODO: Add createQuietly()

public function create(array $attributes = []): Collection|Entry|Term
public function create(array $attributes = []): Collection|Entry|Term|User
{
if (! empty($attributes)) {
return $this->state($attributes)->create();
Expand All @@ -149,7 +150,7 @@ public function lazy(array $attributes = [])
return fn () => $this->create($attributes);
}

protected function makeInstance(): Entry|Term
protected function makeInstance(): Entry|Term|User
{
return $this->newModel($this->getExpandedAttributes());
}
Expand Down Expand Up @@ -183,6 +184,7 @@ protected function expandAttributes(array $definition)
$attribute = $this->getRandomRecycledModel($attribute->modelName())?->id()
?? $attribute->recycle($this->recycle)->create()->id();
} elseif ($attribute instanceof Entry || $attribute instanceof Term) {
// TODO: Also support users
$attribute = $attribute->id();
}

Expand Down Expand Up @@ -242,6 +244,7 @@ public function recycle($model)
'recycle' => $this->recycle
->flatten()
->merge(
// TODO: Also support users
Collection::wrap(($model instanceof Entry || $model instanceof Term) ? func_get_args() : $model)
->flatten()
)
Expand Down Expand Up @@ -312,6 +315,7 @@ protected function getModelNameFromClass($class): string
return match (true) {
$class instanceof Entry => 'Entry'.'\\'.ucfirst($class->collectionHandle()).'\\'.ucfirst($class->blueprint()),
$class instanceof Term => 'Term'.'\\'.ucfirst($class->taxonomyHandle()).'\\'.ucfirst($class->blueprint()),
$class instanceof User => 'User',
$class instanceof self => $class->modelName(),
};
}
Expand Down

0 comments on commit 337b00f

Please sign in to comment.