Am i following good practices?

Hey, i was wondering if anyone with experience could tell me if i am following good practices when doing custom actions, thx
image.png
image.png
Solution
private function findLicense(string $key): ?License
{
    return License::with('product')->where('key', $key)->first();
}

private function sendNotification(string $message, string $type = 'success'): void
{
    Notification::make()
        ->title($message)
        ->status($type)
        ->send();
}

private function findOldSubscription(int $productId, int $userId): ?Subscription
{
    return Subscription::where('product_id', $productId)
        ->where('user_id', $userId)
        ->first();
}

private function updateOldSubscription(Subscription $oldSubscription, License $license): void
{
    $oldSubscription->end_date = Carbon::parse($oldSubscription->end_date)
        ->addDays($license->expiration_time);
    $oldSubscription->update();
}

private function createNewSubscription(License $license, int $userId): void
{
    $subscription = new Subscription();
    $subscription->start_date = now();
    $subscription->user_id = $userId;
    $subscription->product_id = $license->product_id;
    $subscription->end_date = now()->addDays($license->expiration_time);

    if ($license->product->status === ProductStatus::Offline->value) { // could use just ProductStatus::Offline here instead of chaining ->value
        $subscription->paused_at = now();
    }

    if ($subscription->save()) {
        $this->deleteLicenseAndNotify($license, 'Subscription redeemed.');
        $this->redirect($this->getResource()::getUrl('index'));
    } else {
        $this->sendNotification('Error happened at redeeming.', 'danger');
    }
}

private function deleteLicenseAndNotify(License $license, string $message): void
{
    $license->delete();
    $this->sendNotification($message);
}
Was this page helpful?