1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
namespace Kepawni\Twilted\Basic\TestSample; |
3
|
|
|
|
4
|
|
|
use Assert\Assert; |
5
|
|
|
use Kepawni\Twilted\Basic\SimpleAggregateRoot; |
6
|
|
|
use Kepawni\Twilted\Basic\TestSample\Event\ItemWasAddedToShoppingBasket; |
7
|
|
|
use Kepawni\Twilted\Basic\TestSample\Event\ShoppingBasketWasCheckedOut; |
8
|
|
|
use Kepawni\Twilted\Basic\TestSample\Event\ShoppingBasketWasPickedUpByNewCustomer; |
9
|
|
|
use Kepawni\Twilted\Basic\TestSample\Event\ShoppingBasketWasPickedUpByReturningCustomer; |
10
|
|
|
use Kepawni\Twilted\EntityIdentifier; |
11
|
|
|
|
12
|
|
|
class ShoppingBasket extends SimpleAggregateRoot |
13
|
|
|
{ |
14
|
|
|
const FAKE_CUSTOMER_ID = '8ccbb53b-6f23-43b8-be73-00efdfb90996'; |
15
|
|
|
private $isClosed = false; |
16
|
|
|
private $items = []; |
17
|
|
|
/** @var string|null */ |
18
|
|
|
private $returningCustomerId = null; |
19
|
|
|
|
20
|
|
|
public static function pickUp(EntityIdentifier $uuid, string $returningCustomerId = null) |
21
|
|
|
{ |
22
|
|
|
$basket = new static($uuid); |
23
|
|
|
if ($returningCustomerId) { |
24
|
|
|
Assert::that($returningCustomerId)->uuid(); |
25
|
|
|
$basket->recordThat(new ShoppingBasketWasPickedUpByReturningCustomer($returningCustomerId)); |
26
|
|
|
} else { |
27
|
|
|
$basket->recordThat(new ShoppingBasketWasPickedUpByNewCustomer()); |
28
|
|
|
} |
29
|
|
|
return $basket; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function addItem(string $itemCode, int $quantity = 1) |
33
|
|
|
{ |
34
|
|
|
Assert::that($this->isClosed)->false('Basket has been closed during checkout'); |
35
|
|
|
$this->recordThat(new ItemWasAddedToShoppingBasket($itemCode, $quantity)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function checkout($billingData, $shippingData, $paymentInformation) |
39
|
|
|
{ |
40
|
|
|
Assert::that(array_sum($this->items))->greaterThan(0, 'Empty baskets cannot be processed'); |
41
|
|
|
$newCustomerId = $this->magicallyCreateCustomerId($billingData, $shippingData, $paymentInformation); |
42
|
|
|
$this->recordThat(new ShoppingBasketWasCheckedOut($newCustomerId)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function quickCheckout() |
46
|
|
|
{ |
47
|
|
|
Assert::that(array_sum($this->items))->greaterThan(0, 'Empty baskets cannot be processed'); |
48
|
|
|
Assert::that($this->returningCustomerId)->notNull('Quick checkout is possible only for returning customers'); |
49
|
|
|
$this->recordThat(new ShoppingBasketWasCheckedOut($this->returningCustomerId ?: '')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function whenItemWasAddedToShoppingBasket(ItemWasAddedToShoppingBasket $event) |
53
|
|
|
{ |
54
|
|
|
$this->items[$event->getItemCode()] = $this->items[$event->getItemCode()] ?? 0; |
55
|
|
|
$this->items[$event->getItemCode()] += $event->getQuantity(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function whenShoppingBasketWasCheckedOut(ShoppingBasketWasCheckedOut $event) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$this->isClosed = true; |
61
|
|
|
$this->items = []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function whenShoppingBasketWasPickedUpByNewCustomer(ShoppingBasketWasPickedUpByNewCustomer $event) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function whenShoppingBasketWasPickedUpByReturningCustomer(ShoppingBasketWasPickedUpByReturningCustomer $event) |
69
|
|
|
{ |
70
|
|
|
$this->returningCustomerId = $event->getReturningCustomerId(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function magicallyCreateCustomerId($billingData, $shippingData, $paymentInformation): string |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
return self::FAKE_CUSTOMER_ID; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.