diff --git a/composer.json b/composer.json index 18f8040..2443a27 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/ArFlowService.php b/src/ArFlowService.php index 4164e6d..cad489e 100755 --- a/src/ArFlowService.php +++ b/src/ArFlowService.php @@ -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 { @@ -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|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('/..')); } } diff --git a/src/Facades/ArFlow.php b/src/Facades/ArFlow.php index b936c97..2d1fec4 100644 --- a/src/Facades/ArFlow.php +++ b/src/Facades/ArFlow.php @@ -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 getStates(string $workflow) */ class ArFlow extends Facade diff --git a/tests/Models/Helyum.php b/tests/Models/Helyum.php new file mode 100644 index 0000000..5e5de35 --- /dev/null +++ b/tests/Models/Helyum.php @@ -0,0 +1,16 @@ +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(); @@ -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)); + } +}); diff --git a/tests/TestCase.php b/tests/TestCase.php index 596cd9c..3e46518 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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('/..')); + } }