Skip to content

Commit

Permalink
simplify flows
Browse files Browse the repository at this point in the history
  • Loading branch information
nikuscs committed Jul 10, 2024
1 parent 3334706 commit 838993e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 57 deletions.
9 changes: 4 additions & 5 deletions database/migrations/create_flows_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Flavorly\LaravelFlows\Enums\FlowStatusEnum;

return new class extends Migration
{
public function up()
public function up(): void
{
Schema::create('flows', function (Blueprint $table) {
Schema::create('flows', callback: function (Blueprint $table) {
$table->id();
$table->uuid()->unique()->index();
$table->morphs('flowable');
$table->json('context')->nullable();
$table->string('status')->default(FlowStatusEnum::active->value);
$table->softDeletes();
$table->timestamps();
$table->unique(['flowable_id', 'flowable_type', 'deleted_at']);
});
}
};
11 changes: 5 additions & 6 deletions src/Concerns/Flowable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Flavorly\LaravelFlows\Concerns;

use Flavorly\LaravelFlows\Enums\FlowStatusEnum;
use Flavorly\LaravelFlows\Models\Flow;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;

/**
* @mixin \Illuminate\Database\Eloquent\Model
*/
trait Flowable
{
/**
Expand All @@ -16,7 +18,7 @@ trait Flowable
*/
public function flows(): MorphMany
{
return $this->morphMany(Flow::class, 'flowable');
return $this->morphMany(Flow::class, 'flowable')->withTrashed();
}

/**
Expand All @@ -26,9 +28,6 @@ public function flows(): MorphMany
*/
public function flow(): MorphOne
{
return $this
->morphOne(Flow::class, 'flowable')
->latestOfMany()
->where('status', FlowStatusEnum::active->value);
return $this->morphOne(Flow::class, 'flowable')->whereNull('deleted_at');
}
}
13 changes: 0 additions & 13 deletions src/Enums/FlowStatusEnum.php

This file was deleted.

7 changes: 1 addition & 6 deletions src/Listeners/FlowListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
namespace Flavorly\LaravelFlows\Listeners;

use Flavorly\LaravelFlows\Models\Flow;
use Illuminate\Support\Str;

final class FlowListener
{
public function creating(Flow $model): void
{
if ($model->uuid === null) {
$model->forceFill([
'uuid' => Str::uuid(),
]);
}
$model->flowable->flows()->whereNull('deleted_at')->delete();

Check failure on line 11 in src/Listeners/FlowListener.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Model::flows().
}

public function created(Flow $model): void {}
Expand Down
31 changes: 4 additions & 27 deletions src/Models/Flow.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@

namespace Flavorly\LaravelFlows\Models;

use Flavorly\LaravelFlows\Enums\FlowStatusEnum;
use Flavorly\LaravelFlows\Listeners\FlowListener;
use Illuminate\Database\Eloquent\Attributes\ObservedBy;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
* @property int $id
* @property string $uuid
* @property string $flowable_type
* @property int $flowable_id
* @property array $context
* @property \Flavorly\LaravelFlows\Enums\FlowStatusEnum $status
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property \Carbon\Carbon|null $deleted_at
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $flowable
*/
#[ObservedBy(FlowListener::class)]
final class Flow extends Model
{
use SoftDeletes;

/**
* Get the attributes that should be cast.
*
Expand All @@ -30,7 +31,6 @@ final class Flow extends Model
protected function casts(): array
{
return [
'status' => FlowStatusEnum::class,
'context' => 'array',
];
}
Expand All @@ -44,27 +44,4 @@ public function flowable(): MorphTo
{
return $this->morphTo();
}

/**
* Discard a flow
*/
public function discard(): Flow
{
$this->updateQuietly(['status' => FlowStatusEnum::discarded]);

return $this;
}

/**
* Clone the current flow and discards the current one
*/
public function renew(): Flow
{
$this->discard();
$flow = $this->replicate(['uuid']);
$flow->status = FlowStatusEnum::active;
$flow->save();

return $flow;
}
}

0 comments on commit 838993e

Please sign in to comment.