Skip to content

Commit fed5cbb

Browse files
refactor(Command): improvement code style , extract new methods in MakeInterfaceRepository command
1 parent 3aaf4e9 commit fed5cbb

File tree

1 file changed

+163
-55
lines changed

1 file changed

+163
-55
lines changed

src/Commands/MakeInterfaceRepository.php

Lines changed: 163 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace Eghamat24\DatabaseRepository\Commands;
44

5+
use Eghamat24\DatabaseRepository\Models\Enums\DataTypeEnum;
6+
use Illuminate\Support\Collection;
57
use Illuminate\Support\Str;
68
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
7-
use Illuminate\Console\Command;
89

910
class MakeInterfaceRepository extends BaseCommand
1011
{
12+
use CustomMySqlQueries;
13+
1114
/**
1215
* The name and signature of the console command.
1316
*
@@ -26,89 +29,194 @@ class MakeInterfaceRepository extends BaseCommand
2629
*/
2730
protected $description = 'Create a new interface for repository';
2831

29-
use CustomMySqlQueries;
32+
33+
public function handle(): void
34+
{
35+
$this->setArguments();
36+
$filenameWithPath = $this->relativeInterfacePath . $this->interfaceName . '.php';
37+
38+
$this->checkAndPrepare($filenameWithPath);
39+
40+
[
41+
'basedContent' => $baseContent,
42+
'getOneStub' => $getOneStub,
43+
'getAllStub' => $getAllStub,
44+
'createFunctionStub' => $createFunctionStub,
45+
'updateFunctionStub' => $updateFunctionStub,
46+
'deleteAndUndeleteStub' => $deleteAndUndeleteStub
47+
] = $this->getStubContents($this->interfaceRepositoryStubsPath);
48+
49+
$baseContent = $this->writeFunctionOnBaseContent(
50+
$baseContent, $this->writeGetOneFunction($getOneStub, 'id', DataTypeEnum::INTEGER_TYPE)
51+
);
52+
53+
$baseContent = $this->writeFunctionOnBaseContent(
54+
$baseContent, $this->writeGetAllFunction($getAllStub, 'id', DataTypeEnum::INTEGER_TYPE)
55+
);
56+
57+
$columns = $this->getColumnsOf($this->tableName);
58+
$indexes = $this->extractIndexes($this->tableName);
59+
$baseContent = $this->writeGetFunctionByIndexColumnOnBaseContent($indexes, $columns, $baseContent, $getOneStub, $getAllStub);
60+
$baseContent = $this->writeGetFunctionByForeignKeyOnBaseContent($baseContent, $getOneStub, $getAllStub);
61+
62+
$allColumns = $columns->pluck('COLUMN_NAME')->toArray();
63+
$baseContent = $this->setTimestampsColumnOnBaseContent(
64+
$allColumns, $baseContent, $createFunctionStub, $updateFunctionStub, $deleteAndUndeleteStub
65+
);
66+
67+
$baseContent = $this->replaceDataOnInterfaceContent($baseContent);
68+
69+
$this->finalized($filenameWithPath, $this->entityName, $baseContent);
70+
}
71+
72+
private function writeFunctionOnBaseContent($baseContent, string $writeFunction): string|array
73+
{
74+
return substr_replace($baseContent, $writeFunction, -2, 0);
75+
}
76+
77+
private function writeFunction(string $stub, string $columnName, string $attributeType, array $placeHolders): array|string
78+
{
79+
$replaceValues = \array_map(function ($placeholder) use ($columnName, $attributeType) {
80+
81+
return match ($placeholder) {
82+
'{{ FunctionName }}' => ucfirst(Str::camel($columnName)),
83+
'{{ ColumnName }}' => $columnName,
84+
'{{ AttributeType }}' => $attributeType,
85+
'{{ AttributeName }}' => Str::camel($columnName),
86+
'{{ FunctionNamePlural }}' => ucfirst(Str::plural(Str::camel($columnName))),
87+
'{{ AttributeNamePlural }}' => Str::plural(Str::camel($columnName)),
88+
default => $placeholder,
89+
};
90+
}, $placeHolders);
91+
92+
return str_replace($placeHolders, $replaceValues, $stub);
93+
}
3094

3195
private function writeGetOneFunction(string $getOneStub, string $columnName, string $attributeType): string
3296
{
33-
return str_replace(['{{ FunctionName }}', '{{ ColumnName }}', '{{ AttributeType }}', '{{ AttributeName }}'],
34-
[ucfirst(Str::camel($columnName)), $columnName, $attributeType, Str::camel($columnName)],
35-
$getOneStub);
97+
$placeHolders = ['{{ FunctionName }}', '{{ ColumnName }}', '{{ AttributeType }}', '{{ AttributeName }}'];
98+
return $this->writeFunction($getOneStub, $columnName, $attributeType, $placeHolders);
3699
}
37100

38-
private function writeGetAllFunction(string $getOneStub, string $columnName, string $attributeType): string
101+
private function writeGetAllFunction(string $getAllStub, string $columnName, string $attributeType): string
39102
{
40-
return str_replace(['{{ FunctionNamePlural }}', '{{ AttributeType }}', '{{ AttributeNamePlural }}'],
41-
[ucfirst(Str::plural(Str::camel($columnName))), $attributeType, Str::plural(Str::camel($columnName))],
42-
$getOneStub);
103+
$placeHolders = ['{{ FunctionNamePlural }}', '{{ AttributeType }}', '{{ AttributeNamePlural }}'];
104+
return $this->writeFunction($getAllStub, $columnName, $attributeType, $placeHolders);
43105
}
44106

45-
/**
46-
* Execute the console command.
47-
*
48-
* @return int
49-
*/
50-
public function handle(): void
107+
private function getColumnsOf(string $tableName): Collection
51108
{
52-
$this->setArguments();
53-
$filenameWithPath = $this->relativeInterfacePath . $this->interfaceName . '.php';
109+
$columns = $this->getAllColumnsInTable($tableName);
110+
$this->checkEmpty($columns, $tableName);
54111

55-
$this->checkDelete($filenameWithPath, $this->interfaceName, "Interface");
112+
return $columns;
113+
}
114+
115+
private function checkAndPrepare(string $filenameWithPath): void
116+
{
117+
$this->checkDelete($filenameWithPath, $this->interfaceName, 'Interface');
56118
$this->checkDirectory($this->relativeInterfacePath);
57-
$this->checkClassExist($this->repositoryNamespace, $this->interfaceName, "Interface");
119+
$this->checkClassExist($this->repositoryNamespace, $this->interfaceName, 'Interface');
120+
}
58121

59-
$columns = $this->getAllColumnsInTable($this->tableName);
60-
$this->checkEmpty($columns, $this->tableName);
122+
private function setTimestampsColumnOnBaseContent(
123+
array $allColumns,
124+
array|string $baseContent,
125+
bool|string $createFunctionStub,
126+
bool|string $updateFunctionStub,
127+
bool|string $deleteAndUndeleteStub): string|array
128+
{
129+
if (in_array('created_at', $allColumns, true)) {
130+
$baseContent = substr_replace($baseContent, $createFunctionStub, -2, 0);
131+
}
132+
133+
if (in_array('updated_at', $allColumns, true)) {
134+
$baseContent = substr_replace($baseContent, $updateFunctionStub, -2, 0);
135+
}
136+
137+
if (in_array('deleted_at', $allColumns, true)) {
138+
$baseContent = substr_replace($baseContent, $deleteAndUndeleteStub, -2, 0);
139+
}
61140

62-
if ($this->detectForeignKeys) {
63-
$foreignKeys = $this->extractForeignKeys($this->tableName);
141+
return $baseContent;
142+
}
143+
144+
private function getStubContents(string $basePath): array
145+
{
146+
$stubs = [
147+
'basedContent' => 'class.stub',
148+
'getOneStub' => 'getOneBy.stub',
149+
'getAllStub' => 'getAllBy.stub',
150+
'createFunctionStub' => 'create.stub',
151+
'updateFunctionStub' => 'update.stub',
152+
'deleteAndUndeleteStub' => 'deleteAndUndelete.stub',
153+
];
154+
155+
$stubsContent = [];
156+
157+
foreach ($stubs as $name => $endWith) {
158+
$stubsContent[$name] = file_get_contents($basePath . $endWith);
64159
}
65160

66-
$baseContent = file_get_contents($this->interfaceRepositoryStubsPath . 'class.stub');
67-
$getOneStub = file_get_contents($this->interfaceRepositoryStubsPath . 'getOneBy.stub');
68-
$getAllStub = file_get_contents($this->interfaceRepositoryStubsPath . 'getAllBy.stub');
69-
$createFunctionStub = file_get_contents($this->interfaceRepositoryStubsPath . 'create.stub');
70-
$updateFunctionStub = file_get_contents($this->interfaceRepositoryStubsPath . 'update.stub');
71-
$deleteAndUndeleteStub = file_get_contents($this->interfaceRepositoryStubsPath . 'deleteAndUndelete.stub');
161+
return $stubsContent;
162+
}
72163

73-
$baseContent = substr_replace($baseContent, $this->writeGetOneFunction($getOneStub, 'id', 'int'), -2, 0);
74-
$baseContent = substr_replace($baseContent, $this->writeGetAllFunction($getAllStub, 'id', 'int'), -2, 0);
75-
$columnsInfo = $this->getAllColumnsInTable($this->tableName);
164+
private function replaceDataOnInterfaceContent(array|string $baseContent): string|array
165+
{
166+
$placeHolders = [
167+
'{{ EntityName }}' => $this->entityName,
168+
'{{ EntityNamespace }}' => $this->entityNamespace,
169+
'{{ EntityVariableName }}' => $this->entityVariableName,
170+
'{{ InterfaceRepositoryName }}' => $this->interfaceName,
171+
'{{ RepositoryNamespace }}' => $this->repositoryNamespace
172+
];
173+
174+
return \str_replace(\array_keys($placeHolders), \array_values($placeHolders), $baseContent);
175+
}
76176

77-
$indexes = $this->extractIndexes($this->tableName);
177+
private function writeGetFunctionByIndexColumnOnBaseContent(Collection $indexes, Collection $columns, mixed $baseContent, $getOneStub, $getAllStub): mixed
178+
{
78179
foreach ($indexes as $index) {
79-
$columnInfo = collect($columnsInfo)->where('COLUMN_NAME', $index->COLUMN_NAME)->first();
80-
$baseContent = substr_replace($baseContent, $this->writeGetOneFunction($getOneStub, $index->COLUMN_NAME, $this->getDataType($columnInfo->COLUMN_TYPE, $columnInfo->DATA_TYPE)), -2, 0);
180+
$columnInfo = collect($columns)->where('COLUMN_NAME', $index->COLUMN_NAME)->first();
181+
182+
$baseContent = $this->writeFunctionOnBaseContent($baseContent,
183+
$this->writeGetOneFunction(
184+
$getOneStub, $index->COLUMN_NAME, $this->getDataType($columnInfo->COLUMN_TYPE, $columnInfo->DATA_TYPE)
185+
)
186+
);
81187

82188
if ($index->Non_unique == 1) {
83-
$baseContent = substr_replace($baseContent, $this->writeGetOneFunction($getAllStub, $index->COLUMN_NAME, $this->getDataType($columnInfo->COLUMN_TYPE, $columnInfo->DATA_TYPE)), -2, 0);
84-
}
85-
}
86189

87-
if ($this->detectForeignKeys) {
88-
foreach ($foreignKeys as $_foreignKey) {
89-
$baseContent = substr_replace($baseContent, $this->writeGetOneFunction($getOneStub, $_foreignKey->COLUMN_NAME, $this->entityName), -2, 0);
90-
$baseContent = substr_replace($baseContent, $this->writeGetAllFunction($getAllStub, $_foreignKey->COLUMN_NAME, $this->entityName), -2, 0);
190+
$baseContent = $this->writeFunctionOnBaseContent($baseContent,
191+
$this->writeGetOneFunction(
192+
$getAllStub, $index->COLUMN_NAME, $this->getDataType($columnInfo->COLUMN_TYPE, $columnInfo->DATA_TYPE)
193+
)
194+
);
91195
}
92196
}
93197

94-
$allColumns = $columns->pluck('COLUMN_NAME')->toArray();
198+
return $baseContent;
199+
}
95200

96-
if (in_array('created_at', $allColumns, true)) {
97-
$baseContent = substr_replace($baseContent, $createFunctionStub, -2, 0);
201+
public function writeGetFunctionByForeignKeyOnBaseContent(array|string $baseContent, $getOneStub, $getAllStub): string|array
202+
{
203+
if (empty($this->detectForeignKeys)) {
204+
return $baseContent;
98205
}
99206

100-
if (in_array('updated_at', $allColumns, true)) {
101-
$baseContent = substr_replace($baseContent, $updateFunctionStub, -2, 0);
102-
}
207+
$foreignKeys = $this->extractForeignKeys($this->tableName);
103208

104-
if (in_array('deleted_at', $allColumns, true)) {
105-
$baseContent = substr_replace($baseContent, $deleteAndUndeleteStub, -2, 0);
106-
}
209+
foreach ($foreignKeys as $_foreignKey) {
107210

108-
$baseContent = str_replace(['{{ EntityName }}', '{{ EntityNamespace }}', '{{ EntityVariableName }}', '{{ InterfaceRepositoryName }}', '{{ RepositoryNamespace }}'],
109-
[$this->entityName, $this->entityNamespace, $this->entityVariableName, $this->interfaceName, $this->repositoryNamespace],
110-
$baseContent);
211+
$baseContent = $this->writeFunctionOnBaseContent(
212+
$baseContent, $this->writeGetOneFunction($getOneStub, $_foreignKey->COLUMN_NAME, $this->entityName)
213+
);
111214

112-
$this->finalized($filenameWithPath, $this->entityName, $baseContent);
215+
$baseContent = $this->writeFunctionOnBaseContent(
216+
$baseContent, $this->writeGetAllFunction($getAllStub, $_foreignKey->COLUMN_NAME, $this->entityName)
217+
);
218+
}
219+
220+
return $baseContent;
113221
}
114222
}

0 commit comments

Comments
 (0)