@@ -25,104 +25,86 @@ class DBConstrainCommand extends Command
2525 *
2626 * @var string
2727 */
28- protected $ description = 'Table Constrain Playground ' ;
28+ protected $ description = 'Table Constraint Playground ' ;
2929
3030 /**
3131 * Execute the console command.
32+ * @param AuditService
3233 */
3334 public function handle (AuditService $ auditService )
3435 {
3536 try {
3637
3738 $ tableName = $ this ->components ->choice (
38- ' Which table whould you like to audit? ' ,
39+ __ ( ' Lang::messages.constraint.question.table_selection ' ) ,
3940 $ auditService ->getTablesList ()
4041 );
4142
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 );
5644
5745 if ($ tableName ) {
5846
59- $ continue = Constant::STATUS_TRUE ;
60- do {
47+ $ flag = Constant::STATUS_TRUE ;
6148
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 );
6453
6554 if ($ noConstrainfields ) {
6655
67- $ userInput = $ this ->confirm (" Do you want add more constrain? " );
56+ $ userInput = $ this ->confirm (__ ( ' Lang::messages.constraint.question.continue ' ) );
6857
6958 if ($ userInput ) {
7059
7160 $ selectConstrain = $ this ->choice (
72- ' Please select constrain which you want to add ' ,
61+ __ ( ' Lang::messages.constraint.question.constraint_selection ' ) ,
7362 $ constrainList
7463 );
7564
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 ) {
8866 $ tableHasValue = $ auditService ->tableHasValue ($ tableName );
8967
9068 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> ' );
9671 }
9772 }
9873
99- $ auditService -> addConstrain ( $ tableName , $ selectField , $ selectConstrain );
74+ if ( $ continue ) {
10075
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+ }
10281
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+ );
10486
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+ }
11792
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+ }
119101 } else {
120- $ continue = Constant::STATUS_FALSE ;
102+ $ flag = Constant::STATUS_FALSE ;
121103 }
122104 } else {
123- $ continue = Constant::STATUS_FALSE ;
105+ $ flag = Constant::STATUS_FALSE ;
124106 }
125- } while ($ continue === Constant::STATUS_TRUE );
107+ } while ($ flag === Constant::STATUS_TRUE );
126108 }
127109 } catch (Exception $ exception ) {
128110 Log::error ($ exception ->getMessage ());
@@ -131,4 +113,29 @@ public function handle(AuditService $auditService)
131113
132114 return self ::SUCCESS ;
133115 }
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+ }
134141}
0 commit comments