Skip to content

Commit 0e5ca2b

Browse files
authored
Merge pull request #34 from saMahmoudzadeh/refactor/MakeCommands
refactor commands (more readability and testability)
2 parents 2fa36d8 + 0c3d650 commit 0e5ca2b

File tree

4 files changed

+175
-63
lines changed

4 files changed

+175
-63
lines changed

src/Commands/MakeEntity.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function handle(): void
4848
}
4949

5050
$entityCreator = $this->getCreatorEntity($columns);
51-
$baseContent = $this->createBaseContent($entityCreator, $filenameWithPath);
51+
$baseContent = $this->getBaseContent($entityCreator, $filenameWithPath);
5252

5353
$this->finalized($filenameWithPath, $this->entityName, $baseContent);
5454
}
@@ -96,8 +96,7 @@ private function getCreatorEntity(Collection $columns): CreatorEntity
9696
* @param string $filenameWithPath
9797
* @return string
9898
*/
99-
public function createBaseContent(CreatorEntity $entityCreator, string $filenameWithPath): string
100-
{
99+
private function getBaseContent(CreatorEntity $entityCreator, string $filenameWithPath): string
101100
$creator = new BaseCreator($entityCreator);
102101
return $creator->createClass($filenameWithPath, $this);
103102
}

src/Commands/MakeFactory.php

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

33
namespace Eghamat24\DatabaseRepository\Commands;
44

5+
use Illuminate\Support\Collection;
56
use Illuminate\Support\Str;
67
use Eghamat24\DatabaseRepository\Creators\BaseCreator;
78
use Eghamat24\DatabaseRepository\Creators\CreatorFactory;
89
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
910

1011
class MakeFactory extends BaseCommand
1112
{
13+
use CustomMySqlQueries;
14+
15+
private const OBJECT_NAME = 'Factory';
16+
1217
/**
1318
* The name and signature of the console command.
1419
*
@@ -26,51 +31,85 @@ class MakeFactory extends BaseCommand
2631
*/
2732
protected $description = 'Create a new factory.';
2833

29-
use CustomMySqlQueries;
30-
3134
public function writeSetter(string $setterStub, string $columnName): string
3235
{
3336
return str_replace(['{{ SetterName }}', '{{ AttributeName }}'],
3437
[ucfirst($columnName), Str::snake($columnName)],
3538
$setterStub);
3639
}
3740

38-
/**
39-
* Execute the console command.
40-
*
41-
* @return int
42-
*/
4341
public function handle(): void
4442
{
4543
$this->setArguments();
4644

4745
$filenameWithPath = $this->relativeFactoriesPath . $this->factoryName . '.php';
4846

49-
$this->checkDelete($filenameWithPath, $this->entityName, "Factory");
50-
$this->checkDirectory($this->relativeFactoriesPath);
51-
$this->checkClassExist($this->factoryNamespace, $this->entityName, "Factory");
47+
$this->checkAndPrepare($filenameWithPath);
5248

53-
$columns = $this->getAllColumnsInTable($this->tableName);
54-
$this->checkEmpty($columns, $this->tableName);
49+
$columns = $this->getColumnsOf($this->tableName);
5550

5651
foreach ($columns as $_column) {
5752
$_column->COLUMN_NAME = Str::camel($_column->COLUMN_NAME);
5853
}
5954

6055
$baseContent = file_get_contents($this->factoryStubsPath . 'class.stub');
6156

62-
$factoryCreator = new CreatorFactory(
57+
$factoryCreator = $this->getCreatorFactory($columns, $baseContent);
58+
$baseContent = $this->generateBaseContent($factoryCreator, $filenameWithPath);
59+
60+
$this->finalized($filenameWithPath, $this->factoryName, $baseContent);
61+
}
62+
63+
/**
64+
* @param string $filenameWithPath
65+
* @return void
66+
*/
67+
public function checkAndPrepare(string $filenameWithPath): void
68+
{
69+
$this->checkDelete($filenameWithPath, $this->entityName, self::OBJECT_NAME);
70+
$this->checkDirectory($this->relativeFactoriesPath);
71+
$this->checkClassExist($this->factoryNamespace, $this->entityName, self::OBJECT_NAME);
72+
}
73+
74+
/**
75+
* @param string $tableName
76+
* @return Collection
77+
*/
78+
public function getColumnsOf(string $tableName): Collection
79+
{
80+
$columns = $this->getAllColumnsInTable($tableName);
81+
$this->checkEmpty($columns, $tableName);
82+
83+
return $columns;
84+
}
85+
86+
/**
87+
* @param Collection $columns
88+
* @param bool|string $baseContent
89+
* @return CreatorFactory
90+
*/
91+
public function getCreatorFactory(Collection $columns, bool|string $baseContent): CreatorFactory
92+
{
93+
return new CreatorFactory(
6394
$columns,
6495
$this->entityName,
6596
$this->entityNamespace,
6697
$this->factoryStubsPath,
6798
$this->factoryNamespace,
6899
$this->entityVariableName,
69100
$this->factoryName,
70-
$baseContent);
71-
$creator = new BaseCreator($factoryCreator);
72-
$baseContent = $creator->createClass($filenameWithPath, $this);
101+
$baseContent
102+
);
103+
}
73104

74-
$this->finalized($filenameWithPath, $this->factoryName, $baseContent);
105+
/**
106+
* @param CreatorFactory $factoryCreator
107+
* @param string $filenameWithPath
108+
* @return string
109+
*/
110+
public function generateBaseContent(CreatorFactory $factoryCreator, string $filenameWithPath): string
111+
{
112+
$creator = new BaseCreator($factoryCreator);
113+
return $creator->createClass($filenameWithPath, $this);
75114
}
76115
}

src/Commands/MakeRedisRepository.php

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

33
namespace Eghamat24\DatabaseRepository\Commands;
44

5-
use Illuminate\Support\Str;
5+
use Illuminate\Support\Collection;
66
use Eghamat24\DatabaseRepository\Creators\BaseCreator;
77
use Eghamat24\DatabaseRepository\Creators\CreatorRedisRepository;
88
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
9-
use Illuminate\Console\Command;
109

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

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

43-
$redisRepositoryName = "Redis$this->entityName" . "Repository";
44-
$redisRepositoryNamespace = config('repository.path.namespace.repositories');
45-
$relativeRedisRepositoryPath = config('repository.path.relative.repositories') . "$this->entityName" . DIRECTORY_SEPARATOR;
39+
$redisRepositoryName = "Redis$this->entityName" . 'Repository';
40+
$relativeRedisRepositoryPath = config('repository.path.relative.repositories') . $this->entityName . DIRECTORY_SEPARATOR;
4641
$filenameWithPath = $relativeRedisRepositoryPath . $redisRepositoryName . '.php';
4742

48-
$this->checkDelete($filenameWithPath, $redisRepositoryName, "Redis Repository");
49-
$this->checkDirectory($relativeRedisRepositoryPath);
50-
$this->checkClassExist($this->repositoryNamespace, $redisRepositoryName, "Redis Repository");
51-
$columns = $this->getAllColumnsInTable($this->tableName);
52-
$this->checkEmpty($columns, $this->tableName);
43+
$this->checkAndPrepare($filenameWithPath, $redisRepositoryName, $relativeRedisRepositoryPath);
44+
$this->getColumnsOf($this->tableName);
5345

54-
if ($this->detectForeignKeys) {
55-
$foreignKeys = $this->extractForeignKeys($this->tableName);
56-
}
46+
$redisRepositoryCreator = $this->getRedisRepositoryCreator($redisRepositoryName);
47+
$baseContent = $this->getBaseContent($redisRepositoryCreator, $filenameWithPath);
5748

58-
$repositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.base');
59-
$mysqlRepoCreator = new CreatorRedisRepository($redisRepositoryName, $redisRepositoryNamespace, $this->entityName, $this->strategyName, $repositoryStubsPath);
60-
$creator = new BaseCreator($mysqlRepoCreator);
61-
$baseContent = $creator->createClass($filenameWithPath, $this);
6249
$this->finalized($filenameWithPath, $redisRepositoryName, $baseContent);
6350
}
51+
52+
53+
private function getColumnsOf(string $tableName): Collection
54+
{
55+
$columns = $this->getAllColumnsInTable($tableName);
56+
$this->checkEmpty($columns, $tableName);
57+
58+
return $columns;
59+
}
60+
61+
62+
private function getRedisRepositoryCreator(string $redisRepositoryName): CreatorRedisRepository
63+
{
64+
$redisRepositoryNamespace = config('repository.path.namespace.repositories');
65+
$repositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.base');
66+
67+
return new CreatorRedisRepository(
68+
$redisRepositoryName,
69+
$redisRepositoryNamespace,
70+
$this->entityName,
71+
$this->strategyName,
72+
$repositoryStubsPath
73+
);
74+
}
75+
76+
/**
77+
* @param CreatorRedisRepository $redisRepositoryCreator
78+
* @param string $filenameWithPath
79+
* @return string
80+
*/
81+
private function getBaseContent(CreatorRedisRepository $redisRepositoryCreator, string $filenameWithPath): string
82+
{
83+
$creator = new BaseCreator($redisRepositoryCreator);
84+
return $creator->createClass($filenameWithPath, $this);
85+
}
86+
87+
/**
88+
* @param string $filenameWithPath
89+
* @param string $redisRepositoryName
90+
* @param string $relativeRedisRepositoryPath
91+
* @return void
92+
*/
93+
private function checkAndPrepare(string $filenameWithPath, string $redisRepositoryName, string $relativeRedisRepositoryPath): void
94+
{
95+
$this->checkDelete($filenameWithPath, $redisRepositoryName, self::OBJECT_NAME);
96+
$this->checkDirectory($relativeRedisRepositoryPath);
97+
$this->checkClassExist($this->repositoryNamespace, $redisRepositoryName, self::OBJECT_NAME);
98+
}
6499
}

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)