6
6
use Illuminate \Console \Command ;
7
7
use Illuminate \Support \Facades \Log ;
8
8
use Vcian \LaravelDBAuditor \Constants \Constant ;
9
- use Vcian \LaravelDBAuditor \Services \ AuditService ;
9
+ use Vcian \LaravelDBAuditor \Traits \ Audit ;
10
10
use function Termwind \{render };
11
11
use function Termwind \{renderUsing };
12
12
13
13
class DBConstraintCommand extends Command
14
14
{
15
+ use Audit;
15
16
/**
16
17
* @var bool
17
18
*/
@@ -37,11 +38,10 @@ class DBConstraintCommand extends Command
37
38
public function handle (): int |string
38
39
{
39
40
try {
40
- $ auditService = app (AuditService::class);
41
-
41
+
42
42
$ tableName = $ this ->components ->choice (
43
43
__ ('Lang::messages.constraint.question.table_selection ' ),
44
- $ auditService -> getTablesList ()
44
+ $ this -> getTableList ()
45
45
);
46
46
47
47
$ this ->displayTable ($ tableName );
@@ -51,15 +51,15 @@ public function handle(): int|string
51
51
$ continue = Constant::STATUS_TRUE ;
52
52
53
53
do {
54
- $ noConstraintFields = $ auditService ->getNoConstraintFields ($ tableName );
54
+ $ noConstraintFields = $ this ->getNoConstraintFields ($ tableName );
55
55
56
56
if (empty ($ noConstraintFields )) {
57
57
$ continue = Constant::STATUS_FALSE ;
58
58
} else {
59
59
if ($ this ->confirm (__ ('Lang::messages.constraint.question.continue ' ))) {
60
60
61
61
$ this ->skip = Constant::STATUS_FALSE ;
62
- $ constraintList = $ auditService ->getConstraintList ($ tableName , $ noConstraintFields );
62
+ $ constraintList = $ this ->getConstraintList ($ tableName , $ noConstraintFields );
63
63
$ selectConstrain = $ this ->choice (
64
64
__ ('Lang::messages.constraint.question.constraint_selection ' ),
65
65
$ constraintList
@@ -86,18 +86,17 @@ public function handle(): int|string
86
86
*/
87
87
public function displayTable (string $ tableName ): void
88
88
{
89
- $ auditService = app (AuditService::class);
90
89
91
90
$ data = [
92
91
"table " => $ tableName ,
93
- "size " => $ auditService ->getTableSize ($ tableName ),
94
- "fields " => $ auditService -> getTableFields ($ tableName ),
95
- 'field_count ' => count ($ auditService -> getTableFields ($ tableName )),
92
+ "size " => $ this ->getTableSize ($ tableName ),
93
+ "fields " => $ this -> getFieldsDetails ($ tableName ),
94
+ 'field_count ' => count ($ this -> getFieldsDetails ($ tableName )),
96
95
'constrain ' => [
97
- 'primary ' => $ auditService ->getConstraintField ($ tableName , Constant::CONSTRAINT_PRIMARY_KEY ),
98
- 'unique ' => $ auditService ->getConstraintField ($ tableName , Constant::CONSTRAINT_UNIQUE_KEY ),
99
- 'foreign ' => $ auditService ->getConstraintField ($ tableName , Constant::CONSTRAINT_FOREIGN_KEY ),
100
- 'index ' => $ auditService ->getConstraintField ($ tableName , Constant::CONSTRAINT_INDEX_KEY )
96
+ 'primary ' => $ this ->getConstraintField ($ tableName , Constant::CONSTRAINT_PRIMARY_KEY ),
97
+ 'unique ' => $ this ->getConstraintField ($ tableName , Constant::CONSTRAINT_UNIQUE_KEY ),
98
+ 'foreign ' => $ this ->getConstraintField ($ tableName , Constant::CONSTRAINT_FOREIGN_KEY ),
99
+ 'index ' => $ this ->getConstraintField ($ tableName , Constant::CONSTRAINT_INDEX_KEY )
101
100
]
102
101
];
103
102
@@ -131,23 +130,22 @@ public function successMessage(string $message): void
131
130
*/
132
131
public function foreignKeyConstraint (string $ tableName , string $ selectField ): void
133
132
{
134
- $ auditService = app (AuditService::class);
135
133
$ foreignContinue = Constant::STATUS_FALSE ;
136
134
$ referenceField = Constant::NULL ;
137
135
$ fields = Constant::ARRAY_DECLARATION ;
138
136
139
137
do {
140
- $ referenceTable = $ this ->anticipate (__ ('Lang::messages.constraint.question.foreign_table ' ), $ auditService ->getTablesList ());
138
+ $ referenceTable = $ this ->anticipate (__ ('Lang::messages.constraint.question.foreign_table ' ), $ this ->getTablesList ());
141
139
142
- if ($ referenceTable && $ auditService ->checkTableExistOrNot ($ referenceTable )) {
140
+ if ($ referenceTable && $ this ->checkTableExistOrNot ($ referenceTable )) {
143
141
144
- foreach ($ auditService ->getTableFields ($ referenceTable ) as $ field ) {
142
+ foreach ($ this ->getTableFields ($ referenceTable ) as $ field ) {
145
143
$ fields [] = $ field ->COLUMN_NAME ;
146
144
}
147
145
do {
148
146
$ referenceField = $ this ->anticipate (__ ('Lang::messages.constraint.question.foreign_field ' ), $ fields );
149
147
150
- if (!$ referenceField || !$ auditService ->checkFieldExistOrNot ($ referenceTable , $ referenceField )) {
148
+ if (!$ referenceField || !$ this ->checkFieldExistOrNot ($ referenceTable , $ referenceField )) {
151
149
$ this ->errorMessage (__ ('Lang::messages.constraint.error_message.field_not_found ' ));
152
150
} else {
153
151
$ foreignContinue = Constant::STATUS_TRUE ;
@@ -158,8 +156,8 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
158
156
}
159
157
} while ($ foreignContinue === Constant::STATUS_FALSE );
160
158
161
- $ referenceFieldType = $ auditService ->getFieldDataType ($ referenceTable , $ referenceField );
162
- $ selectedFieldType = $ auditService ->getFieldDataType ($ tableName , $ selectField );
159
+ $ referenceFieldType = $ this ->getFieldDataType ($ referenceTable , $ referenceField );
160
+ $ selectedFieldType = $ this ->getFieldDataType ($ tableName , $ selectField );
163
161
164
162
if ($ referenceTable === $ tableName ) {
165
163
$ this ->errorMessage (__ ('Lang::messages.constraint.error_message.foreign_selected_table_match ' , ['foreign ' => $ referenceTable , 'selected ' => $ tableName ]));
@@ -181,7 +179,7 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
181
179
' );
182
180
$ this ->errorMessage (__ ('Lang::messages.constraint.error_message.foreign_not_apply ' ));
183
181
} else {
184
- $ auditService ->addConstraint ($ tableName , $ selectField , Constant::CONSTRAINT_FOREIGN_KEY , $ referenceTable , $ referenceField );
182
+ $ this ->addConstraint ($ tableName , $ selectField , Constant::CONSTRAINT_FOREIGN_KEY , $ referenceTable , $ referenceField );
185
183
}
186
184
}
187
185
}
@@ -195,10 +193,8 @@ public function foreignKeyConstraint(string $tableName, string $selectField): vo
195
193
public function selectedConstraint (string $ selectConstrain , array $ noConstraintFields , string $ tableName ): void
196
194
{
197
195
198
- $ auditService = app (AuditService::class);
199
-
200
196
if ($ selectConstrain === Constant::CONSTRAINT_FOREIGN_KEY ) {
201
- $ tableHasValue = $ auditService ->tableHasValue ($ tableName );
197
+ $ tableHasValue = $ this ->tableHasValue ($ tableName );
202
198
203
199
if ($ tableHasValue ) {
204
200
$ this ->errorMessage (__ ('Lang::messages.constraint.error_message.constraint_not_apply ' , ['constraint ' => strtolower ($ selectConstrain )]));
@@ -213,7 +209,7 @@ public function selectedConstraint(string $selectConstrain, array $noConstraintF
213
209
}
214
210
215
211
if ($ selectConstrain === Constant::CONSTRAINT_UNIQUE_KEY ) {
216
- $ fields = $ auditService ->getUniqueFields ($ tableName , $ noConstraintFields ['mix ' ]);
212
+ $ fields = $ this ->getUniqueFields ($ tableName , $ noConstraintFields ['mix ' ]);
217
213
if (empty ($ fields )) {
218
214
$ this ->errorMessage (__ ('Lang::messages.constraint.error_message.unique_constraint_not_apply ' ));
219
215
}
@@ -228,7 +224,7 @@ public function selectedConstraint(string $selectConstrain, array $noConstraintF
228
224
if ($ selectConstrain === Constant::CONSTRAINT_FOREIGN_KEY ) {
229
225
$ this ->foreignKeyConstraint ($ tableName , $ selectField );
230
226
} else {
231
- $ auditService ->addConstraint ($ tableName , $ selectField , $ selectConstrain );
227
+ $ this ->addConstraint ($ tableName , $ selectField , $ selectConstrain );
232
228
}
233
229
}
234
230
}
0 commit comments