Skip to content

Commit 4994e6b

Browse files
refactor(Commands): modify and refactor handle method in MakeMySqlRepository
1 parent 58c1405 commit 4994e6b

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

src/Commands/MakeMySqlRepository.php

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class MakeMySqlRepository extends BaseCommand
1111
{
1212
use CustomMySqlQueries;
1313

14+
private const OBJECT_NAME = 'MySql Repository';
15+
1416
/**
1517
* The name and signature of the console command.
1618
*
@@ -30,22 +32,52 @@ class MakeMySqlRepository extends BaseCommand
3032
protected $description = 'Create a new MySql repository class';
3133

3234

33-
/**
34-
* Execute the console command.
35-
*
36-
* @return int
37-
*/
3835
public function handle(): void
3936
{
4037
$this->setArguments();
41-
$filenameWithPath = $this->relativeMysqlRepositoryPath . $this->mysqlRepositoryName . '.php';
42-
$this->checkDelete($filenameWithPath, $this->mysqlRepositoryName, "MySql Repository");
38+
39+
$filenameWithPath = $this->getFileNameWithPath(
40+
$this->relativeMysqlRepositoryPath,
41+
$this->mysqlRepositoryName
42+
);
43+
44+
$this->checkAndPrepare($filenameWithPath);
45+
46+
$this->finalized(
47+
$filenameWithPath,
48+
$this->mysqlRepositoryName,
49+
$this->generateBaseContent($filenameWithPath)
50+
);
51+
}
52+
53+
54+
private function getFileNameWithPath(string $relativePath, string $sectionName): string
55+
{
56+
return $relativePath . $sectionName . '.php';
57+
}
58+
59+
60+
private function checkAndPrepare(string $filenameWithPath)
61+
{
62+
$this->checkDelete($filenameWithPath, $this->mysqlRepositoryName, self::OBJECT_NAME);
4363
$this->checkDirectory($this->relativeMysqlRepositoryPath);
44-
$this->checkClassExist($this->repositoryNamespace, $this->mysqlRepositoryName, "MySql Repository");
45-
$columns = $this->getAllColumnsInTable($this->tableName);
46-
$this->checkEmpty($columns, $this->tableName);
64+
$this->checkClassExist($this->repositoryNamespace, $this->mysqlRepositoryName, self::OBJECT_NAME);
65+
}
66+
4767

48-
$mysqlRepoCreator = new CreatorMySqlRepository($columns,
68+
private function getColumnsOf(string $tableName): Collection
69+
{
70+
$columns = $this->getAllColumnsInTable($tableName);
71+
$this->checkEmpty($columns, $tableName);
72+
73+
return $columns;
74+
}
75+
76+
77+
private function generateBaseContent(string $filenameWithPath): string
78+
{
79+
$mysqlRepoCreator = new CreatorMySqlRepository(
80+
$this->getColumnsOf($this->tableName),
4981
$this->tableName,
5082
$this->entityName,
5183
$this->entityVariableName,
@@ -58,9 +90,7 @@ public function handle(): void
5890
$this->mysqlRepositoryStubsPath,
5991
$this->detectForeignKeys
6092
);
61-
$creator = new BaseCreator($mysqlRepoCreator);
62-
$baseContent = $creator->createClass($filenameWithPath, $this);
6393

64-
$this->finalized($filenameWithPath, $this->mysqlRepositoryName, $baseContent);
94+
return (new BaseCreator($mysqlRepoCreator))->createClass($filenameWithPath, $this);
6595
}
6696
}

0 commit comments

Comments
 (0)