-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathProductAvailabilityPurchasableWithoutStockStrategy.php
More file actions
70 lines (57 loc) · 2.01 KB
/
ProductAvailabilityPurchasableWithoutStockStrategy.php
File metadata and controls
70 lines (57 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php declare(strict_types=1);
namespace App\ProductCatalog\Availability;
use Ibexa\Contracts\ProductCatalog\ProductAvailabilityStrategyInterface;
use Ibexa\Contracts\ProductCatalog\Values\Availability\AvailabilityContextInterface;
use Ibexa\Contracts\ProductCatalog\Values\Availability\AvailabilityInterface;
use Ibexa\Contracts\ProductCatalog\Values\ProductInterface;
use Ibexa\ProductCatalog\Local\Persistence\Legacy\ProductAvailability\HandlerInterface;
use Ibexa\ProductCatalog\Local\Repository\Values\Availability;
final class ProductAvailabilityPurchasableWithoutStockStrategy implements ProductAvailabilityStrategyInterface
{
private HandlerInterface $handler;
public function __construct(HandlerInterface $handler)
{
$this->handler = $handler;
}
public function accept(AvailabilityContextInterface $context): bool
{
return $context instanceof PurchasableWithoutStockAvailabilityContext;
}
public function getProductAvailability(
ProductInterface $product,
AvailabilityContextInterface $context
): AvailabilityInterface {
$productAvailability = $this->handler->find($product->getCode());
$rawAvailableFlag = $productAvailability->isAvailable();
$stock = $productAvailability->getStock();
$isInfinite = $productAvailability->isInfinite();
$computedAvailable = $this->calculateAvailability(
$rawAvailableFlag,
$stock,
$isInfinite,
);
return new Availability(
$product,
$rawAvailableFlag,
$computedAvailable,
$isInfinite,
$stock,
);
}
private function calculateAvailability(
bool $rawAvailable,
?int $stock,
bool $isInfinite
): bool {
if ($rawAvailable === false) {
return false;
}
if ($isInfinite) {
return true;
}
if ($stock === null) {
return true;
}
return $stock >= 0;
}
}