Skip to content

Commit 84e953c

Browse files
committed
Resolved class naming convention issue.
1 parent 1dce716 commit 84e953c

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

src/Commands/DBConstraintCommand.php

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace Vcian\LaravelDBAuditor\Commands;
4+
5+
use Exception;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\Log;
8+
use Vcian\LaravelDBAuditor\Constants\Constant;
9+
use Vcian\LaravelDBAuditor\Services\AuditService;
10+
11+
use function Termwind\{render};
12+
use function Termwind\{renderUsing};
13+
14+
class DBConstraintCommand extends Command
15+
{
16+
/**
17+
* The name and signature of the console command.
18+
*
19+
* @var string
20+
*/
21+
protected $signature = 'db:constraint {table?}';
22+
23+
/**
24+
* The console command description.
25+
*
26+
* @var string
27+
*/
28+
protected $description = 'Table Constraint Playground';
29+
30+
/**
31+
* Execute the console command.
32+
* @param AuditService
33+
*/
34+
public function handle(AuditService $auditService)
35+
{
36+
try {
37+
38+
$tableName = $this->components->choice(
39+
__('Lang::messages.constraint.question.table_selection'),
40+
$auditService->getTablesList()
41+
);
42+
43+
$this->displayTable($tableName);
44+
45+
if ($tableName) {
46+
47+
$flag = Constant::STATUS_TRUE;
48+
49+
do {
50+
$continue = Constant::STATUS_TRUE;
51+
$noConstrainfields = $auditService->getNoConstraintFields($tableName);
52+
$constrainList = $auditService->getConstraintList($tableName, $noConstrainfields);
53+
54+
if ($noConstrainfields) {
55+
56+
$userInput = $this->confirm(__('Lang::messages.constraint.question.continue'));
57+
58+
if ($userInput) {
59+
60+
$selectConstrain = $this->choice(
61+
__('Lang::messages.constraint.question.constraint_selection'),
62+
$constrainList
63+
);
64+
65+
if ($selectConstrain === Constant::CONSTRAINT_FOREIGN_KEY || $selectConstrain === Constant::CONSTRAINT_UNIQUE_KEY) {
66+
$tableHasValue = $auditService->tableHasValue($tableName);
67+
68+
if ($tableHasValue) {
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>');
71+
}
72+
}
73+
74+
if ($continue) {
75+
76+
if ($selectConstrain === Constant::CONSTRAINT_PRIMARY_KEY || $selectConstrain === Constant::CONSTRAINT_FOREIGN_KEY) {
77+
$fields = $noConstrainfields['integer'];
78+
} else {
79+
$fields = $noConstrainfields['mix'];
80+
}
81+
82+
$selectField = $this->choice(
83+
__('Lang::messages.constraint.question.field_selection').' '. strtolower($selectConstrain) . ' key',
84+
$fields
85+
);
86+
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+
}
92+
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.success_message.constraint_added') . ' 😎 </div>');
98+
99+
$this->displayTable($tableName);
100+
}
101+
} else {
102+
$flag = Constant::STATUS_FALSE;
103+
}
104+
} else {
105+
$flag = Constant::STATUS_FALSE;
106+
}
107+
} while ($flag === Constant::STATUS_TRUE);
108+
}
109+
} catch (Exception $exception) {
110+
Log::error($exception->getMessage());
111+
return $exception->getMessage();
112+
}
113+
114+
return self::SUCCESS;
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+
}
141+
}

src/Providers/DBAuditorServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class DBAuditorServiceProvider extends ServiceProvider
1010
protected $commands = [
1111
'Vcian\LaravelDBAuditor\Commands\DBAuditCommand',
1212
'Vcian\LaravelDBAuditor\Commands\DBStandardCommand',
13-
'Vcian\LaravelDBAuditor\Commands\DBConstrainCommand'
13+
'Vcian\LaravelDBAuditor\Commands\DBConstraintCommand'
1414
];
1515
/**
1616
* Register services.

0 commit comments

Comments
 (0)