diff --git a/src/Exceptions/AbstractResourceException.php b/src/Exceptions/AbstractResourceException.php new file mode 100644 index 0000000..32ad389 --- /dev/null +++ b/src/Exceptions/AbstractResourceException.php @@ -0,0 +1,16 @@ +getEntity(); - - parent::__construct("Cannot create {$entity} cause it already exists. Remove {$this->filePath} and run command again."); - } - - protected function getEntity(): string - { - $fileName = Str::afterLast($this->filePath, '/'); + $entity = $this->getEntity($filePath); - return Str::before($fileName, '.php'); + parent::__construct("Cannot create {$entity} cause it already exists. Remove {$filePath} and run command again."); } } diff --git a/src/Exceptions/ResourceNotExistsException.php b/src/Exceptions/ResourceNotExistsException.php new file mode 100644 index 0000000..5aecb27 --- /dev/null +++ b/src/Exceptions/ResourceNotExistsException.php @@ -0,0 +1,15 @@ +getEntity($filePath); + + parent::__construct("Cannot create {$entity} cause {$resource} does not exist. Create {$filePath} and run command again."); + } +} diff --git a/src/Generators/ControllerGenerator.php b/src/Generators/ControllerGenerator.php index 8b492b2..5914cdf 100644 --- a/src/Generators/ControllerGenerator.php +++ b/src/Generators/ControllerGenerator.php @@ -3,7 +3,6 @@ namespace RonasIT\Support\Generators; use Illuminate\Contracts\Filesystem\FileNotFoundException; -use RonasIT\Support\Exceptions\ClassNotExistsException; use RonasIT\Support\Events\SuccessCreateMessage; class ControllerGenerator extends EntityGenerator @@ -12,13 +11,7 @@ public function generate(): void { $this->checkResourceExists('controllers', "{$this->model}Controller"); - if (!$this->classExists('services', "{$this->model}Service")) { - $this->throwFailureException( - ClassNotExistsException::class, - "Cannot create {$this->model}Controller cause {$this->model}Service does not exists.", - "Create a {$this->model}Service by himself.", - ); - } + $this->checkResourceNotExists('services', "{$this->model}Controller", "{$this->model}Service"); if (!$this->isStubExists('controller')) { return; diff --git a/src/Generators/EntityGenerator.php b/src/Generators/EntityGenerator.php index 764f1b5..3788aa1 100644 --- a/src/Generators/EntityGenerator.php +++ b/src/Generators/EntityGenerator.php @@ -13,6 +13,7 @@ use RonasIT\Support\Events\WarningEvent; use Illuminate\Database\Eloquent\Relations\BelongsTo; use RonasIT\Support\Exceptions\ClassNotExistsException; +use RonasIT\Support\Exceptions\ResourceNotExistsException; use RonasIT\Support\Exceptions\IncorrectClassPathException; use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; @@ -328,4 +329,13 @@ protected function checkResourceExists(string $path, string $resourceName, ?stri throw new ResourceAlreadyExistsException($filePath); } } + + protected function checkResourceNotExists(string $path, string $entity, string $resourceName, ?string $subFolder = null): void + { + if (!$this->classExists($path, $resourceName, $subFolder)) { + $filePath = $this->getClassPath($path, $resourceName, $subFolder); + + throw new ResourceNotExistsException($entity, $filePath); + } + } } diff --git a/src/Generators/FactoryGenerator.php b/src/Generators/FactoryGenerator.php index 26cd576..cae1dc4 100644 --- a/src/Generators/FactoryGenerator.php +++ b/src/Generators/FactoryGenerator.php @@ -7,7 +7,6 @@ use Illuminate\Support\Str; use InvalidArgumentException; use RonasIT\Support\Exceptions\FakerMethodNotFoundException; -use RonasIT\Support\Exceptions\ClassNotExistsException; use RonasIT\Support\Events\SuccessCreateMessage; class FactoryGenerator extends EntityGenerator @@ -26,14 +25,7 @@ class FactoryGenerator extends EntityGenerator public function generate(): void { - if (!$this->classExists('models', $this->model, $this->modelSubFolder)) { - // TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179 - $this->throwFailureException( - exceptionClass: ClassNotExistsException::class, - failureMessage: "Cannot create {$this->model}Factory cause {$this->model} Model does not exists.", - recommendedMessage: "Create a {$this->model} Model by itself or run command 'php artisan make:entity {$this->model} --only-model'.", - ); - } + $this->checkResourceNotExists('models', "{$this->model}Factory", $this->model, $this->modelSubFolder); $this->checkResourceExists('factories', "{$this->model}Factory"); diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 4ca0329..69a11cf 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -4,7 +4,6 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; -use RonasIT\Support\Exceptions\ClassNotExistsException; use RonasIT\Support\Events\SuccessCreateMessage; class ModelGenerator extends EntityGenerator @@ -63,14 +62,7 @@ public function prepareRelatedModels(): void foreach ($this->relations as $type => $relationsByType) { foreach ($relationsByType as $relation) { - if (!$this->classExists('models', $relation)) { - // TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179 - $this->throwFailureException( - exceptionClass: ClassNotExistsException::class, - failureMessage: "Cannot create {$this->model} Model cause relation model {$relation} does not exist.", - recommendedMessage: "Create the {$relation} Model by himself or run command 'php artisan make:entity {$relation} --only-model'.", - ); - } + $this->checkResourceNotExists('models', $this->model, $relation); $content = $this->getModelContent($relation); diff --git a/src/Generators/NovaResourceGenerator.php b/src/Generators/NovaResourceGenerator.php index e3dce9e..02c10a3 100644 --- a/src/Generators/NovaResourceGenerator.php +++ b/src/Generators/NovaResourceGenerator.php @@ -8,6 +8,7 @@ use Laravel\Nova\NovaServiceProvider; use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Exceptions\ClassNotExistsException; +use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; use RonasIT\Support\Support\CommandLineNovaField; use RonasIT\Support\Support\DatabaseNovaField; @@ -54,14 +55,7 @@ class NovaResourceGenerator extends EntityGenerator public function generate(): void { if (class_exists(NovaServiceProvider::class)) { - if (!$this->classExists('models', $this->model, $this->modelSubFolder)) { - // TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179 - $this->throwFailureException( - ClassNotExistsException::class, - "Cannot create {$this->model} Nova resource cause {$this->model} Model does not exists.", - "Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'." - ); - } + $this->checkResourceNotExists('models', "{$this->model}Resource", $this->model, $this->modelSubFolder); $this->checkResourceExists('nova', "{$this->model}Resource", $this->modelSubFolder); diff --git a/src/Generators/RepositoryGenerator.php b/src/Generators/RepositoryGenerator.php index 32f31a5..b258dae 100644 --- a/src/Generators/RepositoryGenerator.php +++ b/src/Generators/RepositoryGenerator.php @@ -2,21 +2,13 @@ namespace RonasIT\Support\Generators; -use RonasIT\Support\Exceptions\ClassNotExistsException; use RonasIT\Support\Events\SuccessCreateMessage; class RepositoryGenerator extends EntityGenerator { public function generate(): void { - if (!$this->classExists('models', $this->model, $this->modelSubFolder)) { - // TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179 - $this->throwFailureException( - ClassNotExistsException::class, - "Cannot create {$this->model}Repository cause {$this->model} Model does not exists.", - "Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'." - ); - } + $this->checkResourceNotExists('models', "{$this->model}Repository", $this->model, $this->modelSubFolder); $this->checkResourceExists('repositories', "{$this->model}Repository"); diff --git a/src/Generators/ServiceGenerator.php b/src/Generators/ServiceGenerator.php index b82a5f4..2e93b5e 100644 --- a/src/Generators/ServiceGenerator.php +++ b/src/Generators/ServiceGenerator.php @@ -3,20 +3,13 @@ namespace RonasIT\Support\Generators; use Illuminate\Support\Arr; -use RonasIT\Support\Exceptions\ClassNotExistsException; use RonasIT\Support\Events\SuccessCreateMessage; class ServiceGenerator extends EntityGenerator { public function generate(): void { - if (!$this->classExists('repositories', "{$this->model}Repository")) { - $this->throwFailureException( - exceptionClass: ClassNotExistsException::class, - failureMessage: "Cannot create {$this->model}Service cause {$this->model}Repository does not exists.", - recommendedMessage: "Create a {$this->model}Repository by himself or run command 'php artisan make:entity {$this->model} --only-repository'.", - ); - } + $this->checkResourceNotExists('repositories', "{$this->model}Service", "{$this->model}Repository"); $this->checkResourceExists('services', "{$this->model}Service"); diff --git a/tests/ControllerGeneratorTest.php b/tests/ControllerGeneratorTest.php index 6c6fa7d..1d428f2 100644 --- a/tests/ControllerGeneratorTest.php +++ b/tests/ControllerGeneratorTest.php @@ -2,13 +2,13 @@ namespace RonasIT\Support\Tests; -use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Support\Facades\View; -use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Events\WarningEvent; -use RonasIT\Support\Exceptions\ClassNotExistsException; -use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; +use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Generators\ControllerGenerator; +use Illuminate\Contracts\Filesystem\FileNotFoundException; +use RonasIT\Support\Exceptions\ResourceNotExistsException; +use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; use RonasIT\Support\Tests\Support\ControllerGeneratorTest\ControllerGeneratorMockTrait; class ControllerGeneratorTest extends TestCase @@ -46,8 +46,8 @@ public function testModelServiceNotExists() ]); $this->assertExceptionThrew( - className: ClassNotExistsException::class, - message: 'Cannot create PostController cause PostService does not exists. Create a PostService by himself.', + className: ResourceNotExistsException::class, + message: 'Cannot create PostController cause PostService does not exist. Create app/Services/PostService.php and run command again.', ); app(ControllerGenerator::class) diff --git a/tests/FactoryGeneratorTest.php b/tests/FactoryGeneratorTest.php index 60f4617..d62034a 100644 --- a/tests/FactoryGeneratorTest.php +++ b/tests/FactoryGeneratorTest.php @@ -2,17 +2,17 @@ namespace RonasIT\Support\Tests; -use Illuminate\Support\Facades\Config; -use Illuminate\Support\Facades\Event; use Illuminate\View\ViewException; +use Illuminate\Support\Facades\Event; use RonasIT\Support\DTO\RelationsDTO; -use RonasIT\Support\Events\SuccessCreateMessage; +use Illuminate\Support\Facades\Config; use RonasIT\Support\Events\WarningEvent; -use RonasIT\Support\Exceptions\ClassNotExistsException; -use RonasIT\Support\Exceptions\IncorrectClassPathException; -use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; +use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Generators\FactoryGenerator; +use RonasIT\Support\Exceptions\ResourceNotExistsException; +use RonasIT\Support\Exceptions\IncorrectClassPathException; use RonasIT\Support\Tests\Support\Factory\FactoryMockTrait; +use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; class FactoryGeneratorTest extends TestCase { @@ -30,9 +30,8 @@ public function testModelNotExists() $this->mockFileSystemWithoutPostModel(); $this->assertExceptionThrew( - className: ClassNotExistsException::class, - message: "Cannot create PostFactory cause Post Model does not exists. " - . "Create a Post Model by itself or run command 'php artisan make:entity Post --only-model'.", + className: ResourceNotExistsException::class, + message: 'Cannot create PostFactory cause Post does not exist. Create app/Models/Post.php and run command again.', ); app(FactoryGenerator::class) diff --git a/tests/ModelGeneratorTest.php b/tests/ModelGeneratorTest.php index c69cc06..0568597 100755 --- a/tests/ModelGeneratorTest.php +++ b/tests/ModelGeneratorTest.php @@ -3,13 +3,13 @@ namespace RonasIT\Support\Tests; use RonasIT\Support\DTO\RelationsDTO; -use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Events\WarningEvent; -use RonasIT\Support\Exceptions\ClassNotExistsException; -use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; use RonasIT\Support\Generators\ModelGenerator; +use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Tests\Support\Model\ModelMockTrait; use Symfony\Component\Console\Exception\RuntimeException; +use RonasIT\Support\Exceptions\ResourceNotExistsException; +use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; class ModelGeneratorTest extends TestCase { @@ -46,9 +46,8 @@ public function testRelationModelMissing() ]); $this->assertExceptionThrew( - className: ClassNotExistsException::class, - message: "Cannot create Post Model cause relation model Comment does not exist. " - . "Create the Comment Model by himself or run command 'php artisan make:entity Comment --only-model'.", + className: ResourceNotExistsException::class, + message: 'Cannot create Post cause Comment does not exist. Create app/Models/Comment.php and run command again.', ); app(ModelGenerator::class) diff --git a/tests/NovaResourceGeneratorTest.php b/tests/NovaResourceGeneratorTest.php index 8995ac6..55964eb 100644 --- a/tests/NovaResourceGeneratorTest.php +++ b/tests/NovaResourceGeneratorTest.php @@ -2,13 +2,13 @@ namespace RonasIT\Support\Tests; -use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Events\WarningEvent; -use RonasIT\Support\Exceptions\ClassNotExistsException; -use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; +use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Generators\NovaResourceGenerator; -use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\NovaResourceGeneratorMockTrait; +use RonasIT\Support\Exceptions\ResourceNotExistsException; +use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\Post; +use RonasIT\Support\Tests\Support\NovaResourceGeneratorTest\NovaResourceGeneratorMockTrait; class NovaResourceGeneratorTest extends TestCase { @@ -42,9 +42,8 @@ public function testCreateNovaResourceWithMissingModel() $this->mockNovaServiceProviderExists(); $this->assertExceptionThrew( - className: ClassNotExistsException::class, - message: 'Cannot create Post Nova resource cause Post Model does not exists. ' - . "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'" + className: ResourceNotExistsException::class, + message: 'Cannot create PostResource cause Post does not exist. Create app/Models/Post.php and run command again.' ); app(NovaResourceGenerator::class) diff --git a/tests/RepositoryGeneratorTest.php b/tests/RepositoryGeneratorTest.php index 29b83e2..84474a3 100644 --- a/tests/RepositoryGeneratorTest.php +++ b/tests/RepositoryGeneratorTest.php @@ -4,9 +4,9 @@ use RonasIT\Support\Events\WarningEvent; use RonasIT\Support\Events\SuccessCreateMessage; -use RonasIT\Support\Generators\RepositoryGenerator; -use RonasIT\Support\Exceptions\ClassNotExistsException; use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; +use RonasIT\Support\Generators\RepositoryGenerator; +use RonasIT\Support\Exceptions\ResourceNotExistsException; use RonasIT\Support\Tests\Support\Repository\RepositoryMockTrait; class RepositoryGeneratorTest extends TestCase @@ -27,9 +27,8 @@ public function testModelNotExist() ]); $this->assertExceptionThrew( - className: ClassNotExistsException::class, - message: "Cannot create PostRepository cause Post Model does not exists. " - . "Create a Post Model by himself or run command 'php artisan make:entity Post --only-model'.", + className: ResourceNotExistsException::class, + message: 'Cannot create PostRepository cause Post does not exist. Create app/Models/Post.php and run command again.', ); app(RepositoryGenerator::class) diff --git a/tests/ServiceGeneratorTest.php b/tests/ServiceGeneratorTest.php index 6b140bf..50342ad 100644 --- a/tests/ServiceGeneratorTest.php +++ b/tests/ServiceGeneratorTest.php @@ -8,8 +8,8 @@ use RonasIT\Support\Events\SuccessCreateMessage; use RonasIT\Support\Generators\ServiceGenerator; use RonasIT\Support\Tests\Support\GeneratorMockTrait; -use RonasIT\Support\Exceptions\ClassNotExistsException; use RonasIT\Support\Exceptions\ResourceAlreadyExistsException; +use RonasIT\Support\Exceptions\ResourceNotExistsException; class ServiceGeneratorTest extends TestCase { @@ -22,9 +22,8 @@ public function testMissingRepository() ]); $this->assertExceptionThrew( - className: ClassNotExistsException::class, - message: 'Cannot create PostService cause PostRepository does not exists. ' - . "Create a PostRepository by himself or run command 'php artisan make:entity Post --only-repository'", + className: ResourceNotExistsException::class, + message: 'Cannot create PostService cause PostRepository does not exist. Create app/Repositories/PostRepository.php and run command again.', ); app(ServiceGenerator::class)