Skip to content

Commit

Permalink
Refactor cart services to improve session handling
Browse files Browse the repository at this point in the history
The refactor includes renaming methods for better clarity such as getCartSessionId to getCartSessionKey and getSessionId to getAuthUserId. We have also improved the instantiation of CartService, SessionResolver, and capturing of session keys and user identity within the constructor. Adjustments are also made within the Cart model to better capture and handle the user and session scopes.
  • Loading branch information
CrazyBoy49z committed Jul 7, 2024
1 parent 08f12c9 commit 10ec9ed
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
25 changes: 14 additions & 11 deletions src/Models/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,33 @@ public function items(): HasMany
return $this->hasMany(CartItem::class);
}

public function scopeForUser(Builder $query, int $userId): Builder
public function scopeByUser(Builder $query, int $userId): Builder
{
return $query->where('user_id', $userId);
}

/**
* @noinspection SensitiveParameterInspection
*/
public function scopeForSession(Builder $query, ?string $sessionId = null): Builder
public function scopeBySession(Builder $query, ?string $sessionId = null): Builder
{
return $query->where('session_id', $sessionId);
}

public function scopeForCurrentUser(Builder $query): Builder
{
$currentSessionId = (new CartService)->getSessionId();

return $query->when(auth()->check(),
fn (Builder $query) => $query
->forUser(auth()->id())
->orWhere
->forSession($currentSessionId),
fn (Builder $query) => $query
->forSession($currentSessionId));
$cartService = new CartService();
$currentSessionId = $cartService->getSessionId();

Check failure on line 41 in src/Models/Cart.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Step2Dev\LazyCart\Services\CartService::getSessionId().
$currentUserId = $cartService->getUserId();

Check failure on line 42 in src/Models/Cart.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Step2Dev\LazyCart\Services\CartService::getUserId().

return $query
->when($currentUserId,
fn (Builder $query) => $query

Check failure on line 46 in src/Models/Cart.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::byUser().
->byUser($currentUserId)
->orWhere
->forSession($currentSessionId),
fn (Builder $query) => $query

Check failure on line 50 in src/Models/Cart.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Database\Eloquent\Builder::bySession().
->bySession($currentSessionId));
}

public function prunable(): self
Expand Down
33 changes: 18 additions & 15 deletions src/Services/CartService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cookie;
use Step2Dev\LazyCart\Models\Cart;
use Step2Dev\LazyCart\Support\SessionResolver;

class CartService
{
private ?string $sessionKey;
private SessionResolver $sessionResolver;
private ?int $authUserId;

public function __construct()
{
$this->sessionResolver = new SessionResolver();
$this->sessionKey = $this->sessionResolver->getCartSessionKey();
$this->authUserId = auth('sanctum')->id() ?? auth()->id();
}

public function getCartId(): int
{
return Cart::forCurrentUser()->value('id');
Expand All @@ -21,23 +33,14 @@ public function getCart()
->first();
}

public function getSessionId(): string
public function getAuthUserId(): ?int
{
$name = config('lazy.cart.cookie.name', 'cart_session_id');
$sessionCartId = Cookie::get($name);

if ($sessionCartId) {
return $sessionCartId;
}

if (auth()->guest()) {
$sessionCartId = session()->getId();
$sessionCartDays = config('lazy.cart.cart.days', 30);

cookie()->queue($name, $sessionCartId, 60 * 24 * $sessionCartDays);
}
return $this->authUserId;
}

return $sessionCartId;
public function getSessionKey(): string
{
return $this->sessionKey;
}

public function add(Model $item, int $quantity, array $options = []): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Support/SessionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function saveCartSession(#[SensitiveParameter] string $sessionId): void
cookie()->queue($this->sessionName, $sessionId, 60 * 24 * $this->cartSaveDays);
}

public function getCartSessionId(): ?string
public function getCartSessionKey(): ?string
{
$sessionCart = $this->getCartSession();

Expand Down

0 comments on commit 10ec9ed

Please sign in to comment.