-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoice.php
More file actions
64 lines (52 loc) · 1.65 KB
/
Invoice.php
File metadata and controls
64 lines (52 loc) · 1.65 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
<?php declare(strict_types=1);
namespace ExampleDomain;
use Cocoders\EventStore\AggregateRoot;
use Cocoders\EventStore\AggregateRootBehavior;
use ExampleDomain\Invoice\Amount;
use ExampleDomain\Invoice\Events\InvoiceIssued;
use ExampleDomain\Invoice\Events\ItemAdded;
final class Invoice implements AggregateRoot
{
use AggregateRootBehavior;
/**
* @var Invoice\Id
*/
private $id;
private $maxItemNumbers;
private $itemNumbers = 0;
private function __construct(Invoice\Id $id)
{
$this->id = $id;
}
public static function issueInvoice(
Invoice\Id $id,
Invoice\Seller $seller,
Invoice\Buyer $buyer,
int $maxItemNumbers = 3
): Invoice {
$invoice = new Invoice($id);
$invoiceIssued = new InvoiceIssued($id, $seller, $buyer, $maxItemNumbers);
$invoice->events[] = $invoiceIssued;
$invoice->applyInvoiceIssued($invoiceIssued);
return $invoice;
}
public function addItem($name, Amount $amount, $quantity)
{
if ($this->itemNumbers >= $this->maxItemNumbers) {
throw new \LogicException(
sprintf('Max item number is %d, so you cannot add next item', $this->maxItemNumbers)
);
}
$itemAdded = new ItemAdded($this->id, $name, $amount, $quantity);
$this->events[] = $itemAdded;
$this->applyItemAdded($itemAdded);
}
private function applyInvoiceIssued(InvoiceIssued $invoiceIssued)
{
$this->maxItemNumbers = $invoiceIssued->getMaxItemNumbers();
}
private function applyItemAdded(ItemAdded $itemAdded)
{
$this->itemNumbers++;
}
}