Skip to content
16 changes: 16 additions & 0 deletions src/Exceptions/AbstractResourceException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace RonasIT\Support\Exceptions;

use Exception;
use Illuminate\Support\Str;

abstract class AbstractResourceException extends Exception
{
protected function getEntity(string $filePath): string
{
$fileName = Str::afterLast($filePath, '/');

return Str::before($fileName, '.php');
}
}
18 changes: 4 additions & 14 deletions src/Exceptions/ResourceAlreadyExistsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@

namespace RonasIT\Support\Exceptions;

use Exception;
use Illuminate\Support\Str;

class ResourceAlreadyExistsException extends Exception
class ResourceAlreadyExistsException extends AbstractResourceException
{
public function __construct(
protected string $filePath,
string $filePath,
) {
$entity = $this->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.");
}
}
15 changes: 15 additions & 0 deletions src/Exceptions/ResourceNotExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace RonasIT\Support\Exceptions;

class ResourceNotExistsException extends AbstractResourceException
{
public function __construct(
string $entity,
string $filePath,
) {
$resource = $this->getEntity($filePath);

parent::__construct("Cannot create {$entity} cause {$resource} does not exist. Create {$filePath} and run command again.");
}
}
9 changes: 1 addition & 8 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/Generators/EntityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -329,6 +330,15 @@ protected function checkResourceExists(string $path, string $resourceName, ?stri
}
}

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);
}
}

protected function getRelationName(string $relation, string $type): string
{
$relationName = Str::snake($relation);
Expand Down
10 changes: 1 addition & 9 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");

Expand Down
10 changes: 1 addition & 9 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,14 +57,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);

Expand Down
10 changes: 2 additions & 8 deletions src/Generators/NovaResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down
10 changes: 1 addition & 9 deletions src/Generators/RepositoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
9 changes: 1 addition & 8 deletions src/Generators/ServiceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
12 changes: 6 additions & 6 deletions tests/ControllerGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 8 additions & 9 deletions tests/FactoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions tests/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions tests/NovaResourceGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions tests/RepositoryGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions tests/ServiceGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand Down