Skip to content

Change from Loader (load only) to Storage (load and save) #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/workflow-storage.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

return [
'databaseLoader' => [
'databaseStorage' => [
'tableNames' => [
'workflows' => 'workflows',
'workflow_states' => 'workflow_states',
'workflow_transitions' => 'workflow_transitions',
'workflow_state_transitions' => 'workflow_state_transitions',
],
//'loaderClass' => \Soap\WorkflowStorage\DatabaseLoader::class,
'loaderClass' => \Soap\WorkflowStorage\DatabaseStorage::class,
],
];
2 changes: 1 addition & 1 deletion database/factories/WorkflowFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class WorkflowFactory extends Factory
{
protected $model = Workflow::class;

public function definition()
public function definition(): array
{
return [
'name' => $this->faker->name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Soap\WorkflowStorage\Contracts;

interface WorkflowLoader
interface WorkflowDatabaseStorage extends WorkflowStorage
{
public function load(string $workflowName): array;

public function getWorkflowTableName(): string;

public function getWorkflowStateTableName(): string;
Expand Down
10 changes: 10 additions & 0 deletions src/Contracts/WorkflowStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Soap\WorkflowStorage\Contracts;

interface WorkflowStorage
{
public function all(): array;

public function load(string $workflowName): array;
}
15 changes: 9 additions & 6 deletions src/DatabaseLoader.php → src/DatabaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Soap\WorkflowStorage;

use Soap\WorkflowStorage\Contracts\WorkflowLoader;
use Soap\WorkflowStorage\Models\Workflow;
use Soap\WorkflowStorage\Contracts\WorkflowDatabaseStorage;

class DatabaseLoader implements WorkflowLoader
class DatabaseStorage implements WorkflowDatabaseStorage
{
const KEY_TABLENAMES = 'tableNames';

Expand Down Expand Up @@ -51,9 +50,13 @@ public function getWorkflowStateTransitionTableName(): string

public function load(string $workflowName): array
{
$workflow = Workflow::with(['states', 'transitions'])->where('name', $workflowName)->first();
$config[$workflowName] = [];
$workflowConfig = [];

return $config;
return $workflowConfig;
}

public function all(): array
{
return [];
}
}
4 changes: 2 additions & 2 deletions src/Models/Workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Soap\WorkflowStorage\DatabaseLoader;
use Soap\WorkflowStorage\DatabaseStorage;
use Soap\WorkflowStorage\Enums\WorkflowTypeEnum;

class Workflow extends Model
Expand All @@ -22,7 +22,7 @@ class Workflow extends Model

public function getTable(): string
{
return app(DatabaseLoader::class)->getWorkflowTableName();
return app(DatabaseStorage::class)->getWorkflowTableName();
}

public function states(): HasMany
Expand Down
4 changes: 2 additions & 2 deletions src/Models/WorkflowState.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Soap\WorkflowStorage\DatabaseLoader;
use Soap\WorkflowStorage\DatabaseStorage;

class WorkflowState extends Model
{
Expand All @@ -21,7 +21,7 @@ class WorkflowState extends Model

public function getTable(): string
{
return app(DatabaseLoader::class)->getWorkflowStateTableName();
return app(DatabaseStorage::class)->getWorkflowStateTableName();
}

public function workflow(): BelongsTo
Expand Down
9 changes: 2 additions & 7 deletions src/Models/WorkflowStateTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Soap\WorkflowStorage\DatabaseLoader;
use Soap\WorkflowStorage\DatabaseStorage;

class WorkflowStateTransition extends Model
{
Expand All @@ -19,7 +19,7 @@ class WorkflowStateTransition extends Model

public function getTable(): string
{
return app(DatabaseLoader::class)->getWorkflowStateTransitionTableName();
return app(DatabaseStorage::class)->getWorkflowStateTransitionTableName();
}

public function transition(): BelongsTo
Expand All @@ -31,9 +31,4 @@ public function fromState(): BelongsTo
{
return $this->belongsTo(WorkflowState::class, 'from_state_id');
}

public function toState(): BelongsTo
{
return $this->belongsTo(WorkflowState::class, 'to_state_id');
}
}
4 changes: 2 additions & 2 deletions src/Models/WorkflowTransition.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Soap\WorkflowStorage\DatabaseLoader;
use Soap\WorkflowStorage\DatabaseStorage;

class WorkflowTransition extends Model
{
Expand All @@ -20,7 +20,7 @@ class WorkflowTransition extends Model

public function getTable(): string
{
return app(DatabaseLoader::class)->getWorkflowTransitionTableName();
return app(DatabaseStorage::class)->getWorkflowTransitionTableName();
}

public function workflow(): BelongsTo
Expand Down
39 changes: 36 additions & 3 deletions src/Repositories/WorkflowRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,46 @@

class WorkflowRepository
{
public function all()
protected $model;

public function __construct(Workflow $model)
{
return [];
$this->model = $model;
}

public function find($id)
{
//$workflow = Workflow::with(['transitions.stateTransitions', 'states'])->find($id);
$workflow = $this->model->with(['transitions', 'states'])->find($id);
$places = $workflow->states->map(function ($state) {
return $state->name;
})->toArray();
$transitions = $workflow->transitions->map(function ($transition) {
return [
$transition->name => [
'from' => $transition->fromStates->map(function ($transitionState) {
return $transitionState->fromState->name;
})->toArray(),
'to' => $transition->toState->name,
],
];
})->toArray();

return [
$workflow->name => [
'supports' => $workflow->supports,
'places' => $places,
'transitions' => $transitions,
],
];
}

public function findByName(string $name): array
{
$workflowId = $this->model->where('name', $name)->first()->id;
if (! $workflowId) {
return [];
}

return $this->find($workflowId);
}
}
8 changes: 4 additions & 4 deletions src/WorkflowStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

namespace Soap\WorkflowStorage;

use Soap\WorkflowStorage\Contracts\WorkflowLoader;
use Soap\WorkflowStorage\Contracts\WorkflowStorage as WorkflowStorageContract;

class WorkflowStorage
{
protected array $loaders = [];

public function __construct(?WorkflowLoader $loader)
public function __construct(?WorkflowStorageContract $loader)
{
if ($loader) {
$this->registerLoader($loader);
}
}

public function registerLoader(WorkflowLoader $loader)
public function registerLoader(WorkflowStorageContract $loader)
{
$this->loaders[$loader::class] = $loader;
}

public function getLoader(string $loader): WorkflowLoader
public function getLoader(string $loader): WorkflowStorageContract
{
return $this->loaders[$loader];
}
Expand Down
8 changes: 4 additions & 4 deletions src/WorkflowStorageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public function configurePackage(Package $package): void
public function packageRegistered()
{
$this->app->singleton('workflow-storage', function ($app) {
$workflowStorage = new WorkflowStorage($app->make(DatabaseLoader::class));
$workflowStorage = new WorkflowStorage($app->make(DatabaseStorage::class));
});

$this->app->singleton(DatabaseLoader::class, function ($app) {
$config = $app->make('config')->get('workflow-storage.databaseLoader', []);
$this->app->singleton(DatabaseStorage::class, function ($app) {
$config = $app->make('config')->get('workflow-storage.databaseStorage', []);

return new DatabaseLoader(config: $config);
return new DatabaseStorage(config: $config);
});

}
Expand Down
21 changes: 16 additions & 5 deletions tests/Feature/WorkflowStorageTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

use Illuminate\Support\Arr;
use Soap\WorkflowStorage\Models\Workflow;
use Soap\WorkflowStorage\Repositories\WorkflowRepository;

beforeEach(function () {

$workflow = Workflow::create([
'name' => 'Test Workflow',
'name' => 'test_workflow',
'type' => 'workflow',
'description' => 'Test Workflow Description',
'supports' => [],
'supports' => ['App\Models\Article'],
'metadata' => [],
]);

Expand Down Expand Up @@ -43,17 +45,26 @@
]);
});

test('data can be retrieved from the database', function () {
test('workflow data can be retrieved via models from the database', function () {
$workflow = Workflow::first();
$states = $workflow->states;
$transitions = $workflow->transitions;

expect($workflow->name)->toBe('Test Workflow');
expect($workflow->name)->toBe('test_workflow');
expect($workflow->type->value)->toBe('workflow');
expect($workflow->description)->toBe('Test Workflow Description');
expect($workflow->supports)->toBe([]);
expect($workflow->supports)->toBe(['App\Models\Article']);
expect($workflow->metadata)->toBe([]);

expect($states->count())->toBe(4);
expect($transitions->count())->toBe(1);
});

test('workflow configuration can be retrievd via the repository', function () {
$repo = app()->make(WorkflowRepository::class);
$config = $repo->find(1);
ray($config);
expect(count($config))->toBe(1);
expect(count(Arr::get($config, 'test_workflow.places')))->toBe(4);
expect(count(Arr::get($config, 'test_workflow.transitions')))->toBe(1);
});
20 changes: 20 additions & 0 deletions workbench/app/Models/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Workbench\App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Workbench\AppModels\User;
use ZeroDaHero\LaravelWorkflow\Traits\WorkflowTrait;

class Order extends Model
{
protected $guarded = ['id'];

use WorkflowTrait;

public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
28 changes: 28 additions & 0 deletions workbench/database/factories/ArticleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Workbench\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class ArticleFactory extends Factory
{
protected $model = Order::class;

public function definition()
{
return [
'title' => $this->faker->sentence(),
];
}

public function forUser(int|User $user)
{
$userId = is_object($user) ? $user->id : $user;

return $this->state(function (array $attributes) {
return [
'user_id' => $userId,
];
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('state')->default('draft');
$table->string('intro_image')->nullable();
$table->string('content_image')->nullable();
$table->text('intro_text')->nullable();
$table->text('content')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->timestamps();
});
}
};