-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Purge empty campaigns + fix magical campaign_id
- Loading branch information
Showing
8 changed files
with
108 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Services\Campaign\PurgeService; | ||
use Carbon\Carbon; | ||
use Illuminate\Console\Command; | ||
|
||
class CleanupCampaigns extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'campaigns:cleanup {dry=1}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Delete empty campaigns'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
$this->info(Carbon::now()); | ||
|
||
/** @var PurgeService $service */ | ||
$service = app()->make(PurgeService::class); | ||
|
||
$dry = $this->argument('dry'); | ||
if ($dry === '0') { | ||
$service->real(); | ||
} | ||
|
||
$count = $service->purgeEmpty(); | ||
|
||
if ($dry === '0') { | ||
$this->info(Carbon::now() . ': Deleted ' . $count . ' empty campaigns.'); | ||
} else { | ||
$this->info(Carbon::now() . ': There are ' . $count . ' empty campaigns that can be deleted.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace App\Services\Campaign; | ||
|
||
use App\Models\Campaign; | ||
use App\Services\ImageService; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class PurgeService | ||
{ | ||
protected int $count = 0; | ||
protected bool $dry = true; | ||
|
||
public function real(): self | ||
{ | ||
$this->dry = false; | ||
return $this; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return $this->baseAll() | ||
->count(); | ||
} | ||
|
||
public function purgeEmpty(): int | ||
{ | ||
$this->baseAll() | ||
->chunk(500, function ($campaigns) { | ||
/** @var Campaign $campaign */ | ||
foreach ($campaigns as $campaign) { | ||
if (!$this->dry) { | ||
ImageService::cleanup($campaign); | ||
$campaign->delete(); | ||
Log::info('Services\Campaigns\PurgeService', ['campaign' => $campaign->id]); | ||
} | ||
$this->count++; | ||
} | ||
}); | ||
return $this->count; | ||
} | ||
|
||
protected function baseAll(): Builder | ||
{ | ||
return Campaign::select('campaigns.id') | ||
->leftJoin('campaign_user as cu', 'cu.campaign_id', 'campaigns.id') | ||
->whereNull('cu.id'); | ||
} | ||
} |