Skip to content
Open
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
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
],
"require": {
"php": "^8.2",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^10.0"
"illuminate/contracts": "^10.0",
"spatie/laravel-model-info": "^1.4",
"spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down
45 changes: 32 additions & 13 deletions src/ArFlowService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace AuroraWebSoftware\ArFlow;

use AuroraWebSoftware\ArFlow\Contacts\StateableModelContract;
use AuroraWebSoftware\ArFlow\Exceptions\StateNotFoundException;
use AuroraWebSoftware\ArFlow\Exceptions\WorkflowNotFoundException;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Config;
use Spatie\ModelInfo\ModelFinder;

class ArFlowService
{
Expand Down Expand Up @@ -35,20 +36,38 @@ public function getStates(string $workflow): array

public function getSupportedModelTypes(string $workflow): array
{
return [];
// workflowun supportded olduğu model ler
// https://github.com/spatie/laravel-model-info
// todo akif
$modelClasses = ModelFinder::all(
$this->getTestSupportDirectory(),
$this->getTestDirectory(),
"AuroraWebSoftware\ArFlow",
);

$supportedModelTypes = [];

foreach ($modelClasses as $modelClass) {
if (in_array(StateableModelContract::class, class_implements($modelClass))) {
if (in_array($workflow, $modelClass::supportedWorkflows())) {
$supportedModelTypes[] = $modelClass;
}
}
}

return $supportedModelTypes;

}

/**
* @param class-string $modelType
* @return Collection<int, Model>|null
*/
public function getModelInstances(string $workflow, string $modelType): ?Collection
public function getModelInstances(string $workflow, string $modelType): Collection
{
return $modelType::where('workflow', $workflow)->get();
}

public function getTestSupportDirectory(string $suffix = ''): string
{
return __DIR__.$suffix;
}

public function getTestDirectory(): string|false
{
return null;
// workflow u kullanan modeller
// todo akif
return realpath($this->getTestSupportDirectory('/..'));
}
}
3 changes: 3 additions & 0 deletions src/Facades/ArFlow.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace AuroraWebSoftware\ArFlow\Facades;

use AuroraWebSoftware\ArFlow\ArFlowService;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Facade;

/**
* @see ArFlowService
*
* @method Collection getModelInstances(string $workflow, string $modelType)
* @method array getSupportedModelTypes(string $workflow)
* @method static array<string> getStates(string $workflow)
*/
class ArFlow extends Facade
Expand Down
16 changes: 16 additions & 0 deletions tests/Models/Helyum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace AuroraWebSoftware\ArFlow\Tests\Models;

use AuroraWebSoftware\ArFlow\Traits\HasState;
use Illuminate\Database\Eloquent\Model;

class Helyum extends Model
{
use HasState;

public static function supportedWorkflows(): array
{
return ['workflow3'];
}
}
54 changes: 54 additions & 0 deletions tests/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@
$table->timestamps();
});

Schema::create('helyums', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->arflow();
$table->timestamps();
});
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->timestamps();
Expand Down Expand Up @@ -373,3 +379,51 @@
->toContain('todo', 'in_progress', 'done', 'cancelled', 'in_review', 'on_going')
->toHaveCount(6);
});

it('can get retrieve all rows of a given workflow in a model using Facade', function () {
$name = 'name1';
$workflow = 'workflow1';
$initalState = 'todo';

$name2 = 'name2';
$workflow2 = 'workflow3';

$name3 = 'name3';

/**
* @var StateableModelContract & Model $modelInstance
*/
$modelInstance = Stateable::create(
['name' => $name]
);
$modelInstance2 = Stateable::create(
['name' => $name2]
);
$modelInstance3 = Stateable::create(
['name' => $name3]
);

$modelInstance->applyWorkflow($workflow);
$modelInstance2->applyWorkflow($workflow2);
$modelInstance3->applyWorkflow($workflow2);

$models = ArFlow::getModelInstances($workflow2, Stateable::class);
foreach ($models as $row) {
$this->assertEquals($row->workflow, $workflow2);
}
});

it('can get all models that support a workflow using Facade', function () {
$workflow = 'workflow1';

$supportedModels = ArFlow::getSupportedModelTypes($workflow);

if (count($supportedModels) != 0) {
foreach ($supportedModels as $model) {
$instance = new $model();
$this->assertTrue(in_array($workflow, $instance::supportedWorkflows()));
}
} else {
$this->assertEquals(0, count($supportedModels));
}
});
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ public function getEnvironmentSetUp($app)
config()->set('database.default', 'mysql');

}

public function getTestSupportDirectory(string $suffix = ''): string
{
return __DIR__.$suffix;
}

public function getTestDirectory(): string
{
return realpath($this->getTestSupportDirectory('/..'));
}
}