diff --git a/src/Commands/DBTrackCommand.php b/src/Commands/DBTrackCommand.php index 66ff385..f1fa2c9 100644 --- a/src/Commands/DBTrackCommand.php +++ b/src/Commands/DBTrackCommand.php @@ -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; @@ -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 ); diff --git a/src/Constants/Constant.php b/src/Constants/Constant.php index 2fc799a..2a70686 100644 --- a/src/Constants/Constant.php +++ b/src/Constants/Constant.php @@ -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'; } diff --git a/src/Traits/DBMigrationTrack.php b/src/Traits/DBMigrationTrack.php index 428a30c..f6b2b3e 100644 --- a/src/Traits/DBMigrationTrack.php +++ b/src/Traits/DBMigrationTrack.php @@ -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 = []; @@ -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; @@ -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;