Skip to content

Commit bbb138c

Browse files
authored
Merge pull request #222 from RonasIT/add-resource-not-exists-exception
feat: add resource not found exception
2 parents 4b48984 + a85e956 commit bbb138c

17 files changed

+87
-112
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Exceptions;
4+
5+
use Exception;
6+
use Illuminate\Support\Str;
7+
8+
abstract class AbstractResourceException extends Exception
9+
{
10+
protected function getEntity(string $filePath): string
11+
{
12+
$fileName = Str::afterLast($filePath, '/');
13+
14+
return Str::before($fileName, '.php');
15+
}
16+
}

src/Exceptions/ResourceAlreadyExistsException.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,13 @@
22

33
namespace RonasIT\Support\Exceptions;
44

5-
use Exception;
6-
use Illuminate\Support\Str;
7-
8-
class ResourceAlreadyExistsException extends Exception
5+
class ResourceAlreadyExistsException extends AbstractResourceException
96
{
107
public function __construct(
11-
protected string $filePath,
8+
string $filePath,
129
) {
13-
$entity = $this->getEntity();
14-
15-
parent::__construct("Cannot create {$entity} cause it already exists. Remove {$this->filePath} and run command again.");
16-
}
17-
18-
protected function getEntity(): string
19-
{
20-
$fileName = Str::afterLast($this->filePath, '/');
10+
$entity = $this->getEntity($filePath);
2111

22-
return Str::before($fileName, '.php');
12+
parent::__construct("Cannot create {$entity} cause it already exists. Remove {$filePath} and run command again.");
2313
}
2414
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Exceptions;
4+
5+
class ResourceNotExistsException extends AbstractResourceException
6+
{
7+
public function __construct(
8+
string $createableResource,
9+
string $requiredFilePath,
10+
) {
11+
$resource = $this->getEntity($requiredFilePath);
12+
13+
parent::__construct("Cannot create {$createableResource} cause {$resource} does not exist. Create {$requiredFilePath} and run command again.");
14+
}
15+
}

src/Generators/ControllerGenerator.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace RonasIT\Support\Generators;
44

55
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6-
use RonasIT\Support\Exceptions\ClassNotExistsException;
76
use RonasIT\Support\Events\SuccessCreateMessage;
87

98
class ControllerGenerator extends EntityGenerator
@@ -12,13 +11,7 @@ public function generate(): void
1211
{
1312
$this->checkResourceExists('controllers', "{$this->model}Controller");
1413

15-
if (!$this->classExists('services', "{$this->model}Service")) {
16-
$this->throwFailureException(
17-
ClassNotExistsException::class,
18-
"Cannot create {$this->model}Controller cause {$this->model}Service does not exists.",
19-
"Create a {$this->model}Service by himself.",
20-
);
21-
}
14+
$this->checkResourceNotExists('services', "{$this->model}Controller", "{$this->model}Service");
2215

2316
if (!$this->isStubExists('controller')) {
2417
return;

src/Generators/EntityGenerator.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use RonasIT\Support\DTO\RelationsDTO;
1313
use RonasIT\Support\Events\WarningEvent;
1414
use Illuminate\Database\Eloquent\Relations\BelongsTo;
15-
use RonasIT\Support\Exceptions\ClassNotExistsException;
15+
use RonasIT\Support\Exceptions\ResourceNotExistsException;
1616
use RonasIT\Support\Exceptions\IncorrectClassPathException;
1717
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1818

@@ -214,11 +214,7 @@ protected function getRelatedModels(string $model, string $creatableClass): arra
214214
$modelClass = $this->getModelClass($model);
215215

216216
if (!class_exists($modelClass)) {
217-
$this->throwFailureException(
218-
exceptionClass: ClassNotExistsException::class,
219-
failureMessage: "Cannot create {$creatableClass} cause {$model} Model does not exists.",
220-
recommendedMessage: "Create a {$model} Model by himself or run command 'php artisan make:entity {$model} --only-model'.",
221-
);
217+
throw new ResourceNotExistsException($creatableClass, $model);
222218
}
223219

224220
$instance = new $modelClass();
@@ -329,6 +325,15 @@ protected function checkResourceExists(string $path, string $resourceName, ?stri
329325
}
330326
}
331327

328+
protected function checkResourceNotExists(string $path, string $createableResource, string $requiredResource, ?string $subFolder = null): void
329+
{
330+
if (!$this->classExists($path, $requiredResource, $subFolder)) {
331+
$filePath = $this->getClassPath($path, $requiredResource, $subFolder);
332+
333+
throw new ResourceNotExistsException($createableResource, $filePath);
334+
}
335+
}
336+
332337
protected function getRelationName(string $relation, string $type): string
333338
{
334339
$relationName = Str::snake($relation);

src/Generators/FactoryGenerator.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Support\Str;
88
use InvalidArgumentException;
99
use RonasIT\Support\Exceptions\FakerMethodNotFoundException;
10-
use RonasIT\Support\Exceptions\ClassNotExistsException;
1110
use RonasIT\Support\Events\SuccessCreateMessage;
1211

1312
class FactoryGenerator extends EntityGenerator
@@ -26,14 +25,7 @@ class FactoryGenerator extends EntityGenerator
2625

2726
public function generate(): void
2827
{
29-
if (!$this->classExists('models', $this->model, $this->modelSubFolder)) {
30-
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
31-
$this->throwFailureException(
32-
exceptionClass: ClassNotExistsException::class,
33-
failureMessage: "Cannot create {$this->model}Factory cause {$this->model} Model does not exists.",
34-
recommendedMessage: "Create a {$this->model} Model by itself or run command 'php artisan make:entity {$this->model} --only-model'.",
35-
);
36-
}
28+
$this->checkResourceNotExists('models', "{$this->model}Factory", $this->model, $this->modelSubFolder);
3729

3830
$this->checkResourceExists('factories', "{$this->model}Factory");
3931

src/Generators/ModelGenerator.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Str;
7-
use RonasIT\Support\Exceptions\ClassNotExistsException;
87
use RonasIT\Support\Events\SuccessCreateMessage;
98

109
class ModelGenerator extends EntityGenerator
@@ -58,14 +57,7 @@ public function prepareRelatedModels(): void
5857

5958
foreach ($this->relations as $type => $relationsByType) {
6059
foreach ($relationsByType as $relation) {
61-
if (!$this->classExists('models', $relation)) {
62-
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
63-
$this->throwFailureException(
64-
exceptionClass: ClassNotExistsException::class,
65-
failureMessage: "Cannot create {$this->model} Model cause relation model {$relation} does not exist.",
66-
recommendedMessage: "Create the {$relation} Model by himself or run command 'php artisan make:entity {$relation} --only-model'.",
67-
);
68-
}
60+
$this->checkResourceNotExists('models', $this->model, $relation);
6961

7062
$content = $this->getModelContent($relation);
7163

src/Generators/NovaResourceGenerator.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Support\Facades\DB;
88
use Laravel\Nova\NovaServiceProvider;
99
use RonasIT\Support\Events\SuccessCreateMessage;
10-
use RonasIT\Support\Exceptions\ClassNotExistsException;
1110
use RonasIT\Support\Support\CommandLineNovaField;
1211
use RonasIT\Support\Support\DatabaseNovaField;
1312

@@ -54,14 +53,7 @@ class NovaResourceGenerator extends EntityGenerator
5453
public function generate(): void
5554
{
5655
if (class_exists(NovaServiceProvider::class)) {
57-
if (!$this->classExists('models', $this->model, $this->modelSubFolder)) {
58-
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
59-
$this->throwFailureException(
60-
ClassNotExistsException::class,
61-
"Cannot create {$this->model} Nova resource cause {$this->model} Model does not exists.",
62-
"Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'."
63-
);
64-
}
56+
$this->checkResourceNotExists('models', "{$this->model}Resource", $this->model, $this->modelSubFolder);
6557

6658
$this->checkResourceExists('nova', "{$this->model}Resource", $this->modelSubFolder);
6759

src/Generators/RepositoryGenerator.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22

33
namespace RonasIT\Support\Generators;
44

5-
use RonasIT\Support\Exceptions\ClassNotExistsException;
65
use RonasIT\Support\Events\SuccessCreateMessage;
76

87
class RepositoryGenerator extends EntityGenerator
98
{
109
public function generate(): void
1110
{
12-
if (!$this->classExists('models', $this->model, $this->modelSubFolder)) {
13-
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
14-
$this->throwFailureException(
15-
ClassNotExistsException::class,
16-
"Cannot create {$this->model}Repository cause {$this->model} Model does not exists.",
17-
"Create a {$this->model} Model by himself or run command 'php artisan make:entity {$this->model} --only-model'."
18-
);
19-
}
11+
$this->checkResourceNotExists('models', "{$this->model}Repository", $this->model, $this->modelSubFolder);
2012

2113
$this->checkResourceExists('repositories', "{$this->model}Repository");
2214

src/Generators/ServiceGenerator.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@
33
namespace RonasIT\Support\Generators;
44

55
use Illuminate\Support\Arr;
6-
use RonasIT\Support\Exceptions\ClassNotExistsException;
76
use RonasIT\Support\Events\SuccessCreateMessage;
87

98
class ServiceGenerator extends EntityGenerator
109
{
1110
public function generate(): void
1211
{
13-
if (!$this->classExists('repositories', "{$this->model}Repository")) {
14-
$this->throwFailureException(
15-
exceptionClass: ClassNotExistsException::class,
16-
failureMessage: "Cannot create {$this->model}Service cause {$this->model}Repository does not exists.",
17-
recommendedMessage: "Create a {$this->model}Repository by himself or run command 'php artisan make:entity {$this->model} --only-repository'.",
18-
);
19-
}
12+
$this->checkResourceNotExists('repositories', "{$this->model}Service", "{$this->model}Repository");
2013

2114
$this->checkResourceExists('services', "{$this->model}Service");
2215

0 commit comments

Comments
 (0)