Skip to content

phpgears/aggregate

Repository files navigation

PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

Aggregate

Aggregate base

Installation

Composer

composer require phpgears/aggregate

Usage

Require composer autoload file

require './vendor/autoload.php';

Aggregate identity

Aggregate identities are provided by gears/identity, head over there to learn about them

Aggregate root

Aggregate roots should implement Gears\Aggregate\AggregateRoot interface. You can extend from Gears\Aggregate\AbstractAggregateRoot for simplicity

use Gears\Aggregate\AbstractAggregateRoot;
use Gears\Identity\Identity;

class CustomAggregate extends AbstractAggregateRoot
{
    public static function instantiate(Identity $identity): self
    {
        return new self($identity);
    }
}

Mind that AbstractAggregateRoot constructor is protected forcing you to create static named constructors methods

Entities

Entities can implement Gears\Aggregate\Entity interface. You can extend from Gears\Aggregate\AbstractEntity for simplicity

Events

Aggregate roots can record gears/event as operations are performed

use Gears\Aggregate\AbstractAggregateRoot;
use Gears\Identity\Identity;

class CustomAggregate extends AbstractAggregateRoot
{
    public static function instantiate(Identity $identity): self
    {
        return new self($identity);
    }

    public function doSomething(): void
    {
        // do something

        $this->recordEvent(new SomethingHappened());
    }
}

These events could be collected afterwards and sent to an event bus such as gears/event

$customAggregate = CustomAggregate::instantiate(
    UuidIdentity::fromString('4c4316cb-b48b-44fb-a034-90d789966bac')
);
$customAggregate->doSomething();

foreach ($customAggregate->collectRecordedEvents() as $event) {
    /** @var \Gears\Event\EventBus $eventBus */
    $eventBus->dispatch($event);
}

Contributing

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.

See file CONTRIBUTING.md

License

See file LICENSE included with the source code for a copy of the license terms.

Packages

No packages published

Languages