2
2
3
3
namespace Eghamat24 \DatabaseRepository \Commands ;
4
4
5
- //use Illuminate\Console\Command;
6
- use Illuminate \Support \Str ;
5
+ use Illuminate \Support \Collection ;
7
6
use Eghamat24 \DatabaseRepository \Creators \BaseCreator ;
8
7
use Eghamat24 \DatabaseRepository \Creators \CreatorRepository ;
9
8
use Eghamat24 \DatabaseRepository \CustomMySqlQueries ;
10
9
11
10
class MakeRepository extends BaseCommand
12
11
{
12
+ use CustomMySqlQueries;
13
+
14
+ private const OBJECT_NAME = 'Repository ' ;
15
+
13
16
/**
14
17
* The name and signature of the console command.
15
18
*
@@ -28,36 +31,36 @@ class MakeRepository extends BaseCommand
28
31
*/
29
32
protected $ description = 'Create a new repository ' ;
30
33
31
- use CustomMySqlQueries;
32
-
33
- /**
34
- * Execute the console command.
35
- *
36
- * @return int
37
- */
38
34
public function handle (): void
39
35
{
40
36
$ this ->checkDatabasesExist ();
41
37
$ this ->checkStrategyName ();
42
38
43
39
$ 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 ;
50
41
$ relativeRepositoryPath = config ('repository.path.relative.repositories ' ) . "$ this ->entityName " . DIRECTORY_SEPARATOR ;
51
- $ repositoryStubsPath = __DIR__ . ' /../../ ' . config ( ' repository.path.stub.repositories.base ' );
42
+
52
43
$ 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 ' ,
61
64
$ sqlRepositoryName ,
62
65
$ repositoryStubsPath ,
63
66
$ this ->detectForeignKeys ,
@@ -69,12 +72,45 @@ public function handle(): void
69
72
$ this ->interfaceName ,
70
73
$ this ->repositoryNamespace ,
71
74
$ this ->selectedDb ,
72
- $ redisRepositoryVariable ,
73
- $ redisRepositoryName ,
75
+ ' redisRepository ' ,
76
+ ' Redis ' . $ repositoryName ,
74
77
$ this ->strategyName
75
78
);
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
+ {
76
100
$ 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 );
79
115
}
80
116
}
0 commit comments