Skip to content

Commit c0cbfe2

Browse files
refactor(Command): refactor MakeResource command
1 parent cc9d8b6 commit c0cbfe2

File tree

1 file changed

+58
-19
lines changed

1 file changed

+58
-19
lines changed

src/Commands/MakeResource.php

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
namespace Eghamat24\DatabaseRepository\Commands;
44

5-
use Illuminate\Console\Command;
6-
use Illuminate\Support\Str;
75
use Eghamat24\DatabaseRepository\Creators\BaseCreator;
86
use Eghamat24\DatabaseRepository\Creators\CreatorResource;
97
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
8+
use Illuminate\Support\Collection;
109

1110
class MakeResource extends BaseCommand
1211
{
12+
use CustomMySqlQueries;
13+
14+
private const OBJECT_NAME = 'Resource';
15+
1316
/**
1417
* The name and signature of the console command.
1518
*
@@ -28,30 +31,59 @@ class MakeResource extends BaseCommand
2831
*/
2932
protected $description = 'Create new resource';
3033

31-
use CustomMySqlQueries;
32-
33-
/**
34-
* Execute the console command.
35-
*
36-
* @return int
37-
*/
3834
public function handle(): void
3935
{
4036
$this->setArguments();
41-
$resourceName = $this->entityName . "Resource";
37+
$resourceName = $this->entityName . self::OBJECT_NAME;
4238
$resourceNamespace = config('repository.path.namespace.resources');
4339
$relativeResourcesPath = config('repository.path.relative.resources');
44-
$resourceStubsPath = __DIR__ . '/../../' . config('repository.path.stub.resources');
40+
4541
$filenameWithPath = $relativeResourcesPath . $resourceName . '.php';
4642

47-
$this->checkDelete($filenameWithPath, $resourceName, "Resource");
43+
$this->checkAndPrepare($filenameWithPath, $resourceName, $relativeResourcesPath, $resourceNamespace);
44+
45+
$RepoCreator = $this->getResourceCreator($resourceNamespace, $resourceName);
46+
$baseContent = $this->generateBaseContent($RepoCreator, $filenameWithPath);
47+
48+
$this->finalized($filenameWithPath, $resourceName, $baseContent);
49+
}
50+
51+
/**
52+
* @param string $filenameWithPath
53+
* @param string $resourceName
54+
* @param mixed $relativeResourcesPath
55+
* @param mixed $resourceNamespace
56+
* @return void
57+
*/
58+
private function checkAndPrepare(string $filenameWithPath, string $resourceName, mixed $relativeResourcesPath, mixed $resourceNamespace): void
59+
{
60+
$this->checkDelete($filenameWithPath, $resourceName, self::OBJECT_NAME);
4861
$this->checkDirectory($relativeResourcesPath);
49-
$this->checkClassExist($resourceNamespace, $resourceName, "Resource");
62+
$this->checkClassExist($resourceNamespace, $resourceName, self::OBJECT_NAME);
63+
}
64+
65+
/**
66+
* @param string $tableName
67+
* @return Collection
68+
*/
69+
private function getColumnsOf(string $tableName): Collection
70+
{
71+
$columns = $this->getAllColumnsInTable($tableName);
72+
$this->checkEmpty($columns, $tableName);
5073

51-
$columns = $this->getAllColumnsInTable($this->tableName);
52-
$this->checkEmpty($columns, $this->tableName);
74+
return $columns;
75+
}
76+
77+
/**
78+
* @param mixed $resourceNamespace
79+
* @param string $resourceName
80+
* @return CreatorResource
81+
*/
82+
private function getResourceCreator(mixed $resourceNamespace, string $resourceName): CreatorResource
83+
{
84+
$resourceStubsPath = __DIR__ . '/../../' . config('repository.path.stub.resources');
5385

54-
$RepoCreator = new CreatorResource($columns,
86+
return new CreatorResource($this->getColumnsOf($this->tableName),
5587
$this->tableName,
5688
$this->entityName,
5789
$this->entityNamespace,
@@ -60,10 +92,17 @@ public function handle(): void
6092
$resourceStubsPath,
6193
$this->detectForeignKeys,
6294
$this->entityVariableName);
63-
$creator = new BaseCreator($RepoCreator);
64-
$baseContent = $creator->createClass($filenameWithPath, $this);
65-
$this->finalized($filenameWithPath, $resourceName, $baseContent);
95+
}
6696

97+
/**
98+
* @param CreatorResource $RepoCreator
99+
* @param string $filenameWithPath
100+
* @return string
101+
*/
102+
private function generateBaseContent(CreatorResource $RepoCreator, string $filenameWithPath): string
103+
{
104+
$creator = new BaseCreator($RepoCreator);
105+
return $creator->createClass($filenameWithPath, $this);
67106
}
68107

69108
}

0 commit comments

Comments
 (0)