Skip to content

Commit

Permalink
Merge pull request #144 from dystcz/feature/update-is-purchasable-action
Browse files Browse the repository at this point in the history
Update IsPurchasable action to make use of availability enum
  • Loading branch information
repl6669 authored May 11, 2024
2 parents 79d2733 + 56797d9 commit a45bb68
Showing 1 changed file with 2 additions and 13 deletions.
15 changes: 2 additions & 13 deletions src/Domain/Products/Actions/IsPurchasable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Dystcz\LunarApi\Domain\Products\Enums\Availability;
use Dystcz\LunarApi\Domain\Products\Models\Product;
use Dystcz\LunarApi\Domain\ProductVariants\Models\ProductVariant;
use Illuminate\Support\Facades\Cache;

class IsPurchasable
Expand All @@ -15,23 +14,13 @@ class IsPurchasable
public function __invoke(Product $product, bool $skipCache = false): bool
{
if ($skipCache) {
return $this->atLeastOneVariantPurchasable($product);
return Availability::of($product)->purchasable();
}

return Cache::remember(
"product-{$product->id}-purchasable",
3600,
fn () => $this->atLeastOneVariantPurchasable($product),
fn () => Availability::of($product)->purchasable(),
);
}

/**
* Determine if at least one variant of the product is available.
*/
protected function atLeastOneVariantPurchasable(Product $product): bool
{
return $product->variants->reduce(function (bool $carry, ProductVariant $variant) {
return $carry || Availability::of($variant)->purchasable();
}, false);
}
}

0 comments on commit a45bb68

Please sign in to comment.