I creating a middleware class to check user subscriptions written in Laravel framework and would like some feedback on a good design pattern for handling the multiple conditions.
class CheckSubscription {
use HttpResponse;
/**
* Handle an incoming request.
*
* @param Request $request
* @param \Closure $next
* @return JsonResponse|mixed
*/
public function handle(Request $request, Closure $next)
{
if( Auth::check() ) {
$user = Auth::user();
$userId = empty($user->parent_id) ? $user->id : $user->parent_id;
$subscription = Subscription::with('plan')->whereUserId($userId)->first();
if( empty($subscription) ) {
return $this->sendError(__("You don't have subscription available on your account"));
}
if( Carbon::today() > Carbon::parse($subscription->end_date) ) {
return $this->sendError(__('Sorry! Your subscription has expired'));
}
if($request->method() == 'POST') {
$spacesLimit = $user->spaces->count();
if( $spacesLimit >= $subscription->plan->number_of_workspaces) {
return $this->sendError(__('Sorry, You’ve reached your workspace limit. To extend the workspace limit, please UPGRADE your subscription.'));
}
}
return $next($request);
}
return $this->sendError(__('Unauthenticated'), 401);
}
}
Aucun commentaire:
Enregistrer un commentaire