2018-05-16 19:20:43 -07:00
< ? php
namespace App\Console\Commands ;
use App\Models\Asset ;
2019-03-13 20:12:03 -07:00
use Illuminate\Console\Command ;
2018-05-16 19:20:43 -07:00
class SyncAssetCounters extends Command
{
/**
* The name and signature of the console command .
*
* @ var string
*/
protected $signature = 'snipeit:counter-sync' ;
/**
* The console command description .
*
* @ var string
*/
protected $description = 'Syncs checkedout, checked in, and requested counters for assets' ;
/**
* Create a new command instance .
*
* @ return void
*/
public function __construct ()
{
parent :: __construct ();
}
/**
* Execute the console command .
*
* @ return mixed
*/
public function handle ()
{
$start = microtime ( true );
2023-01-05 17:57:52 -08:00
// We need the whole count of all assets in order to set up the progress bar
$assets_count = Asset :: withTrashed () -> count ();
$bar = $this -> output -> createProgressBar ( $assets_count );
2018-07-24 22:40:05 -07:00
$assets = Asset :: withCount ( 'checkins as checkins_count' , 'checkouts as checkouts_count' , 'userRequests as user_requests_count' )
2023-01-05 18:04:06 -08:00
-> withTrashed () -> chunk ( 100 , function ( $assets ) use ( $bar ) {
2023-01-05 17:57:52 -08:00
if ( $assets -> count () > 0 ) {
foreach ( $assets as $asset ) {
$asset -> checkin_counter = ( int ) $asset -> checkins_count ;
$asset -> checkout_counter = ( int ) $asset -> checkouts_count ;
$asset -> requests_counter = ( int ) $asset -> user_requests_count ;
$asset -> unsetEventDispatcher ();
$asset -> save ();
$bar -> advance ();
\Log :: debug ( 'Asset: ' . $asset -> id . ' has ' . $asset -> checkin_counter . ' checkins, ' . $asset -> checkout_counter . ' checkouts, and ' . $asset -> requests_counter . ' requests' );
}
2018-05-16 19:20:43 -07:00
} else {
$this -> info ( 'No assets to sync' );
}
2023-01-05 17:57:52 -08:00
});
$bar -> finish ();
$time_elapsed_secs = microtime ( true ) - $start ;
$this -> info ( " \n Sync of " . $assets_count . ' assets executed in ' . $time_elapsed_secs . ' seconds' );
2018-05-16 19:20:43 -07:00
}
}