1
0

ImportRemoveDeletedAccounts.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Storage;
  6. use App\User;
  7. use App\Models\ImportPost;
  8. use App\Services\ImportService;
  9. class ImportRemoveDeletedAccounts extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'app:import-remove-deleted-accounts';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. const CACHE_KEY = 'pf:services:import:gc-accounts:skip_min_id';
  24. /**
  25. * Execute the console command.
  26. */
  27. public function handle()
  28. {
  29. $skipMinId = Cache::remember(self::CACHE_KEY, 864000, function() {
  30. return 1;
  31. });
  32. $deletedIds = User::withTrashed()
  33. ->whereNotNull('status')
  34. ->whereIn('status', ['deleted', 'delete'])
  35. ->where('id', '>', $skipMinId)
  36. ->limit(500)
  37. ->pluck('id');
  38. if(!$deletedIds || !$deletedIds->count()) {
  39. return;
  40. }
  41. foreach($deletedIds as $did) {
  42. if(Storage::exists('imports/' . $did)) {
  43. Storage::deleteDirectory('imports/' . $did);
  44. }
  45. ImportPost::where('user_id', $did)->delete();
  46. $skipMinId = $did;
  47. }
  48. Cache::put(self::CACHE_KEY, $skipMinId, 864000);
  49. }
  50. }