@@ -25,104 +25,86 @@ class DBConstrainCommand extends Command
25
25
*
26
26
* @var string
27
27
*/
28
- protected $ description = 'Table Constrain Playground ' ;
28
+ protected $ description = 'Table Constraint Playground ' ;
29
29
30
30
/**
31
31
* Execute the console command.
32
+ * @param AuditService
32
33
*/
33
34
public function handle (AuditService $ auditService )
34
35
{
35
36
try {
36
37
37
38
$ tableName = $ this ->components ->choice (
38
- ' Which table whould you like to audit? ' ,
39
+ __ ( ' Lang::messages.constraint.question.table_selection ' ) ,
39
40
$ auditService ->getTablesList ()
40
41
);
41
42
42
- $ data = [
43
- "table " => $ tableName ,
44
- "size " => $ auditService ->getTableSize ($ tableName ),
45
- "fields " => $ auditService ->getTableFields ($ tableName ),
46
- 'field_count ' => count ($ auditService ->getTableFields ($ tableName )),
47
- 'constrain ' => [
48
- 'primary ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_PRIMARY_KEY ),
49
- 'unique ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_UNIQUE_KEY ),
50
- 'foreign ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_FOREIGN_KEY ),
51
- 'index ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_INDEX_KEY )
52
- ]
53
- ];
54
-
55
- render (view ('DBAuditor::constraint ' , ['data ' => $ data ]));
43
+ $ this ->displayTable ($ tableName );
56
44
57
45
if ($ tableName ) {
58
46
59
- $ continue = Constant::STATUS_TRUE ;
60
- do {
47
+ $ flag = Constant::STATUS_TRUE ;
61
48
62
- $ noConstrainfields = $ auditService ->getNoConstrainFields ($ tableName );
63
- $ constrainList = $ auditService ->getConstrainList ($ tableName , $ noConstrainfields );
49
+ do {
50
+ $ continue = Constant::STATUS_TRUE ;
51
+ $ noConstrainfields = $ auditService ->getNoConstraintFields ($ tableName );
52
+ $ constrainList = $ auditService ->getConstraintList ($ tableName , $ noConstrainfields );
64
53
65
54
if ($ noConstrainfields ) {
66
55
67
- $ userInput = $ this ->confirm (" Do you want add more constrain? " );
56
+ $ userInput = $ this ->confirm (__ ( ' Lang::messages.constraint.question.continue ' ) );
68
57
69
58
if ($ userInput ) {
70
59
71
60
$ selectConstrain = $ this ->choice (
72
- ' Please select constrain which you want to add ' ,
61
+ __ ( ' Lang::messages.constraint.question.constraint_selection ' ) ,
73
62
$ constrainList
74
63
);
75
64
76
- if ($ selectConstrain === Constant::CONSTRAIN_PRIMARY_KEY || $ selectConstrain === Constant::CONSTRAIN_FOREIGN_KEY ) {
77
- $ fields = $ noConstrainfields ['integer ' ];
78
- } else {
79
- $ fields = $ noConstrainfields ['mix ' ];
80
- }
81
-
82
- $ selectField = $ this ->choice (
83
- 'Please select field where you want to add ' . $ selectConstrain ,
84
- $ fields
85
- );
86
-
87
- if ($ selectConstrain === Constant::CONSTRAIN_FOREIGN_KEY ) {
65
+ if ($ selectConstrain === Constant::CONSTRAINT_FOREIGN_KEY || $ selectConstrain === Constant::CONSTRAINT_UNIQUE_KEY ) {
88
66
$ tableHasValue = $ auditService ->tableHasValue ($ tableName );
89
67
90
68
if ($ tableHasValue ) {
91
- render ('<div class="w-120 px-2 p-1 bg-red-600 text-center"> 😢 Can not apply Foreign Key | Please trancate table 😎 </div> ' );
92
- } else {
93
- $ referenceTable = $ this ->ask ("Please add foreign table name " );
94
- $ referenceField = $ this ->ask ("Please add foreign table primary key name " );
95
- $ auditService ->addConstrain ($ tableName , $ selectField , $ selectConstrain , $ referenceTable , $ referenceField );
69
+ $ continue = Constant::STATUS_FALSE ;
70
+ render ('<div class="w-120 px-2 p-1 bg-red-600 text-center"> 😢 Can not apply ' . strtolower ($ selectConstrain ) . ' key | Please trancate table 😎 </div> ' );
96
71
}
97
72
}
98
73
99
- $ auditService -> addConstrain ( $ tableName , $ selectField , $ selectConstrain );
74
+ if ( $ continue ) {
100
75
101
- renderUsing ($ this ->output );
76
+ if ($ selectConstrain === Constant::CONSTRAINT_PRIMARY_KEY || $ selectConstrain === Constant::CONSTRAINT_FOREIGN_KEY ) {
77
+ $ fields = $ noConstrainfields ['integer ' ];
78
+ } else {
79
+ $ fields = $ noConstrainfields ['mix ' ];
80
+ }
102
81
103
- render ('<div class="w-120 px-2 p-1 bg-green-600 text-center"> 😎 Constrain Add Successfully 😎 </div> ' );
82
+ $ selectField = $ this ->choice (
83
+ __ ('Lang::messages.constraint.question.field_selection ' ).' ' . strtolower ($ selectConstrain ) . ' key ' ,
84
+ $ fields
85
+ );
104
86
105
- $ data = [
106
- "table " => $ tableName ,
107
- "size " => $ auditService ->getTableSize ($ tableName ),
108
- "fields " => $ auditService ->getTableFields ($ tableName ),
109
- 'field_count ' => count ($ auditService ->getTableFields ($ tableName )),
110
- 'constrain ' => [
111
- 'primary ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_PRIMARY_KEY ),
112
- 'unique ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_UNIQUE_KEY ),
113
- 'foreign ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_FOREIGN_KEY ),
114
- 'index ' => $ auditService ->getConstrainField ($ tableName , Constant::CONSTRAIN_INDEX_KEY )
115
- ]
116
- ];
87
+ if ($ selectConstrain === Constant::CONSTRAINT_FOREIGN_KEY ) {
88
+ $ referenceTable = $ this ->ask (__ ('Lang::messages.constraint.question.foreign_table ' ));
89
+ $ referenceField = $ this ->ask (__ ('Lang::messages.constraint.question.foreign_field ' ));
90
+ $ auditService ->addConstraint ($ tableName , $ selectField , $ selectConstrain , $ referenceTable , $ referenceField );
91
+ }
117
92
118
- render (view ('DBAuditor::constraint ' , ['data ' => $ data ]));
93
+ $ auditService ->addConstraint ($ tableName , $ selectField , $ selectConstrain );
94
+
95
+ renderUsing ($ this ->output );
96
+
97
+ render ('<div class="w-120 px-2 p-1 bg-green-600 text-center"> 😎 ' . __ ('Lang::messages.constraint.messages.success ' ) . ' 😎 </div> ' );
98
+
99
+ $ this ->displayTable ($ tableName );
100
+ }
119
101
} else {
120
- $ continue = Constant::STATUS_FALSE ;
102
+ $ flag = Constant::STATUS_FALSE ;
121
103
}
122
104
} else {
123
- $ continue = Constant::STATUS_FALSE ;
105
+ $ flag = Constant::STATUS_FALSE ;
124
106
}
125
- } while ($ continue === Constant::STATUS_TRUE );
107
+ } while ($ flag === Constant::STATUS_TRUE );
126
108
}
127
109
} catch (Exception $ exception ) {
128
110
Log::error ($ exception ->getMessage ());
@@ -131,4 +113,29 @@ public function handle(AuditService $auditService)
131
113
132
114
return self ::SUCCESS ;
133
115
}
116
+
117
+ /**
118
+ * Display selected table
119
+ * @param string $tableName
120
+ * @return render
121
+ */
122
+ public function displayTable ($ tableName )
123
+ {
124
+ $ auditService = resolve (AuditService::class);
125
+
126
+ $ data = [
127
+ "table " => $ tableName ,
128
+ "size " => $ auditService ->getTableSize ($ tableName ),
129
+ "fields " => $ auditService ->getTableFields ($ tableName ),
130
+ 'field_count ' => count ($ auditService ->getTableFields ($ tableName )),
131
+ 'constrain ' => [
132
+ 'primary ' => $ auditService ->getConstraintField ($ tableName , Constant::CONSTRAINT_PRIMARY_KEY ),
133
+ 'unique ' => $ auditService ->getConstraintField ($ tableName , Constant::CONSTRAINT_UNIQUE_KEY ),
134
+ 'foreign ' => $ auditService ->getConstraintField ($ tableName , Constant::CONSTRAINT_FOREIGN_KEY ),
135
+ 'index ' => $ auditService ->getConstraintField ($ tableName , Constant::CONSTRAINT_INDEX_KEY )
136
+ ]
137
+ ];
138
+
139
+ render (view ('DBAuditor::constraint ' , ['data ' => $ data ]));
140
+ }
134
141
}
0 commit comments