Skip to content

Commit

Permalink
Merge pull request vcian#43 from vc-urvin/main
Browse files Browse the repository at this point in the history
Replace static value with constant and change ordering of the field
  • Loading branch information
ruchit288 authored Jan 19, 2024
2 parents f72000d + 140a57a commit 855d234
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 48 deletions.
33 changes: 17 additions & 16 deletions src/Commands/DBTrackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Vcian\LaravelDBAuditor\Commands;

use Illuminate\Console\Command;
use Vcian\LaravelDBAuditor\Constants\Constant;
use Vcian\LaravelDBAuditor\Traits\DBMigrationTrack;

use function Laravel\Prompts\table;
Expand Down Expand Up @@ -31,36 +32,36 @@ class DBTrackCommand extends Command
*/
public function handle()
{
$actionKeywords = ['U' => 'Update', 'u' => 'Update', 'c' => 'Create', 'C' => 'Create'];
$statusKeywords = ['M' => 'Migrated', 'm' => 'Migrated', 'P' => 'Pending', 'p' => 'Pending'];
$actionKeywords = ['U' => Constant::UPDATE, 'u' => Constant::UPDATE, 'c' => Constant::CREATE, 'C' => Constant::CREATE];
$statusKeywords = ['M' => Constant::MIGRATED, 'm' => Constant::MIGRATED, 'P' => Constant::PENDING, 'p' => Constant::PENDING];

$data = $this->collectDataFromFile('command');
$data = $this->collectDataFromFile();

if ($this->option('table')) {
if ($this->option(Constant::TABLE)) {

$data = $this->filter('table', $data, $this->option('table'));
$data = $this->filter(Constant::TABLE, $data, $this->option(Constant::TABLE));

} elseif ($this->option('action')) {
} elseif ($this->option(Constant::ACTION)) {

if (array_key_exists($this->option('action'), $actionKeywords)) {
$action = $actionKeywords[$this->option('action')];
if (array_key_exists($this->option(Constant::ACTION), $actionKeywords)) {
$action = $actionKeywords[$this->option(Constant::ACTION)];
} else {
$action = ucfirst($this->option('action'));
$action = ucfirst($this->option(Constant::ACTION));
}
$data = $this->filter('action', $data, $action);
$data = $this->filter(Constant::ACTION, $data, $action);

} elseif ($this->option('status')) {
} elseif ($this->option(Constant::STATUS)) {

if (array_key_exists($this->option('status'), $statusKeywords)) {
$status = $statusKeywords[$this->option('status')];
if (array_key_exists($this->option(Constant::STATUS), $statusKeywords)) {
$status = $statusKeywords[$this->option(Constant::STATUS)];
} else {
$status = ucfirst($this->option('status'));
$status = ucfirst($this->option(Constant::STATUS));
}
$data = $this->filter('status', $data, $status);
$data = $this->filter(Constant::STATUS, $data, $status);
}

table(
['Date', 'Table', 'Fields', 'Action', 'File Name', 'Status', 'Created By'],
['Date', 'File Name', 'Table', 'Fields', 'Action', 'Status', 'Created By'],
$data
);

Expand Down
6 changes: 6 additions & 0 deletions src/Constants/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,10 @@ class Constant
public const PENDING = 'Pending';

public const MIGRATED = 'Migrated';

public const TABLE = 'table';

public const ACTION = 'action';

public const STATUS = 'status';
}
49 changes: 17 additions & 32 deletions src/Traits/DBMigrationTrack.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait DBMigrationTrack
* Get Database related information from the migration file.
* Collect all the details into one place.
*/
public function collectDataFromFile($type = 'command')
public function collectDataFromFile()
{
$data = [];

Expand All @@ -22,32 +22,17 @@ public function collectDataFromFile($type = 'command')

$fileName = $file['basename'];

if ($type === 'command') {
array_push($data,
[
$this->getMigrationDate($file['filename']),
$this->getMigrationTableName($fileName),
$this->replaceStringWithDots($this->getMigrationFieldName($fileName)),
$this->getMigrationAction($fileName),
$fileName,
$this->getMigrationStatus($file['filename']),
$this->getMigrationCreatedBy($fileName),
]
);
} else {
array_push($data,
[
'date' => $this->getMigrationDate($file['filename']),
'table' => $this->getMigrationTableName($fileName),
'fields' => $this->getMigrationFieldName($fileName),
'action' => $this->getMigrationAction($fileName),
'file' => $fileName,
'status' => $this->getMigrationStatus($file['filename']),
'createdby' => $this->getMigrationCreatedBy($fileName),
]
);
}

array_push($data,
[
$this->getMigrationDate($file['filename']),
$fileName,
$this->getMigrationTableName($fileName),
$this->replaceStringWithDots($this->getMigrationFieldName($fileName)),
$this->getMigrationAction($fileName),
$this->getMigrationStatus($file['filename']),
$this->getMigrationCreatedBy($fileName),
]
);
}

return $data;
Expand All @@ -61,11 +46,11 @@ public function filter($filterType, $data, $filter)
$result = array_filter($data, function ($item) use ($filter, $filterType) {

switch ($filterType) {
case 'table':
return $item[1] === $filter;
case 'action':
return $item[3] === $filter;
case 'status':
case Constant::TABLE:
return $item[2] === $filter;
case Constant::ACTION:
return $item[4] === $filter;
case Constant::STATUS:
return $item[5] === $filter;
default:
return $item;
Expand Down

0 comments on commit 855d234

Please sign in to comment.