Skip to content

Commit 67fb09d

Browse files
committed
Merge remote-tracking branch 'origin/development'
2 parents 6b92a31 + e929575 commit 67fb09d

File tree

8 files changed

+275
-162
lines changed

8 files changed

+275
-162
lines changed

src/Commands/DBAuditCommand.php

Lines changed: 12 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22

33
namespace Vcian\LaravelDBAuditor\Commands;
44

5-
use Exception;
65
use Illuminate\Console\Command;
7-
use Illuminate\Support\Facades\Log;
86
use Vcian\LaravelDBAuditor\Constants\Constant;
9-
use Vcian\LaravelDBAuditor\Services\AuditService;
10-
11-
use function Termwind\{render};
127

138
class DBAuditCommand extends Command
149
{
@@ -17,81 +12,31 @@ class DBAuditCommand extends Command
1712
*
1813
* @var string
1914
*/
20-
protected $signature = 'db:audit {table?}';
15+
protected $signature = 'db:audit';
2116

2217
/**
2318
* The console command description.
2419
*
2520
* @var string
2621
*/
27-
protected $description = 'Database Audit : Check the Constraint';
22+
protected $description = 'Database Audit : Check Standard and Constrain';
2823

2924
/**
3025
* Execute the console command.
3126
*/
32-
public function handle(AuditService $auditService)
27+
public function handle()
3328
{
34-
try {
35-
$tableName = $this->argument('table');
36-
37-
$results = Constant::ARRAY_DECLARATION;
38-
39-
$referenceTable = null;
40-
$referenceField = null;
41-
42-
$constrains = [
43-
Constant::CONSTRAIN_PRIMARY_KEY,
44-
Constant::CONSTRAIN_INDEX_KEY,
45-
Constant::CONSTRAIN_UNIQUE_KEY,
46-
Constant::CONSTRAIN_FOREIGN_KEY,
47-
Constant::CONSTRAIN_ALL_KEY
48-
];
29+
$commandSelect = $this->choice(
30+
'Please Select',
31+
[Constant::STANDARD_COMMAND, Constant::CONSTRAIN_COMMAND]
32+
);
4933

50-
$userInput = $this->choice(
51-
'Please Select',
52-
$constrains
53-
);
54-
55-
if ($tableName) {
56-
$results = $auditService->checkConstrain($tableName, $userInput);
57-
if (!$results) {
58-
return render('<div class="w-100 px-1 p-1 bg-red-600 text-center">No Table Found</div>');
59-
}
60-
render(view('DBAuditor::constraint', ['tables' => $results]));
61-
62-
$checkUserHasFields = $auditService->getFieldByUserInput($tableName, $userInput);
63-
if ($checkUserHasFields) {
64-
$userSelection = $this->choice('Do you want to add constrain into your table?', ['Yes', 'No']);
65-
if ($userSelection == "Yes") {
66-
if ($userInput === Constant::CONSTRAIN_ALL_KEY) {
67-
$options = [Constant::CONSTRAIN_INDEX_KEY, Constant::CONSTRAIN_UNIQUE_KEY, Constant::CONSTRAIN_FOREIGN_KEY];
68-
if (!$auditService->checkTableHasPrimaryKey($tableName)) {
69-
array_push($options, Constant::CONSTRAIN_PRIMARY_KEY);
70-
}
71-
$userInput = $this->choice("Please select constraints which you want process", $options);
72-
}
73-
$selectField = $this->choice("Please Select Field where you want to apply " . strtolower($userInput) . " key", $checkUserHasFields);
74-
if ($userInput === Constant::CONSTRAIN_FOREIGN_KEY) {
75-
$referenceTable = $this->ask("Please add reference table name");
76-
$referenceField = $this->ask("Please add reference table primary key name");
77-
}
78-
79-
$auditService->addConstrain($tableName, $selectField, $userInput, $referenceTable, $referenceField);
80-
render('<div class="w-100 px-1 p-1 bg-green-600 text-center">Run Successfully</div>');
81-
}
82-
}
83-
} else {
84-
$results = $auditService->getList($userInput);
85-
if (!$results) {
86-
return render('<div class="w-100 px-1 p-1 bg-red-600 text-center">No Table Found</div>');
87-
}
88-
render(view('DBAuditor::constraint', ['tables' => $results]));
89-
}
90-
} catch (Exception $exception) {
91-
Log::error($exception->getMessage());
92-
return render('<div class="w-100 px-1 p-1 bg-red-600 text-center">' . $exception->getMessage() . '</div>');
34+
if ($commandSelect === Constant::STANDARD_COMMAND) {
35+
$this->call('db:standard');
9336
}
9437

95-
return self::SUCCESS;
38+
if ($commandSelect === Constant::CONSTRAIN_COMMAND) {
39+
$this->call('db:constrain');
40+
}
9641
}
9742
}

src/Commands/DBConstrainCommand.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
13+
class DBConstrainCommand extends Command
14+
{
15+
/**
16+
* The name and signature of the console command.
17+
*
18+
* @var string
19+
*/
20+
protected $signature = 'db:constrain {table?}';
21+
22+
/**
23+
* The console command description.
24+
*
25+
* @var string
26+
*/
27+
protected $description = 'Table Constrain Playground';
28+
29+
/**
30+
* Execute the console command.
31+
*/
32+
public function handle(AuditService $auditService)
33+
{
34+
try {
35+
$tableName = $this->argument('table');
36+
37+
$results = Constant::ARRAY_DECLARATION;
38+
39+
$referenceTable = null;
40+
$referenceField = null;
41+
42+
$constrains = [
43+
Constant::CONSTRAIN_PRIMARY_KEY,
44+
Constant::CONSTRAIN_INDEX_KEY,
45+
Constant::CONSTRAIN_UNIQUE_KEY,
46+
Constant::CONSTRAIN_FOREIGN_KEY,
47+
Constant::CONSTRAIN_ALL_KEY
48+
];
49+
50+
$userInput = $this->choice(
51+
'Please Select',
52+
$constrains
53+
);
54+
55+
if ($tableName) {
56+
$results = $auditService->checkConstrain($tableName, $userInput);
57+
if (!$results) {
58+
return render('<div class="w-100 px-1 p-1 bg-red-600 text-center">No Table Found</div>');
59+
}
60+
render(view('DBAuditor::constraint', ['tables' => $results]));
61+
62+
$checkUserHasFields = $auditService->getFieldByUserInput($tableName, $userInput);
63+
if ($checkUserHasFields) {
64+
$userSelection = $this->choice('Do you want to add constrain into your table?', ['Yes', 'No']);
65+
if ($userSelection == "Yes") {
66+
if ($userInput === Constant::CONSTRAIN_ALL_KEY) {
67+
$options = [Constant::CONSTRAIN_INDEX_KEY, Constant::CONSTRAIN_UNIQUE_KEY, Constant::CONSTRAIN_FOREIGN_KEY];
68+
if (!$auditService->checkTableHasPrimaryKey($tableName)) {
69+
array_push($options, Constant::CONSTRAIN_PRIMARY_KEY);
70+
}
71+
$userInput = $this->choice("Please select constraints which you want process", $options);
72+
}
73+
$selectField = $this->choice("Please Select Field where you want to apply " . strtolower($userInput) . " key", $checkUserHasFields);
74+
if ($userInput === Constant::CONSTRAIN_FOREIGN_KEY) {
75+
$referenceTable = $this->ask("Please add reference table name");
76+
$referenceField = $this->ask("Please add reference table primary key name");
77+
}
78+
79+
$auditService->addConstrain($tableName, $selectField, $userInput, $referenceTable, $referenceField);
80+
render('<div class="w-100 px-1 p-1 bg-green-600 text-center">Run Successfully</div>');
81+
}
82+
}
83+
} else {
84+
$results = $auditService->getList($userInput);
85+
if (!$results) {
86+
return render('<div class="w-100 px-1 p-1 bg-red-600 text-center">No Table Found</div>');
87+
}
88+
render(view('DBAuditor::constraint', ['tables' => $results]));
89+
}
90+
} catch (Exception $exception) {
91+
Log::error($exception->getMessage());
92+
return render('<div class="w-100 px-1 p-1 bg-red-600 text-center">' . $exception->getMessage() . '</div>');
93+
}
94+
95+
return self::SUCCESS;
96+
}
97+
}

src/Commands/DBStandardCommand.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22

3-
namespace Vcian\LaravelDBPlayground\Commands;
3+
namespace Vcian\LaravelDBAuditor\Commands;
44

55
use Illuminate\Console\Command;
6-
use Vcian\LaravelDBPlayground\Services\RuleService;
6+
use Vcian\LaravelDBAuditor\Constants\Constant;
7+
use Vcian\LaravelDBAuditor\Services\RuleService;
78
use function Termwind\render;
89

910
class DBStandardCommand extends Command
@@ -13,7 +14,7 @@ class DBStandardCommand extends Command
1314
*
1415
* @var string
1516
*/
16-
protected $signature = 'db:standard {table?}';
17+
protected $signature = 'db:standard';
1718

1819
/**
1920
* The console command description.
@@ -27,22 +28,35 @@ class DBStandardCommand extends Command
2728
*/
2829
public function handle(RuleService $ruleService)
2930
{
30-
$tableName = $this->argument('table');
31+
$tableStatus = $ruleService->tablesRule();
3132

32-
if($tableName) {
33-
$tableResult = $ruleService->tableRules($tableName);
34-
} else {
35-
$tableResult = $ruleService->tablesRule();
33+
if (!$tableStatus) {
34+
return render('<div class="w-100 px-1 p-1 bg-red-600 text-center"> 😢 No Table Found 😩 </div>');
3635
}
3736

38-
if (!$tableResult) {
39-
return render('<div class="w-100 px-1 p-1 bg-red-600 text-center">No Table Found</div>');
40-
}
37+
render(view('DBAuditor::standard', ['tableStatus' => $tableStatus]));
38+
39+
$continue = Constant::STATUS_TRUE;
40+
41+
do {
42+
43+
$tableName = $this->ask('Please select table if you want to see the report');
44+
45+
$tableStatus = $ruleService->tableRules($tableName);
46+
47+
if (!$tableStatus) {
48+
render('<div class="w-120 px-2 p-1 bg-red-600 text-center"> 😢 No Table Found 😩 </div>');
49+
} else {
50+
render(view('DBAuditor::fail_standard_table', ['tableStatus' => $tableStatus]));
51+
}
52+
53+
$report = $this->confirm("Do you want see other table report?");
54+
55+
if (!$report) {
56+
$continue = Constant::STATUS_FALSE;
57+
}
58+
} while ($continue === Constant::STATUS_TRUE);
4159

42-
return render(
43-
view('DBAuditor::standard', [
44-
'tables' => $tableResult,
45-
])
46-
);
60+
return self::SUCCESS;
4761
}
4862
}

src/Constants/Constant.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ class Constant
4242
public const PRIMARY_FILE_NAME = 'update_table_primary.php';
4343
public const FOREIGN_FILE_NAME = 'update_table_foreign.php';
4444

45+
public const STATUS_TRUE = true;
46+
public const STATUS_FALSE = false;
47+
48+
public const STANDARD_COMMAND = 'STANDARD';
49+
public const CONSTRAIN_COMMAND = 'CONSTRAIN';
4550
}

src/Providers/DBAuditorServiceProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class DBAuditorServiceProvider extends ServiceProvider
99

1010
protected $commands = [
1111
'Vcian\LaravelDBAuditor\Commands\DBAuditCommand',
12-
'Vcian\LaravelDBAuditor\Commands\DBStandardCommand'
12+
'Vcian\LaravelDBAuditor\Commands\DBStandardCommand',
13+
'Vcian\LaravelDBAuditor\Commands\DBConstrainCommand'
1314
];
1415
/**
1516
* Register services.
@@ -24,6 +25,6 @@ public function register(): void
2425
*/
2526
public function boot(): void
2627
{
27-
$this->loadViewsFrom(__DIR__ . '/../Views', 'DBAuditor');
28+
$this->loadViewsFrom(__DIR__ . '/../views', 'DBAuditor');
2829
}
2930
}

0 commit comments

Comments
 (0)