diff --git a/app/commands/ThreadAssetLogsCommand.php b/app/commands/ThreadAssetLogsCommand.php new file mode 100755 index 0000000000..7dd3eb21c2 --- /dev/null +++ b/app/commands/ThreadAssetLogsCommand.php @@ -0,0 +1,214 @@ +actionlog = new Actionlog(); + $this->assetLogs = $this->actionlog->getListingOfActionLogsChronologicalOrder(); + } + + /** + * Execute the console command. + * + * @return void + */ + public function fire() + { + + if (!$this->confirm( "Do you want to update your asset logs to be properly threaded? [yes|no]" )) { + return; + } + + foreach ($this->assetLogs as $assetLog) { + + if ($this->hasAssetChanged( $assetLog )) { + $this->resetCurrentAssetInformation( $assetLog ); + } + + if ($this->hasBegunNewChain( $assetLog )) { + $this->startOfCurrentThread = false; + continue; + } + + $this->updateAssetLogWithThreadInformation( $assetLog ); + + if ($this->hasReachedEndOfChain( $assetLog ) + ) { + $this->clearCurrentAssetInformation(); + } + + } + } + + /** + * hasAssetChanged + * + * @param $assetLog + * + * @return bool + * @author Vincent Sposato + * @version v1.0 + */ + protected function hasAssetChanged( $assetLog ) + { + + return $assetLog->asset_id !== $this->currentAssetId; + } + + /** + * resetCurrentAssetInformation + * + * @param $assetLog + * + * @author Vincent Sposato + * @version v1.0 + */ + protected function resetCurrentAssetInformation( $assetLog ) + { + + $this->currentAssetId = $assetLog->asset_id; + $this->currentAssetLogId = $assetLog->id; + $this->startOfCurrentThread = true; + } + + /** + * hasReachedEndOfChain + * + * @param $assetLog + * + * @return bool + * @author Vincent Sposato + * @version v1.0 + */ + protected function hasReachedEndOfChain( $assetLog ) + { + + return in_array( $assetLog->action_type, $this->threadFinalActionTypes ) + && $this->startOfCurrentThread == false; + } + + /** + * clearCurrentAssetInformation + * @author Vincent Sposato + * @version v1.0 + */ + protected function clearCurrentAssetInformation() + { + + $this->startOfCurrentThread = true; + $this->currentAssetLogId = null; + $this->currentAssetId = null; + } + + /** + * updateAssetLogWithThreadInformation + * + * @param $assetLog + * + * @author Vincent Sposato + * @version v1.0 + */ + protected function updateAssetLogWithThreadInformation( $assetLog ) + { + + $loadedAssetLog = Actionlog::find( $assetLog->id ); + + $loadedAssetLog->thread_id = $this->currentAssetLogId; + + $loadedAssetLog->update(); + + unset( $loadedAssetLog ); + } + + /** + * hasBegunNewChain + * + * @param $assetLog + * + * @return bool + * @author Vincent Sposato + * @version v1.0 + */ + protected function hasBegunNewChain( $assetLog ) + { + + return in_array( $assetLog->action_type, $this->threadStartingActionTypes ) + && $this->startOfCurrentThread == true; + } + + }