Skip to content

Commit db58b8f

Browse files
authored
Merge pull request #184 from RonasIT/179-resource-exists-exception
[179]: throw directly ResourceAlreadyExistsException
2 parents a875233 + 2892f73 commit db58b8f

15 files changed

+71
-91
lines changed

src/Enums/ResourceTypeEnum.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Exceptions/ResourceAlreadyExistsException.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
namespace RonasIT\Support\Exceptions;
44

55
use Exception;
6-
use RonasIT\Support\Enums\ResourceTypeEnum;
6+
use Illuminate\Support\Str;
77

88
class ResourceAlreadyExistsException extends Exception
99
{
1010
public function __construct(
11-
protected string $entityName,
12-
protected ResourceTypeEnum $resourceType,
13-
protected ?string $entityNamespace = null,
11+
protected string $filePath,
1412
) {
15-
parent::__construct("Cannot create {$entityNamespace}{$resourceType->value} cause it already exists. Remove {$entityName}{$resourceType->value} and run command again.");
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, '/');
21+
22+
return Str::before($fileName, '.php');
1623
}
17-
}
24+
}

src/Generators/ControllerGenerator.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
namespace RonasIT\Support\Generators;
44

55
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6-
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
76
use RonasIT\Support\Exceptions\ClassNotExistsException;
87
use RonasIT\Support\Events\SuccessCreateMessage;
8+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
99

1010
class ControllerGenerator extends EntityGenerator
1111
{
1212
public function generate(): void
1313
{
1414
if ($this->classExists('controllers', "{$this->model}Controller")) {
15-
$this->throwFailureException(
16-
ClassAlreadyExistsException::class,
17-
"Cannot create {$this->model}Controller cause {$this->model}Controller already exists.",
18-
"Remove {$this->model}Controller.",
19-
);
15+
$path = $this->getClassPath('controllers', "{$this->model}Controller");
16+
17+
throw new ResourceAlreadyExistsException($path);
2018
}
2119

2220
if (!$this->classExists('services', "{$this->model}Service")) {

src/Generators/EntityGenerator.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,16 @@ abstract public function generate(): void;
145145

146146
protected function classExists(string $path, string $name, ?string $subFolder = null): bool
147147
{
148-
$entitiesPath = $this->paths[$path];
148+
$classPath = $this->getClassPath($path, $name, $subFolder);
149149

150-
if (!empty($subFolder)) {
151-
$entitiesPath = "{$entitiesPath}/{$subFolder}";
152-
}
150+
return file_exists($classPath);
151+
}
153152

154-
$classPath = base_path("{$entitiesPath}/{$name}.php");
153+
protected function getClassPath(string $path, string $name, ?string $subFolder = null): string
154+
{
155+
$path = $this->getPath($path, $subFolder);
155156

156-
return file_exists($classPath);
157+
return base_path("{$path}/{$name}.php");
157158
}
158159

159160
protected function saveClass($path, $name, $content, ?string $entityFolder = null): string

src/Generators/FactoryGenerator.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use InvalidArgumentException;
99
use RonasIT\Support\Exceptions\FakerMethodNotFoundException;
1010
use RonasIT\Support\Exceptions\ClassNotExistsException;
11-
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
1211
use RonasIT\Support\Events\SuccessCreateMessage;
12+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1313

1414
class FactoryGenerator extends EntityGenerator
1515
{
@@ -37,11 +37,9 @@ public function generate(): void
3737
}
3838

3939
if ($this->classExists('factories', "{$this->model}Factory")) {
40-
$this->throwFailureException(
41-
exceptionClass: ClassAlreadyExistsException::class,
42-
failureMessage: "Cannot create {$this->model}Factory cause {$this->model}Factory already exists.",
43-
recommendedMessage: "Remove {$this->model}Factory.",
44-
);
40+
$path = $this->getClassPath('factories', "{$this->model}Factory");
41+
42+
throw new ResourceAlreadyExistsException($path);
4543
}
4644

4745
if (!$this->isStubExists('factory')) {

src/Generators/ModelGenerator.php

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

55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Str;
7-
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
87
use RonasIT\Support\Exceptions\ClassNotExistsException;
98
use RonasIT\Support\Events\SuccessCreateMessage;
9+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1010

1111
class ModelGenerator extends EntityGenerator
1212
{
@@ -18,12 +18,9 @@ class ModelGenerator extends EntityGenerator
1818
public function generate(): void
1919
{
2020
if ($this->classExists('models', $this->model, $this->modelSubFolder)) {
21-
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
22-
$this->throwFailureException(
23-
exceptionClass: ClassAlreadyExistsException::class,
24-
failureMessage: "Cannot create {$this->model} Model cause {$this->model} Model already exists.",
25-
recommendedMessage: "Remove {$this->model} Model.",
26-
);
21+
$path = $this->getClassPath('models', $this->model, $this->modelSubFolder);
22+
23+
throw new ResourceAlreadyExistsException($path);
2724
}
2825

2926
if ($this->isStubExists('model') && (!$this->hasRelations() || $this->isStubExists('relation', 'model'))) {

src/Generators/NovaResourceGenerator.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Illuminate\Support\Facades\DB;
88
use Laravel\Nova\NovaServiceProvider;
99
use RonasIT\Support\Events\SuccessCreateMessage;
10-
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
1110
use RonasIT\Support\Exceptions\ClassNotExistsException;
11+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1212
use RonasIT\Support\Support\CommandLineNovaField;
1313
use RonasIT\Support\Support\DatabaseNovaField;
1414

@@ -65,12 +65,9 @@ public function generate(): void
6565
}
6666

6767
if ($this->classExists('nova', "{$this->model}Resource")) {
68-
// TODO: pass $this->modelSubfolder to Exception after refactoring in https://github.com/RonasIT/laravel-entity-generator/issues/179
69-
$this->throwFailureException(
70-
ClassAlreadyExistsException::class,
71-
"Cannot create {$this->model}Resource cause {$this->model}Resource already exists.",
72-
"Remove {$this->model}Resource."
73-
);
68+
$path = $this->getClassPath('nova', "{$this->model}Resource", $this->modelSubFolder);
69+
70+
throw new ResourceAlreadyExistsException($path);
7471
}
7572

7673
if (!$this->isStubExists('nova_resource')) {

src/Generators/NovaTestGenerator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use RecursiveIteratorIterator;
1414
use RecursiveDirectoryIterator;
1515
use Illuminate\Support\Arr;
16+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1617

1718
class NovaTestGenerator extends AbstractTestsGenerator
1819
{
@@ -31,11 +32,10 @@ public function generate(): void
3132
{
3233
if (class_exists(NovaServiceProvider::class)) {
3334
if ($this->classExists('nova', "Nova{$this->model}ResourceTest")) {
34-
$this->throwFailureException(
35-
ClassAlreadyExistsException::class,
36-
"Cannot create Nova{$this->model}ResourceTest cause it's already exist.",
37-
"Remove Nova{$this->model}ResourceTest."
38-
);
35+
36+
$path = $this->getClassPath('nova', "Nova{$this->model}ResourceTest");
37+
38+
throw new ResourceAlreadyExistsException($path);
3939
}
4040

4141
$novaResources = $this->getCommonNovaResources();
@@ -178,7 +178,7 @@ protected function isFixtureNeeded($type): bool
178178
{
179179
return true;
180180
}
181-
181+
182182
protected function collectFilters(): array
183183
{
184184
$filtersFromFields = $this->getFiltersFromFields();

src/Generators/ResourceGenerator.php

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

55
use RonasIT\Support\Events\SuccessCreateMessage;
6-
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
6+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
77

88
class ResourceGenerator extends EntityGenerator
99
{
@@ -25,11 +25,9 @@ public function generateCollectionResource(): void
2525
$pluralName = $this->getPluralName($this->model);
2626

2727
if ($this->classExists('resources', "{$pluralName}CollectionResource")) {
28-
$this->throwFailureException(
29-
ClassAlreadyExistsException::class,
30-
"Cannot create {$pluralName}CollectionResource cause {$pluralName}CollectionResource already exists.",
31-
"Remove {$pluralName}CollectionResource."
32-
);
28+
$path = $this->getClassPath('resources', "{$pluralName}CollectionResource");
29+
30+
throw new ResourceAlreadyExistsException($path);
3331
}
3432

3533
$collectionResourceContent = $this->getStub('collection_resource', [
@@ -46,11 +44,9 @@ public function generateCollectionResource(): void
4644
public function generateResource(): void
4745
{
4846
if ($this->classExists('resources', "{$this->model}Resource")) {
49-
$this->throwFailureException(
50-
ClassAlreadyExistsException::class,
51-
"Cannot create {$this->model}Resource cause {$this->model}Resource already exists.",
52-
"Remove {$this->model}Resource."
53-
);
47+
$path = $this->getClassPath('resources', "{$this->model}Resource");
48+
49+
throw new ResourceAlreadyExistsException($path);
5450
}
5551

5652
$resourceContent = $this->getStub('resource', [

tests/ControllerGeneratorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
use Illuminate\Support\Facades\View;
77
use RonasIT\Support\Events\SuccessCreateMessage;
88
use RonasIT\Support\Events\WarningEvent;
9-
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
109
use RonasIT\Support\Exceptions\ClassNotExistsException;
10+
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
1111
use RonasIT\Support\Generators\ControllerGenerator;
1212
use RonasIT\Support\Tests\Support\ControllerGeneratorTest\ControllerGeneratorMockTrait;
1313

@@ -29,8 +29,8 @@ public function testControllerAlreadyExists()
2929
]);
3030

3131
$this->assertExceptionThrew(
32-
className: ClassAlreadyExistsException::class,
33-
message: 'Cannot create PostController cause PostController already exists. Remove PostController.',
32+
className: ResourceAlreadyExistsException::class,
33+
message: 'Cannot create PostController cause it already exists. Remove vfs://root/app/Http/Controllers/PostController.php and run command again.',
3434
);
3535

3636
app(ControllerGenerator::class)

0 commit comments

Comments
 (0)