Skip to content

Commit a7d8603

Browse files
committed
Split product interface
1 parent e4ccf1d commit a7d8603

File tree

14 files changed

+99
-71
lines changed

14 files changed

+99
-71
lines changed

src/Interfaces/Customer.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ interface Customer extends Wallet
2727
* @throws TransactionFailedException
2828
* @throws ExceptionInterface
2929
*/
30-
public function payFree(Product $product): Transfer;
30+
public function payFree(ProductInterface $product): Transfer;
3131

32-
public function safePay(Product $product, bool $force = false): ?Transfer;
32+
public function safePay(ProductInterface $product, bool $force = false): ?Transfer;
3333

3434
/**
3535
* @throws ProductEnded
@@ -41,7 +41,7 @@ public function safePay(Product $product, bool $force = false): ?Transfer;
4141
* @throws TransactionFailedException
4242
* @throws ExceptionInterface
4343
*/
44-
public function pay(Product $product, bool $force = false): Transfer;
44+
public function pay(ProductInterface $product, bool $force = false): Transfer;
4545

4646
/**
4747
* @throws ProductEnded
@@ -51,9 +51,9 @@ public function pay(Product $product, bool $force = false): Transfer;
5151
* @throws TransactionFailedException
5252
* @throws ExceptionInterface
5353
*/
54-
public function forcePay(Product $product): Transfer;
54+
public function forcePay(ProductInterface $product): Transfer;
5555

56-
public function safeRefund(Product $product, bool $force = false, bool $gifts = false): bool;
56+
public function safeRefund(ProductInterface $product, bool $force = false, bool $gifts = false): bool;
5757

5858
/**
5959
* @throws BalanceIsEmpty
@@ -65,7 +65,7 @@ public function safeRefund(Product $product, bool $force = false, bool $gifts =
6565
* @throws ModelNotFoundException
6666
* @throws ExceptionInterface
6767
*/
68-
public function refund(Product $product, bool $force = false, bool $gifts = false): bool;
68+
public function refund(ProductInterface $product, bool $force = false, bool $gifts = false): bool;
6969

7070
/**
7171
* @throws LockProviderNotFoundException
@@ -75,9 +75,9 @@ public function refund(Product $product, bool $force = false, bool $gifts = fals
7575
* @throws ModelNotFoundException
7676
* @throws ExceptionInterface
7777
*/
78-
public function forceRefund(Product $product, bool $gifts = false): bool;
78+
public function forceRefund(ProductInterface $product, bool $gifts = false): bool;
7979

80-
public function safeRefundGift(Product $product, bool $force = false): bool;
80+
public function safeRefundGift(ProductInterface $product, bool $force = false): bool;
8181

8282
/**
8383
* @throws BalanceIsEmpty
@@ -89,7 +89,7 @@ public function safeRefundGift(Product $product, bool $force = false): bool;
8989
* @throws ModelNotFoundException
9090
* @throws ExceptionInterface
9191
*/
92-
public function refundGift(Product $product, bool $force = false): bool;
92+
public function refundGift(ProductInterface $product, bool $force = false): bool;
9393

9494
/**
9595
* @throws LockProviderNotFoundException
@@ -99,7 +99,7 @@ public function refundGift(Product $product, bool $force = false): bool;
9999
* @throws ModelNotFoundException
100100
* @throws ExceptionInterface
101101
*/
102-
public function forceRefundGift(Product $product): bool;
102+
public function forceRefundGift(ProductInterface $product): bool;
103103

104104
/**
105105
* @throws ProductEnded
@@ -199,5 +199,5 @@ public function forceRefundGiftCart(CartInterface $cart): bool;
199199
*
200200
* @deprecated The method is slow and will be removed in the future
201201
*/
202-
public function paid(Product $product, bool $gifts = false): ?Transfer;
202+
public function paid(ProductInterface $product, bool $gifts = false): ?Transfer;
203203
}

src/Interfaces/Product.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,14 @@
44

55
namespace Bavix\Wallet\Interfaces;
66

7-
interface Product extends Wallet
7+
/**
8+
* If the product is always in stock, then the ProductInterface must be used. If the product may not be available, then
9+
* there is a need to use the ProductLimitedInterface.
10+
*
11+
* @deprecated The class is deprecated. Will be removed in the future.
12+
* @see ProductInterface
13+
* @see ProductLimitedInterface
14+
*/
15+
interface Product extends ProductLimitedInterface
816
{
9-
/**
10-
* The method is only needed for simple projects. For more complex projects, deprecate this method and redefine the
11-
* "BasketServiceInterface" interface. Typically, in projects, this method always returns false, and the presence
12-
* interface goes to the microservice and receives data on products.
13-
*/
14-
public function canBuy(Customer $customer, int $quantity = 1, bool $force = false): bool;
15-
16-
/**
17-
* @return float|int|string
18-
*/
19-
public function getAmountProduct(Customer $customer);
20-
21-
/**
22-
* @return array
23-
*/
24-
public function getMetaProduct(): ?array;
2517
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Interfaces;
6+
7+
/**
8+
* @internal
9+
*/
10+
interface ProductInterface extends Wallet
11+
{
12+
/**
13+
* @return float|int|string
14+
*/
15+
public function getAmountProduct(Customer $customer);
16+
17+
public function getMetaProduct(): ?array;
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Interfaces;
6+
7+
interface ProductLimitedInterface extends ProductInterface
8+
{
9+
/**
10+
* The method is only needed for simple projects. For more complex projects, deprecate this method and redefine the
11+
* "BasketServiceInterface" interface. Typically, in projects, this method always returns false, and the presence
12+
* interface goes to the microservice and receives data on products.
13+
*/
14+
public function canBuy(Customer $customer, int $quantity = 1, bool $force = false): bool;
15+
}

src/Internal/Dto/BasketDto.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Bavix\Wallet\Internal\Dto;
66

7-
use Bavix\Wallet\Interfaces\Product;
7+
use Bavix\Wallet\Interfaces\ProductInterface;
88
use Generator;
99

1010
final class BasketDto implements BasketDtoInterface
@@ -34,7 +34,7 @@ public function total(): int
3434
}
3535

3636
/**
37-
* @return Generator<array-key, Product, mixed, void>
37+
* @return Generator<array-key, ProductInterface, mixed, void>
3838
*/
3939
public function cursor(): Generator
4040
{

src/Internal/Dto/BasketDtoInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Bavix\Wallet\Internal\Dto;
66

7-
use Bavix\Wallet\Interfaces\Product;
7+
use Bavix\Wallet\Interfaces\ProductInterface;
88
use Countable;
99
use Generator;
1010

@@ -20,7 +20,7 @@ public function meta(): array;
2020
public function items(): array;
2121

2222
/**
23-
* @return Generator<array-key, Product, mixed, void>
23+
* @return Generator<array-key, ProductInterface, mixed, void>
2424
*/
2525
public function cursor(): Generator;
2626
}

src/Internal/Dto/ItemDto.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@
44

55
namespace Bavix\Wallet\Internal\Dto;
66

7-
use Bavix\Wallet\Interfaces\Product;
7+
use Bavix\Wallet\Interfaces\ProductInterface;
88

99
/** @psalm-immutable */
1010
final class ItemDto implements ItemDtoInterface
1111
{
1212
public function __construct(
13-
private Product $product,
13+
private ProductInterface $product,
1414
private int $quantity
1515
) {
1616
}
1717

1818
/**
19-
* @return Product[]
19+
* @return ProductInterface[]
2020
*/
2121
public function items(): array
2222
{
2323
return array_fill(0, $this->quantity, $this->product);
2424
}
2525

26-
public function product(): Product
26+
public function product(): ProductInterface
2727
{
2828
return $this->product;
2929
}

src/Internal/Dto/ItemDtoInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
namespace Bavix\Wallet\Internal\Dto;
66

7-
use Bavix\Wallet\Interfaces\Product;
7+
use Bavix\Wallet\Interfaces\ProductInterface;
88
use Countable;
99

1010
interface ItemDtoInterface extends Countable
1111
{
1212
/**
13-
* @return Product[]
13+
* @return ProductInterface[]
1414
*/
1515
public function items(): array;
1616

1717
public function count(): int;
1818

19-
public function product(): Product;
19+
public function product(): ProductInterface;
2020
}

src/Objects/Cart.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Bavix\Wallet\Interfaces\CartInterface;
88
use Bavix\Wallet\Interfaces\Customer;
9-
use Bavix\Wallet\Interfaces\Product;
9+
use Bavix\Wallet\Interfaces\ProductInterface;
1010
use Bavix\Wallet\Internal\Dto\BasketDto;
1111
use Bavix\Wallet\Internal\Dto\BasketDtoInterface;
1212
use Bavix\Wallet\Internal\Dto\ItemDto;
@@ -21,7 +21,7 @@
2121
final class Cart implements Countable, CartInterface
2222
{
2323
/**
24-
* @var Product[]
24+
* @var ProductInterface[]
2525
*/
2626
private array $items = [];
2727

@@ -64,7 +64,7 @@ public function setMeta(array $meta): self
6464
return $this;
6565
}
6666

67-
public function withItem(Product $product, int $quantity = 1): self
67+
public function withItem(ProductInterface $product, int $quantity = 1): self
6868
{
6969
$self = clone $this;
7070

@@ -82,7 +82,7 @@ public function withItem(Product $product, int $quantity = 1): self
8282
* @deprecated
8383
* @see withItem
8484
*/
85-
public function addItem(Product $product, int $quantity = 1): self
85+
public function addItem(ProductInterface $product, int $quantity = 1): self
8686
{
8787
$productId = $this->productId($product);
8888

@@ -118,7 +118,7 @@ public function addItems(iterable $products): self
118118
}
119119

120120
/**
121-
* @return Product[]
121+
* @return ProductInterface[]
122122
*/
123123
public function getItems(): array
124124
{
@@ -134,10 +134,7 @@ public function getItems(): array
134134
}
135135

136136
/**
137-
* @return Product[]
138-
*
139-
* @deprecated Will be removed in 9.x
140-
* @see getItems
137+
* @return ProductInterface[]
141138
*/
142139
public function getUniqueItems(): array
143140
{
@@ -160,7 +157,7 @@ public function count(): int
160157
return count($this->items);
161158
}
162159

163-
public function getQuantity(Product $product): int
160+
public function getQuantity(ProductInterface $product): int
164161
{
165162
return $this->quantity[$this->productId($product)] ?? 0;
166163
}
@@ -171,7 +168,7 @@ public function getQuantity(Product $product): int
171168
public function getBasketDto(): BasketDtoInterface
172169
{
173170
$items = array_map(
174-
fn (Product $product): ItemDtoInterface => new ItemDto($product, $this->getQuantity($product)),
171+
fn (ProductInterface $product): ItemDtoInterface => new ItemDto($product, $this->getQuantity($product)),
175172
$this->getUniqueItems()
176173
);
177174

@@ -182,7 +179,7 @@ public function getBasketDto(): BasketDtoInterface
182179
return new BasketDto($items, $this->getMeta());
183180
}
184181

185-
private function productId(Product $product): string
182+
private function productId(ProductInterface $product): string
186183
{
187184
return $product::class.':'.$this->castService->getModel($product)->getKey();
188185
}

src/Services/BasketService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Bavix\Wallet\Services;
66

7+
use Bavix\Wallet\Interfaces\ProductLimitedInterface;
78
use Bavix\Wallet\Internal\Dto\AvailabilityDtoInterface;
89

910
final class BasketService implements BasketServiceInterface
@@ -13,7 +14,12 @@ public function availability(AvailabilityDtoInterface $availabilityDto): bool
1314
$basketDto = $availabilityDto->getBasketDto();
1415
$customer = $availabilityDto->getCustomer();
1516
foreach ($basketDto->items() as $itemDto) {
16-
if (!$itemDto->product()->canBuy($customer, $itemDto->count(), $availabilityDto->isForce())) {
17+
$product = $itemDto->product();
18+
if ($product instanceof ProductLimitedInterface && !$product->canBuy(
19+
$customer,
20+
$itemDto->count(),
21+
$availabilityDto->isForce()
22+
)) {
1723
return false;
1824
}
1925
}

0 commit comments

Comments
 (0)