2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
2016-03-24 18:02:45 +01:00
|
|
|
|
|
|
|
use App\Models\Document;
|
2017-01-30 20:40:43 +01:00
|
|
|
use DateTime;
|
2016-03-24 18:02:45 +01:00
|
|
|
use Illuminate\Console\Command;
|
2017-05-01 14:46:57 +02:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2016-03-24 18:02:45 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class RemoveOrphanedDocuments.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2016-03-24 18:02:45 +01:00
|
|
|
class RemoveOrphanedDocuments extends Command
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-03-24 18:02:45 +01:00
|
|
|
protected $name = 'ninja:remove-orphaned-documents';
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-03-24 18:02:45 +01:00
|
|
|
protected $description = 'Removes old documents not associated with an expense or invoice';
|
2017-05-01 14:17:52 +02:00
|
|
|
|
2016-03-24 18:02:45 +01:00
|
|
|
public function fire()
|
|
|
|
{
|
2017-10-24 09:59:26 +02:00
|
|
|
$this->info(date('r').' Running RemoveOrphanedDocuments...');
|
2016-03-24 18:02:45 +01:00
|
|
|
|
2017-05-01 14:17:52 +02:00
|
|
|
if ($database = $this->option('database')) {
|
|
|
|
config(['database.default' => $database]);
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
$documents = Document::whereRaw('invoice_id IS NULL AND expense_id IS NULL AND updated_at <= ?', [new DateTime('-1 hour')])
|
2016-03-24 18:02:45 +01:00
|
|
|
->get();
|
2017-05-01 14:17:52 +02:00
|
|
|
|
2018-01-16 14:48:56 +01:00
|
|
|
$this->info($documents->count() . ' orphaned document(s) found');
|
2016-03-24 18:02:45 +01:00
|
|
|
|
|
|
|
foreach ($documents as $document) {
|
|
|
|
$document->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Done');
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-03-24 18:02:45 +01:00
|
|
|
protected function getArguments()
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
return [];
|
2016-03-24 18:02:45 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-03-24 18:02:45 +01:00
|
|
|
protected function getOptions()
|
|
|
|
{
|
2017-05-01 14:17:52 +02:00
|
|
|
return [
|
|
|
|
['database', null, InputOption::VALUE_OPTIONAL, 'Database', null],
|
|
|
|
];
|
2016-03-24 18:02:45 +01:00
|
|
|
}
|
|
|
|
}
|