Created
July 19, 2019 23:06
-
-
Save alexlcdee/68129e35e2834da3f546aabad582f9cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model\Task; | |
class Task | |
{ | |
/** | |
* @var Uuid | |
*/ | |
private $id; | |
/** | |
* @var string | |
*/ | |
private $name; | |
/** | |
* @var string | |
*/ | |
private $text; | |
/** | |
* @var \DateTimeImmutable | |
*/ | |
private $createdAt; | |
/** | |
* @var TaskStatus[]|Collection | |
*/ | |
private $statusLog; | |
protected function __construct() | |
{ | |
parent::__construct(); | |
$this->statusLog = new ArrayCollection(); | |
} | |
public function getCurrentStatus(): TaskStatus | |
{ | |
if ($this->statusLog->count()) { | |
return $this->statusLog->last(); | |
} | |
return new NewTask($this->createdAt); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName(): string | |
{ | |
return $this->name; | |
} | |
/** | |
* @return string | |
*/ | |
public function getText(): string | |
{ | |
return $this->text; | |
} | |
/** | |
* @return \DateTimeImmutable | |
*/ | |
public function getCreatedAt(): \DateTimeImmutable | |
{ | |
return $this->createdAt; | |
} | |
public static function createNew(UuidInterface $id, string $name, string $text, \DateTimeImmutable $at): TaskWasCreated | |
{ | |
return TaskWasCreated::withData($id, $name, $text, $at); | |
} | |
public function open(\DateTimeImmutable $at) | |
{ | |
return TaskWasOpened::byId($this->id, $at); | |
} | |
public function start(\DateTimeImmutable $at) | |
{ | |
return TaskWasStarted::byId($this->id, $at); | |
} | |
public function stop(\DateTimeImmutable $at) | |
{ | |
return TaskWasOpened::byId($this->id, $at); | |
} | |
public function done(\DateTimeImmutable $at) | |
{ | |
return TaskWasCompleted::byId($this->id, $at); | |
} | |
public function close(\DateTimeImmutable $at) | |
{ | |
return TaskWasClosed::byId($this->id, $at); | |
} | |
public function reopen(\DateTimeImmutable $at) | |
{ | |
return TaskWasOpened::byId($this->id, $at); | |
} | |
protected function aggregateId(): string | |
{ | |
return (string)$this->id; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Model\Task; | |
class Task extends AggregateRoot | |
{ | |
use AggregateEventApply; | |
/** | |
* @var Uuid | |
*/ | |
private $id; | |
/** | |
* @var string | |
*/ | |
private $name; | |
/** | |
* @var string | |
*/ | |
private $text; | |
/** | |
* @var \DateTimeImmutable | |
*/ | |
private $createdAt; | |
/** | |
* @var TaskStatus[]|Collection | |
*/ | |
private $statusLog; | |
protected function __construct() | |
{ | |
parent::__construct(); | |
$this->statusLog = new ArrayCollection(); | |
} | |
public function getCurrentStatus(): TaskStatus | |
{ | |
if ($this->statusLog->count()) { | |
return $this->statusLog->last(); | |
} | |
return new NewTask($this->createdAt); | |
} | |
/** | |
* @return string | |
*/ | |
public function getName(): string | |
{ | |
return $this->name; | |
} | |
/** | |
* @return string | |
*/ | |
public function getText(): string | |
{ | |
return $this->text; | |
} | |
/** | |
* @return \DateTimeImmutable | |
*/ | |
public function getCreatedAt(): \DateTimeImmutable | |
{ | |
return $this->createdAt; | |
} | |
/** | |
* @return TaskStatus[]|Collection | |
*/ | |
public function getStatusLog() | |
{ | |
return $this->statusLog; | |
} | |
public static function createNew(UuidInterface $id, string $name, string $text, \DateTimeImmutable $at): Task | |
{ | |
$object = new self(); | |
$object->recordThat(TaskWasCreated::withData($id, $name, $text, $at)); | |
$object->open($at); | |
return $object; | |
} | |
public function open(\DateTimeImmutable $at) | |
{ | |
$this->recordThat(TaskWasOpened::byId($this->id, $at)); | |
} | |
public function start(\DateTimeImmutable $at) | |
{ | |
$this->recordThat(TaskWasStarted::byId($this->id, $at)); | |
} | |
public function stop(\DateTimeImmutable $at) | |
{ | |
$this->recordThat(TaskWasOpened::byId($this->id, $at)); | |
} | |
public function done(\DateTimeImmutable $at) | |
{ | |
$this->recordThat(TaskWasCompleted::byId($this->id, $at)); | |
} | |
public function close(\DateTimeImmutable $at) | |
{ | |
$this->recordThat(TaskWasClosed::byId($this->id, $at)); | |
} | |
public function reopen(\DateTimeImmutable $at) | |
{ | |
$this->recordThat(TaskWasOpened::byId($this->id, $at)); | |
} | |
protected function whenTaskWasCreated(TaskWasCreated $event) | |
{ | |
$this->id = $event->getId(); | |
$this->name = $event->getName(); | |
$this->text = $event->getText(); | |
$this->createdAt = $event->getCreatedAt(); | |
} | |
protected function whenTaskWasOpened(TaskWasOpened $event) | |
{ | |
$this->statusLog->add($this->getCurrentStatus()->open($event->getOpenedAt())); | |
} | |
protected function whenTaskWasStarted(TaskWasStarted $event) | |
{ | |
$this->statusLog->add($this->getCurrentStatus()->inProgress($event->getStartedAt())); | |
} | |
protected function whenTaskWasCompleted(TaskWasCompleted $event) | |
{ | |
$this->statusLog->add($this->getCurrentStatus()->done($event->getCompletedAt())); | |
} | |
protected function whenTaskWasClosed(TaskWasClosed $event) | |
{ | |
$this->statusLog->add($this->getCurrentStatus()->close($event->getClosedAt())); | |
} | |
protected function aggregateId(): string | |
{ | |
return (string)$this->id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment