Skip to content

Commit cc9d8b6

Browse files
authored
Merge pull request #32 from saMahmoudzadeh/refactor/MakeMySqlRepository
Refactor MakeMySqlRepository Command
2 parents 692f853 + 9f25848 commit cc9d8b6

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

src/Commands/MakeMySqlRepository.php

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

33
namespace Eghamat24\DatabaseRepository\Commands;
44

5-
use Illuminate\Support\Str;
65
use Eghamat24\DatabaseRepository\Creators\BaseCreator;
7-
use Eghamat24\DatabaseRepository\Creators\CreatorEntity;
86
use Eghamat24\DatabaseRepository\Creators\CreatorMySqlRepository;
97
use Eghamat24\DatabaseRepository\CustomMySqlQueries;
10-
use Illuminate\Console\Command;
8+
use Illuminate\Support\Collection;
119

1210
class MakeMySqlRepository extends BaseCommand
1311
{
12+
use CustomMySqlQueries;
13+
14+
private const OBJECT_NAME = 'MySql Repository';
15+
1416
/**
1517
* The name and signature of the console command.
1618
*
@@ -29,24 +31,63 @@ class MakeMySqlRepository extends BaseCommand
2931
*/
3032
protected $description = 'Create a new MySql repository class';
3133

32-
use CustomMySqlQueries;
3334

34-
/**
35-
* Execute the console command.
36-
*
37-
* @return int
38-
*/
3935
public function handle(): void
4036
{
4137
$this->setArguments();
42-
$filenameWithPath = $this->relativeMysqlRepositoryPath . $this->mysqlRepositoryName . '.php';
43-
$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);
4463
$this->checkDirectory($this->relativeMysqlRepositoryPath);
45-
$this->checkClassExist($this->repositoryNamespace, $this->mysqlRepositoryName, "MySql Repository");
46-
$columns = $this->getAllColumnsInTable($this->tableName);
47-
$this->checkEmpty($columns, $this->tableName);
64+
$this->checkClassExist($this->repositoryNamespace, $this->mysqlRepositoryName, self::OBJECT_NAME);
65+
}
66+
67+
68+
private function getColumnsOf(string $tableName): Collection
69+
{
70+
$columns = $this->getAllColumnsInTable($tableName);
71+
$this->checkEmpty($columns, $tableName);
72+
73+
return $columns;
74+
}
4875

49-
$mysqlRepoCreator = new CreatorMySqlRepository($columns,
76+
77+
private function generateBaseContent(string $filenameWithPath): string
78+
{
79+
$mysqlRepoCreator = $this->makeMySqlRepoCreator();
80+
81+
return (new BaseCreator($mysqlRepoCreator))->createClass($filenameWithPath, $this);
82+
}
83+
84+
/**
85+
* @return CreatorMySqlRepository
86+
*/
87+
private function makeMySqlRepoCreator(): CreatorMySqlRepository
88+
{
89+
return new CreatorMySqlRepository(
90+
$this->getColumnsOf($this->tableName),
5091
$this->tableName,
5192
$this->entityName,
5293
$this->entityVariableName,
@@ -59,9 +100,5 @@ public function handle(): void
59100
$this->mysqlRepositoryStubsPath,
60101
$this->detectForeignKeys
61102
);
62-
$creator = new BaseCreator($mysqlRepoCreator);
63-
$baseContent = $creator->createClass($filenameWithPath, $this);
64-
65-
$this->finalized($filenameWithPath, $this->mysqlRepositoryName, $baseContent);
66103
}
67104
}

0 commit comments

Comments
 (0)