Skip to content

Commit 8a6580a

Browse files
refactor(Command): refactor MakeFactory
1 parent dbc9243 commit 8a6580a

File tree

1 file changed

+56
-17
lines changed

1 file changed

+56
-17
lines changed

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
}

0 commit comments

Comments
 (0)