Permalink
Browse files

Finished upto separate classes for each Invoice state

  • Loading branch information...
0 parents commit 946c81277a05184c81219cfd21a1bcdf6fac57df Amjad Mohamed committed Sep 14, 2011
Showing with 203 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +121 −0 Domain/Model/Helpers.php
  3. +79 −0 Domain/Model/Invoice.php
@@ -0,0 +1,3 @@
+.buildpath
+.settings
+.project
@@ -0,0 +1,121 @@
+<?php
+
+namespace Domain\Model;
+
+class EventSourced {
+ //abstract protected function applyEvent($event);
+ protected function unhandled($event) {
+ throw new \Exception("This event is not handled");
+ }
+}
+
+class AggregateRoot extends EventSourced {
+
+ public $uncommitedEvents = array();
+
+ protected function markCommited() {
+ $this->uncommitedEvents = array();
+ }
+
+ protected function record($event) {
+ $this->applyEvent($event);
+ $this->uncommitedEvents[] = $event;
+ }
+
+}
+
+class AggregateFactory extends EventSourced {
+
+ public function loadFromHistory($history) {
+ $invoice = $this->applyEvent(array_shift($history));
+ foreach ($history as $event) {
+ $invoice->applyEvent($event);
+ }
+ return $invoice->markCommitted();
+ }
+
+}
+
+class InvoiceFactory extends AggregateFactory {
+
+ public function create($id, $recipient) {
+ return $this->applyEvent(new InvoiceCreated($id, $recipient));
+ }
+
+ public function applyEvent($event) {
+ $event_type = get_class($event);
+ switch($event_type) {
+ case 'Domain\Model\InvoiceCreated':
+ $invoice = new InvoiceDraft($event->id, array($event), $event->recipient);
+ break;
+ }
+
+ return $invoice;
+ }
+
+}
+
+abstract class InvoiceEvent {
+ public $id;
+ public function __construct($id) {
+ $this->id = $id;
+ }
+}
+
+class InvoiceCreated extends InvoiceEvent {
+ public $recipient;
+ public function __construct($id, $recipient) {
+ parent::__construct($id);
+ $this->recipient = $recipient;
+ }
+}
+
+class InvoiceRecipientChanged extends InvoiceEvent {
+ public $recipient;
+ public function __construct($id, $recipient) {
+ parent::__construct($id);
+ $this->recipient = $recipient;
+ }
+}
+
+class InvoiceItemAdded extends InvoiceEvent {
+ public $item;
+ public function __construct($id, $item) {
+ parent::__construct($id);
+ $this->item = $item;
+ }
+}
+
+class InvoiceItemRemoved extends InvoiceEvent {
+ public $item;
+ public function __construct($id, $item) {
+ parent::__construct($id);
+ $this->item = $item;
+ }
+}
+
+class InvoiceSent extends InvoiceEvent {
+ public $sentDate;
+ public $dueDate;
+ public function __construct($id, $sentDate, $dueDate) {
+ parent::__construct($id);
+ $this->sentDate = $sentDate;
+ $this->dueDate = $dueDate;
+ }
+}
+
+class InvoiceReminderEvent extends InvoiceEvent {
+ public $reminderDate;
+ public function __construct($id, $reminderDate) {
+ parent::__construct($id);
+ $this->reminderDate = $reminderDate;
+ }
+}
+
+class InvoicePaymentReceived extends InvoiceEvent {
+ public $paymentDate;
+ public function __construct($id, $paymentDate) {
+ parent::__construct($id);
+ $this->paymentDate = $paymentDate;
+ }
+}
@@ -0,0 +1,79 @@
+<?php
+
+namespace Domain\Model;
+
+require_once 'Helpers.php';
+
+abstract class Invoice extends AggregateRoot {
+ protected $id;
+ public function __construct($id, $uncommitted_events) {
+ $this->uncommitedEvents = $uncommitted_events;
+ $this->id = $id;
+ }
+}
+
+class InvoiceDraft extends Invoice {
+
+ private $recipient;
+ private $nextItemId;
+ private $items = array();
+ private $totalAmount;
+
+ public function __construct($id, $uncommitted_events, $recipient) {
+ parent::__construct($id, $uncommitted_events);
+ $this->recipient = $recipient;
+ }
+
+ public function removeItem($index) {
+ if ($this->isSent()) {
+ throw new \Exception("Cannot remove items from sent invoices");
+ }
+
+ $item = $this->_items[$index];
+ unset($this->_items[$index]);
+
+ $this->_totalAmount -= $item;
+ }
+
+ public function send() {
+ $sentDate = date('Y-m-d');
+ $dueDate = date('Y-m-d', strtotime($sentDate . ' +14 days'));
+ return $this->applySent(new InvoiceSent($this->id, $sentDate, $dueDate));
+ }
+
+ public function applySent($event) {
+ $this->uncommitedEvents[] = $event;
+ $invoice = new SentInvoice($this->id, $this->uncommitedEvents, $event->sentDate, $event->dueDate);
+ return $invoice;
+ }
+
+ public function applyEvent($event) {
+
+ }
+
+}
+
+class SentInvoice extends Invoice {
+
+ private $paid;
+ private $sentDate;
+ private $dueDate;
+
+ public function __construct($id, $uncommitted_events, $sentDate, $dueDate) {
+ parent::__construct($id, $uncommitted_events);
+ $this->sendDate = $sentDate;
+ $this->dueDate = $dueDate;
+ }
+
+ public function ApplyEvent($event) {
+
+ }
+
+}
+
+$fac = new InvoiceFactory();
+
+$invoice = $fac->create(1, 'me')
+ ->send();
+
+var_dump($invoice->uncommitedEvents);

0 comments on commit 946c812

Please sign in to comment.