Skip to content

Commit 2fa36d8

Browse files
authored
Merge pull request #33 from saMahmoudzadeh/refactor/command
refactor commands and more readability and testability
2 parents 0f95a4d + f648a0e commit 2fa36d8

File tree

5 files changed

+145
-52
lines changed

5 files changed

+145
-52
lines changed

src/Commands/MakeEntity.php

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
namespace Eghamat24\DatabaseRepository\Commands;
44

5+
use Illuminate\Support\Collection;
56
use Illuminate\Support\Str;
6-
use Eghamat24\DatabaseRepository\CreateEntity;
77
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
88
use Eghamat24\DatabaseRepository\Creators\CreatorEntity;
99
use Eghamat24\DatabaseRepository\Creators\BaseCreator;
10-
use Illuminate\Support\Collection;
1110

1211
class MakeEntity extends BaseCommand
1312
{
1413
use CustomMySqlQueries;
1514

15+
private const OBJECT_NAME = 'Entity';
16+
1617
/**
1718
* The name and signature of the console command.
1819
*
@@ -39,28 +40,65 @@ public function handle(): void
3940
$this->setArguments();
4041
$filenameWithPath = $this->relativeEntitiesPath . $this->entityName . '.php';
4142

42-
$this->checkDelete($filenameWithPath, $this->entityName, "Entity");
43-
$this->checkDirectory($this->relativeEntitiesPath);
44-
$this->checkClassExist($this->entityNamespace, $this->entityName, "Entity");
45-
46-
$columns = $this->getAllColumnsInTable($this->tableName);
47-
$this->checkEmpty($columns, $this->tableName);
43+
$this->checkAndPrepare($filenameWithPath);
44+
$columns = $this->getColumnsOf($this->tableName);
4845

4946
foreach ($columns as $_column) {
5047
$_column->COLUMN_NAME = Str::camel($_column->COLUMN_NAME);
5148
}
5249

53-
$entityCreator = new CreatorEntity($columns,
50+
$entityCreator = $this->getCreatorEntity($columns);
51+
$baseContent = $this->createBaseContent($entityCreator, $filenameWithPath);
52+
53+
$this->finalized($filenameWithPath, $this->entityName, $baseContent);
54+
}
55+
56+
/**
57+
* @param string $tableName
58+
* @return Collection
59+
*/
60+
private function getColumnsOf(string $tableName): Collection
61+
{
62+
$columns = $this->getAllColumnsInTable($tableName);
63+
$this->checkEmpty($columns, $tableName);
64+
65+
return $columns;
66+
}
67+
68+
/**
69+
* @param string $filenameWithPath
70+
* @return void
71+
*/
72+
private function checkAndPrepare(string $filenameWithPath): void
73+
{
74+
$this->checkDelete($filenameWithPath, $this->entityName, self::OBJECT_NAME);
75+
$this->checkDirectory($this->relativeEntitiesPath);
76+
$this->checkClassExist($this->entityNamespace, $this->entityName, self::OBJECT_NAME);
77+
}
78+
79+
/**
80+
* @param Collection $columns
81+
* @return CreatorEntity
82+
*/
83+
private function getCreatorEntity(Collection $columns): CreatorEntity
84+
{
85+
return new CreatorEntity($columns,
5486
$this->detectForeignKeys,
5587
$this->tableName,
5688
$this->entityName,
5789
$this->entityNamespace,
5890
$this->entityStubsPath
5991
);
60-
$creator = new BaseCreator($entityCreator);
61-
$baseContent = $creator->createClass($filenameWithPath, $this);
62-
63-
$this->finalized($filenameWithPath, $this->entityName, $baseContent);
92+
}
6493

94+
/**
95+
* @param CreatorEntity $entityCreator
96+
* @param string $filenameWithPath
97+
* @return string
98+
*/
99+
public function createBaseContent(CreatorEntity $entityCreator, string $filenameWithPath): string
100+
{
101+
$creator = new BaseCreator($entityCreator);
102+
return $creator->createClass($filenameWithPath, $this);
65103
}
66104
}

src/Commands/MakeEnum.php

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class MakeEnum extends BaseCommand
1212
{
1313
use CustomMySqlQueries;
1414

15+
private const OBJECT_NAME = 'Enum';
16+
1517
/**
1618
* The name and signature of the console command.
1719
*
@@ -32,20 +34,15 @@ class MakeEnum extends BaseCommand
3234
public function handle(): void
3335
{
3436
$this->setArguments();
35-
$columns = $this->getAllColumnsInTable($this->tableName);
36-
37-
$this->checkEmpty($columns, $this->tableName);
38-
37+
$columns = $this->getColumnsOf($this->tableName);
3938
$enums = $this->extractEnumsFromColumns($columns);
4039

4140
$attributeStub = file_get_contents($this->enumStubPath . 'attribute.stub');
4241

4342
foreach ($enums as $enumName => $enum) {
4443
$filenameWithPath = $this->relativeEnumsPath . $enumName . '.php';
4544

46-
$this->checkDirectory($this->enumNamespace);
47-
$this->checkClassExist($this->relativeEnumsPath, $enumName, 'Enum');
48-
45+
$this->checkAndPrepare($enumName);
4946
$baseContent = $this->getBaseCreator($columns, $attributeStub, $enum, $enumName)
5047
->createClass($filenameWithPath, $this);
5148

@@ -73,7 +70,7 @@ public function extractEnumsFromColumns(Collection $columns): array
7370
$this->checkDelete(
7471
$this->relativeEnumsPath . $enumClassName . '.php',
7572
$enumClassName,
76-
'Enum'
73+
self::OBJECT_NAME
7774
);
7875
}
7976

@@ -85,7 +82,7 @@ private function getEnumClassName(mixed $_column): string
8582
$tableName = ucfirst(Str::camel($_column->TABLE_NAME));
8683
$columnName = $_column->COLUMN_NAME;
8784

88-
return Str::studly(Str::singular($tableName) . '_' . $columnName) . 'Enum';
85+
return Str::studly(Str::singular($tableName) . '_' . $columnName) . self::OBJECT_NAME;
8986
}
9087

9188
private function extractEnumValues($columnType): array
@@ -108,4 +105,26 @@ private function getBaseCreator(Collection $columns, bool|string $attributeStub,
108105

109106
return new BaseCreator($enumCreator);
110107
}
108+
109+
/**
110+
* @param string $table
111+
* @return Collection
112+
*/
113+
public function getColumnsOf(string $table): Collection
114+
{
115+
$columns = $this->getAllColumnsInTable($table);
116+
$this->checkEmpty($columns, $table);
117+
118+
return $columns;
119+
}
120+
121+
/**
122+
* @param int|string $enumName
123+
* @return void
124+
*/
125+
public function checkAndPrepare(int|string $enumName): void
126+
{
127+
$this->checkDirectory($this->enumNamespace);
128+
$this->checkClassExist($this->relativeEnumsPath, $enumName, self::OBJECT_NAME);
129+
}
111130
}

src/Commands/MakeInterfaceRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function handle(): void
7171

7272
private function writeFunctionOnBaseContent($baseContent, string $writeFunction): string|array
7373
{
74-
return substr_replace($baseContent, $writeFunction, -2, 0);
74+
return \substr_replace($baseContent, $writeFunction, -2, 0);
7575
}
7676

7777
private function writeFunction(string $stub, string $columnName, string $attributeType, array $placeHolders): array|string

src/Commands/MakeRepository.php

Lines changed: 64 additions & 28 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;
5+
use Illuminate\Support\Collection;
76
use Eghamat24\DatabaseRepository\Creators\BaseCreator;
87
use Eghamat24\DatabaseRepository\Creators\CreatorRepository;
98
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
109

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

31-
use CustomMySqlQueries;
32-
33-
/**
34-
* Execute the console command.
35-
*
36-
* @return int
37-
*/
3834
public function handle(): void
3935
{
4036
$this->checkDatabasesExist();
4137
$this->checkStrategyName();
4238

4339
$this->setArguments();
44-
$repositoryName = $this->entityName . 'Repository';
45-
// $sqlRepositoryName = 'MySql'.$this->entityName.'Repository';
46-
$sqlRepositoryName = ucwords($this->selectedDb) . $this->entityName . 'Repository';
47-
$sqlRepositoryVariable = 'repository';
48-
$redisRepositoryVariable = 'redisRepository';
49-
$redisRepositoryName = 'Redis' . $this->entityName . 'Repository';
40+
$repositoryName = $this->entityName . self::OBJECT_NAME;
5041
$relativeRepositoryPath = config('repository.path.relative.repositories') . "$this->entityName" . DIRECTORY_SEPARATOR;
51-
$repositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.base');
42+
5243
$filenameWithPath = $relativeRepositoryPath . $repositoryName . '.php';
53-
$this->checkDelete($filenameWithPath, $repositoryName, "Repository");
54-
$this->checkDirectory($relativeRepositoryPath);
55-
$this->checkClassExist($this->repositoryNamespace, $repositoryName, "Repository");
56-
$columns = $this->getAllColumnsInTable($this->tableName);
57-
$this->checkEmpty($columns, $this->tableName);
58-
$RepoCreator = new CreatorRepository(
59-
$columns,
60-
$sqlRepositoryVariable,
44+
$this->checkAndPrepare($filenameWithPath, $repositoryName, $relativeRepositoryPath);
45+
46+
$repositoryCreator = $this->getRepositoryCreator($repositoryName);
47+
$baseContent = $this->createBaseContent($repositoryCreator, $filenameWithPath);
48+
49+
$this->finalized($filenameWithPath, $repositoryName, $baseContent);
50+
}
51+
52+
/**
53+
* @param string $repositoryName
54+
* @return CreatorRepository
55+
*/
56+
private function getRepositoryCreator(string $repositoryName): CreatorRepository
57+
{
58+
$sqlRepositoryName = ucwords($this->selectedDb) . $repositoryName;
59+
$repositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.base');
60+
61+
return new CreatorRepository(
62+
$this->getColumnsOf($this->tableName),
63+
'repository',
6164
$sqlRepositoryName,
6265
$repositoryStubsPath,
6366
$this->detectForeignKeys,
@@ -69,12 +72,45 @@ public function handle(): void
6972
$this->interfaceName,
7073
$this->repositoryNamespace,
7174
$this->selectedDb,
72-
$redisRepositoryVariable,
73-
$redisRepositoryName,
75+
'redisRepository',
76+
'Redis' . $repositoryName,
7477
$this->strategyName
7578
);
79+
}
80+
81+
/**
82+
* @param string $tableName
83+
* @return Collection
84+
*/
85+
private function getColumnsOf(string $tableName): Collection
86+
{
87+
$columns = $this->getAllColumnsInTable($tableName);
88+
$this->checkEmpty($columns, $tableName);
89+
90+
return $columns;
91+
}
92+
93+
/**
94+
* @param CreatorRepository $RepoCreator
95+
* @param string $filenameWithPath
96+
* @return string
97+
*/
98+
private function createBaseContent(CreatorRepository $RepoCreator, string $filenameWithPath): string
99+
{
76100
$creator = new BaseCreator($RepoCreator);
77-
$baseContent = $creator->createClass($filenameWithPath, $this);
78-
$this->finalized($filenameWithPath, $repositoryName, $baseContent);
101+
return $creator->createClass($filenameWithPath, $this);
102+
}
103+
104+
/**
105+
* @param string $filenameWithPath
106+
* @param string $repositoryName
107+
* @param string $relativeRepositoryPath
108+
* @return void
109+
*/
110+
private function checkAndPrepare(string $filenameWithPath, string $repositoryName, string $relativeRepositoryPath): void
111+
{
112+
$this->checkDelete($filenameWithPath, $repositoryName, self::OBJECT_NAME);
113+
$this->checkDirectory($relativeRepositoryPath);
114+
$this->checkClassExist($this->repositoryNamespace, $repositoryName, self::OBJECT_NAME);
79115
}
80116
}

stubs/Repositories/Redis/getAllBy/base.query_cache_strategy.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ $cacheKey = $this->redisRepository->makeKey([
55

66
$entities = $this->redisRepository->get($cacheKey);
77

8-
if (is_null($entities)) {
8+
if ($entities === null) {
99
$entities = $this->repository->{{ FunctionName }}(${{ ColumnName }});
1010
$this->redisRepository->put($cacheKey, $entities);
1111
}

0 commit comments

Comments
 (0)