Skip to content

Commit ce2943f

Browse files
refactor(Command): modify and refactor
- improvement code style - extract method
1 parent 4636a3a commit ce2943f

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

src/Commands/MakeEntity.php

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,65 @@ public function handle(): void
4040
$this->setArguments();
4141
$filenameWithPath = $this->relativeEntitiesPath . $this->entityName . '.php';
4242

43-
$this->checkDelete($filenameWithPath, $this->entityName, "Entity");
44-
$this->checkDirectory($this->relativeEntitiesPath);
45-
$this->checkClassExist($this->entityNamespace, $this->entityName, "Entity");
46-
47-
$columns = $this->getAllColumnsInTable($this->tableName);
48-
$this->checkEmpty($columns, $this->tableName);
43+
$this->checkAndPrepare($filenameWithPath);
44+
$columns = $this->getColumnsOf($this->tableName);
4945

5046
foreach ($columns as $_column) {
5147
$_column->COLUMN_NAME = Str::camel($_column->COLUMN_NAME);
5248
}
5349

54-
$entityCreator = new CreatorEntity($columns,
50+
$entityCreator = $this->getCreatorEntity($columns);
51+
$baseContent = $this->createBaseContent($entityCreator, $filenameWithPath);
52+
53+
$this->finalized($filenameWithPath, $this->entityName, $baseContent);
54+
}
55+
56+
/**
57+
* @param string $tableName
58+
* @return Collection
59+
*/
60+
private function getColumnsOf(string $tableName): Collection
61+
{
62+
$columns = $this->getAllColumnsInTable($tableName);
63+
$this->checkEmpty($columns, $tableName);
64+
65+
return $columns;
66+
}
67+
68+
/**
69+
* @param string $filenameWithPath
70+
* @return void
71+
*/
72+
private function checkAndPrepare(string $filenameWithPath): void
73+
{
74+
$this->checkDelete($filenameWithPath, $this->entityName, self::OBJECT_NAME);
75+
$this->checkDirectory($this->relativeEntitiesPath);
76+
$this->checkClassExist($this->entityNamespace, $this->entityName, self::OBJECT_NAME);
77+
}
78+
79+
/**
80+
* @param Collection $columns
81+
* @return CreatorEntity
82+
*/
83+
private function getCreatorEntity(Collection $columns): CreatorEntity
84+
{
85+
return new CreatorEntity($columns,
5586
$this->detectForeignKeys,
5687
$this->tableName,
5788
$this->entityName,
5889
$this->entityNamespace,
5990
$this->entityStubsPath
6091
);
61-
$creator = new BaseCreator($entityCreator);
62-
$baseContent = $creator->createClass($filenameWithPath, $this);
63-
64-
$this->finalized($filenameWithPath, $this->entityName, $baseContent);
92+
}
6593

94+
/**
95+
* @param CreatorEntity $entityCreator
96+
* @param string $filenameWithPath
97+
* @return string
98+
*/
99+
public function createBaseContent(CreatorEntity $entityCreator, string $filenameWithPath): string
100+
{
101+
$creator = new BaseCreator($entityCreator);
102+
return $creator->createClass($filenameWithPath, $this);
66103
}
67104
}

0 commit comments

Comments
 (0)