Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Commit

Permalink
Add rename() to the Herd aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
ramondelafuente committed Sep 29, 2017
1 parent 74a45da commit acd2f99
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions spec/Elewant/Herding/Model/HerdSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ function it_throws_an_exception_when_abandoning_a_not_owned_elephpant()
->duringAbandonElePHPant(Breed::greenZf2Regular());
}

function it_can_be_renamed()
{
$this->name()->shouldEqual($this->herdName);
$this->rename('new name');
$this->name()->shouldEqual('new name');
}

function it_can_be_abandoned()
{
$this->shouldHaveType(Herd::class);
Expand Down
22 changes: 22 additions & 0 deletions src/Elewant/Herding/Model/Herd.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Elewant\Herding\Model\Events\ElePHPantWasAdoptedByHerd;
use Elewant\Herding\Model\Events\HerdWasAbandoned;
use Elewant\Herding\Model\Events\HerdWasFormed;
use Elewant\Herding\Model\Events\HerdWasRenamed;
use Prooph\EventSourcing\AggregateChanged;
use Prooph\EventSourcing\AggregateRoot;

Expand Down Expand Up @@ -76,6 +77,18 @@ public function abandon()
);
}

public function rename(string $newName): void
{
$this->guardIsNotAbandoned();

$this->recordThat(
HerdWasRenamed::tookPlace(
$this->herdId,
$newName
)
);
}

public function adoptElePHPant(Breed $breed): void
{
$this->guardIsNotAbandoned();
Expand Down Expand Up @@ -130,6 +143,10 @@ protected function apply(AggregateChanged $event): void
/** @var ElePHPantWasAbandonedByHerd $event */
$this->applyAnElePHPantWasAbandonedByHerd($event->herdId(), $event->elePHPantId(), $event->breed());
break;
case HerdWasRenamed::class:
/** @var HerdWasRenamed $event */
$this->applyHerdWasRenamed($event->newHerdName());
break;
case HerdWasAbandoned::class:
/** @var HerdWasAbandoned $event */
$this->applyHerdWasAbandoned($event->herdId(), $event->shepherdId());
Expand Down Expand Up @@ -165,6 +182,11 @@ private function applyHerdWasAbandoned($herdId, $shepherdId)
$this->abandoned = true;
}

private function applyHerdWasRenamed(string $newHerdName)
{
$this->name = $newHerdName;
}

private function guardIsNotAbandoned()
{
if ($this->abandoned) {
Expand Down

0 comments on commit acd2f99

Please sign in to comment.