Skip to content

Commit f648a0e

Browse files
refactor(Command): modify and refactor MakeRepository
1 parent 4d7b250 commit f648a0e

File tree

1 file changed

+64
-28
lines changed

1 file changed

+64
-28
lines changed

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
}

0 commit comments

Comments
 (0)