Skip to content

Commit ee1a8b6

Browse files
authored
Merge pull request #375 from bavix/basket
[7.x. Stage 2] add BasketInterface
2 parents 89db1b6 + 43ac5fc commit ee1a8b6

File tree

10 files changed

+242
-64
lines changed

10 files changed

+242
-64
lines changed

src/Internal/BasketInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal;
6+
7+
use Bavix\Wallet\Internal\Dto\AvailabilityDto;
8+
9+
interface BasketInterface
10+
{
11+
public function availability(AvailabilityDto $availabilityDto): bool;
12+
}

src/Internal/Dto/AvailabilityDto.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Dto;
6+
7+
use Bavix\Wallet\Interfaces\Customer;
8+
9+
class AvailabilityDto
10+
{
11+
private BasketDto $basketDto;
12+
13+
private Customer $customer;
14+
15+
private bool $force;
16+
17+
public function __construct(
18+
Customer $customer,
19+
BasketDto $basketDto,
20+
bool $force
21+
) {
22+
$this->customer = $customer;
23+
$this->basketDto = $basketDto;
24+
$this->force = $force;
25+
}
26+
27+
public function getBasketDto(): BasketDto
28+
{
29+
return $this->basketDto;
30+
}
31+
32+
public function getCustomer(): Customer
33+
{
34+
return $this->customer;
35+
}
36+
37+
public function isForce(): bool
38+
{
39+
return $this->force;
40+
}
41+
}

src/Internal/Dto/BasketDto.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Dto;
6+
7+
use Countable;
8+
9+
class BasketDto implements Countable
10+
{
11+
/** @var ProductDto[] */
12+
private array $items;
13+
14+
private array $meta;
15+
16+
/** @param ProductDto[] $items */
17+
public function __construct(array $items, array $meta)
18+
{
19+
$this->items = $items;
20+
$this->meta = $meta;
21+
}
22+
23+
public function meta(): array
24+
{
25+
return $this->meta;
26+
}
27+
28+
public function count(): int
29+
{
30+
return count($this->items);
31+
}
32+
33+
/** @return ProductDto[] */
34+
public function items(): array
35+
{
36+
return $this->items;
37+
}
38+
}

src/Internal/Dto/ProductDto.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Internal\Dto;
6+
7+
use Bavix\Wallet\Interfaces\Product;
8+
use Countable;
9+
10+
class ProductDto implements Countable
11+
{
12+
private Product $product;
13+
14+
private int $quantity;
15+
16+
public function __construct(Product $product, int $quantity)
17+
{
18+
$this->product = $product;
19+
$this->quantity = $quantity;
20+
}
21+
22+
public function item(): Product
23+
{
24+
return $this->product;
25+
}
26+
27+
public function count(): int
28+
{
29+
return $this->quantity;
30+
}
31+
}

src/Objects/Cart.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Bavix\Wallet\Objects;
46

57
use function array_unique;
68
use Bavix\Wallet\Interfaces\Customer;
79
use Bavix\Wallet\Interfaces\Product;
10+
use Bavix\Wallet\Internal\BasketInterface;
11+
use Bavix\Wallet\Internal\Dto\AvailabilityDto;
12+
use Bavix\Wallet\Internal\Dto\BasketDto;
13+
use Bavix\Wallet\Internal\Dto\ProductDto;
814
use Bavix\Wallet\Internal\MathInterface;
915
use Bavix\Wallet\Models\Transfer;
1016
use function count;
@@ -25,6 +31,13 @@ class Cart implements Countable
2531

2632
protected $meta = [];
2733

34+
private BasketInterface $basket;
35+
36+
public function __construct(BasketInterface $basket)
37+
{
38+
$this->basket = $basket;
39+
}
40+
2841
public function getMeta(): array
2942
{
3043
return $this->meta;
@@ -83,7 +96,7 @@ public function getUniqueItems(): array
8396
*
8497
* @return Transfer[]
8598
*/
86-
public function alreadyBuy(Customer $customer, bool $gifts = null): array
99+
public function alreadyBuy(Customer $customer, bool $gifts = false): array
87100
{
88101
$status = [Transfer::STATUS_PAID];
89102
if ($gifts) {
@@ -111,15 +124,13 @@ public function alreadyBuy(Customer $customer, bool $gifts = null): array
111124
return $result;
112125
}
113126

114-
public function canBuy(Customer $customer, bool $force = null): bool
127+
/**
128+
* @deprecated
129+
* @see BasketInterface::availability()
130+
*/
131+
public function canBuy(Customer $customer, bool $force = false): bool
115132
{
116-
foreach ($this->items as $item) {
117-
if (!$item->canBuy($customer, $this->getQuantity($item), $force)) {
118-
return false;
119-
}
120-
}
121-
122-
return true;
133+
return $this->basket->availability(new AvailabilityDto($customer, $this->getBasketDto(), $force));
123134
}
124135

125136
public function getTotal(Customer $customer): string
@@ -143,7 +154,7 @@ public function getQuantity(Product $product): int
143154
$class = get_class($product);
144155
$uniq = $product->getUniqueId();
145156

146-
return $this->quantity[$class][$uniq] ?? 0;
157+
return (int) ($this->quantity[$class][$uniq] ?? 0);
147158
}
148159

149160
protected function addQuantity(Product $product, int $quantity): void
@@ -153,4 +164,14 @@ protected function addQuantity(Product $product, int $quantity): void
153164
$math = app(MathInterface::class);
154165
$this->quantity[$class][$uniq] = $math->add($this->getQuantity($product), $quantity);
155166
}
167+
168+
private function getBasketDto(): BasketDto
169+
{
170+
$productDto = [];
171+
foreach ($this->getUniqueItems() as $product) {
172+
$productDto[] = new ProductDto($product, $this->getQuantity($product));
173+
}
174+
175+
return new BasketDto($productDto, $this->getMeta());
176+
}
156177
}

src/Services/BasketService.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bavix\Wallet\Services;
6+
7+
use Bavix\Wallet\Internal\BasketInterface;
8+
use Bavix\Wallet\Internal\Dto\AvailabilityDto;
9+
10+
class BasketService implements BasketInterface
11+
{
12+
public function availability(AvailabilityDto $availabilityDto): bool
13+
{
14+
$basketDto = $availabilityDto->getBasketDto();
15+
$customer = $availabilityDto->getCustomer();
16+
foreach ($basketDto->items() as $productDto) {
17+
if (!$productDto->item()->canBuy($customer, $productDto->count(), $availabilityDto->isForce())) {
18+
return false;
19+
}
20+
}
21+
22+
return true;
23+
}
24+
}

src/Traits/CanPay.php

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,15 @@ public function payFree(Product $product): Transfer
1919
return current($this->payFreeCart(app(Cart::class)->addItem($product)));
2020
}
2121

22-
/**
23-
* @param bool $force
24-
*/
25-
public function safePay(Product $product, bool $force = null): ?Transfer
22+
public function safePay(Product $product, bool $force = false): ?Transfer
2623
{
2724
return current($this->safePayCart(app(Cart::class)->addItem($product), $force)) ?: null;
2825
}
2926

3027
/**
31-
* @param bool $force
32-
*
3328
* @throws
3429
*/
35-
public function pay(Product $product, bool $force = null): Transfer
30+
public function pay(Product $product, bool $force = false): Transfer
3631
{
3732
return current($this->payCart(app(Cart::class)->addItem($product), $force));
3833
}
@@ -45,50 +40,36 @@ public function forcePay(Product $product): Transfer
4540
return current($this->forcePayCart(app(Cart::class)->addItem($product)));
4641
}
4742

48-
/**
49-
* @param bool $force
50-
* @param bool $gifts
51-
*/
52-
public function safeRefund(Product $product, bool $force = null, bool $gifts = null): bool
43+
public function safeRefund(Product $product, bool $force = false, bool $gifts = false): bool
5344
{
5445
return $this->safeRefundCart(app(Cart::class)->addItem($product), $force, $gifts);
5546
}
5647

5748
/**
58-
* @param bool $force
59-
* @param bool $gifts
60-
*
6149
* @throws
6250
*/
63-
public function refund(Product $product, bool $force = null, bool $gifts = null): bool
51+
public function refund(Product $product, bool $force = false, bool $gifts = false): bool
6452
{
6553
return $this->refundCart(app(Cart::class)->addItem($product), $force, $gifts);
6654
}
6755

6856
/**
69-
* @param bool $gifts
70-
*
7157
* @throws
7258
*/
73-
public function forceRefund(Product $product, bool $gifts = null): bool
59+
public function forceRefund(Product $product, bool $gifts = false): bool
7460
{
7561
return $this->forceRefundCart(app(Cart::class)->addItem($product), $gifts);
7662
}
7763

78-
/**
79-
* @param bool $force
80-
*/
81-
public function safeRefundGift(Product $product, bool $force = null): bool
64+
public function safeRefundGift(Product $product, bool $force = false): bool
8265
{
8366
return $this->safeRefundGiftCart(app(Cart::class)->addItem($product), $force);
8467
}
8568

8669
/**
87-
* @param bool $force
88-
*
8970
* @throws
9071
*/
91-
public function refundGift(Product $product, bool $force = null): bool
72+
public function refundGift(Product $product, bool $force = false): bool
9273
{
9374
return $this->refundGiftCart(app(Cart::class)->addItem($product), $force);
9475
}

0 commit comments

Comments
 (0)