Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

spatie/spatie.be

493 stars
1 code files
View spatie/spatie.be on GitHub

app/Console/Commands/GiftRayToSponsorsCommand.php

Open in GitHub
use App\Actions\CreateLicenseAction;
use App\Models\License;
use App\Models\Purchasable;
use App\Models\Purchase;
use App\Models\User;
use Illuminate\Console\Command;
 
class GiftRayToSponsorsCommand extends Command
{
protected $signature = 'gift-ray-to-sponsors';
 
public function handle()
{
$this->info('Start handing out Ray to sponsors...');
 
$rayPurchasable = Purchasable::find(15);
 
User::query()
->cursor()
->filter(fn (User $user) => $user->isSponsoring())
->filter(fn (User $user) => is_null($user->sponsor_gift_given_at))
->each(function (User $user) use ($rayPurchasable) {
$this->comment("Handing Ray to user {$user->id} ($user->email)");
 
$existingLicense = $user->licenses()->where('purchasable_id', $rayPurchasable->id)->first();
 
$existingLicense
? $this->renewExistingLicense($existingLicense)
: $this->createNewLicense($user, $rayPurchasable);
 
$user->update(['sponsor_gift_given_at' => now()]);
});
 
$this->info('All done!');
}
 
protected function renewExistingLicense(License $license): void
{
$license->renew();
 
$this->comment('Renewed existing Ray license.');
}
 
protected function createNewLicense(User $user, Purchasable $purchasable): void
{
$purchase = Purchase::create([
'user_id' => $user->id,
'purchasable_id' => $purchasable->id,
'quantity' => 1,
'receipt_id' => null,
'paddle_webhook_payload' => null,
'paddle_fee' => 0,
'earnings' => 0,
'passthrough' => null,
]);
 
app(CreateLicenseAction::class)->execute($user, $purchase, $purchasable);
 
$this->comment('New license created.');
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.