Skip to content

Instantly share code, notes, and snippets.

@simensen
Last active December 1, 2015 15:32
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save simensen/61cd0a8ffaa5a10411ea to your computer and use it in GitHub Desktop.
<?php
namespace Monii\AddressBook\Domain\Model\Person\Adapter\Depot;
use Monii\AddressBook\Domain\Model\Person\Person;
use Monii\AddressBook\Domain\Model\Person\PersonId;
use Monii\AddressBook\Domain\Model\Person\PersonRepository;
use Depot\AggregateRoot\Error\AggregateRootIsAlreadyTracked;
use Depot\AggregateRoot\UnitOfWork;
use Depot\Contract\Contract;
use Depot\Contract\SimplePhpFqcnContractResolver;
use Depot\EventStore\Transaction\CommitId;
class DepotPersonRepository implements PersonRepository
{
/**
* @var UnitOfWork
*/
private $unitOfWork;
/**
* @var Contract
*/
private $contract;
public function __construct(UnitOfWork $unitOfWork)
{
$this->unitOfWork = $unitOfWork;
$this->contract = (new SimplePhpFqcnContractResolver())
->resolveFromClassName(Person::class)
;
}
/**
* {@inheritdoc}
*/
public function find(PersonId $personId)
{
return $this->unitOfWork->get($this->contract, (string) $personId);
}
/**
* {@inheritdoc}
*/
public function save(Person $person)
{
try {
$this->unitOfWork->track(
$this->contract,
(string)$person->getPersonId(),
$person
);
} catch (AggregateRootIsAlreadyTracked $e) {
// We will just say this is OK for now.
}
$this->unitOfWork->commit(CommitId::fromString(uniqid()));
}
}
<?php
namespace Monii\Laravel\AddressBook\Providers\Model;
use Monii\AddressBook\Domain\Model\Person\Adapter\Depot\DepotPersonRepository;
use Monii\AddressBook\Domain\Model\Person\Adapter\EventDispatching\EventDispatchingPersonRepository;
use Monii\AddressBook\Domain\Model\Person\PersonRepository;
use Depot\AggregateRoot\UnitOfWork;
use Monii\Common\Infrastructure\EventBus\EventBus;
use Monii\Laravel\MoniiDomainServiceProvider;
class PersonProvider extends MoniiDomainServiceProvider
{
public function registerRepository()
{
$this->container->singleton(PersonRepository::class, function () {
$depotPersonRepository = new DepotPersonRepository(
$this->app->make(UnitOfWork::class)
);
return new EventDispatchingPersonRepository(
$depotPersonRepository,
$this->app->make(EventBus::class)
);
});
}
}
<?php
namespace Monii\Laravel\Common\Providers;
use Doctrine\DBAL\Connection;
use Illuminate\Contracts\Container\Container;
use Depot\AggregateRoot\AggregateRootChangeManipulator;
use Depot\AggregateRoot\AggregateRootManipulator;
use Depot\AggregateRoot\ChangeReading\PublicMethodsChangeReader;
use Depot\AggregateRoot\ChangesClearing\PublicMethodChangesClearor;
use Depot\AggregateRoot\ChangesExtraction\PublicMethodChangesExtractor;
use Depot\AggregateRoot\ChangeWriting\NamedConstructorChangeWriter;
use Depot\AggregateRoot\Identification\PublicMethodIdentifier;
use Depot\AggregateRoot\Instantiation\NamedConstructorInstantiator;
use Depot\AggregateRoot\Reconstitution\PublicMethodReconstituter;
use Depot\AggregateRoot\UnitOfWork;
use Depot\AggregateRoot\VersionReading\PublicMethodVersionReader;
use Depot\Contract\SimplePhpFqcnContractResolver;
use Depot\EventStore\EventStore;
use Depot\EventStore\Persistence\Adapter\Dbal\DbalPersistence;
use Depot\EventStore\Persistence\Persistence;
use Depot\EventStore\Serialization\Adapter\MoniiReflectionPropertiesSerializer\MoniiReflectionPropertiesSerializer;
use Depot\EventStore\Transaction\CallableCommitIdGenerator;
use Depot\EventStore\Transaction\CommitId;
use Monii\Common\Domain\Model\AggregateRoot\EventEnvelope;
class DepotProvider
{
public function register(Container $container)
{
$container->singleton(Persistence::class, function (Container $container) {
$serializer = new MoniiReflectionPropertiesSerializer(
new SimplePhpFqcnContractResolver()
);
$contractResolver = new SimplePhpFqcnContractResolver();
return new DbalPersistence(
$container->make(Connection::class),
$serializer,
$serializer,
$contractResolver,
$contractResolver,
'event'
);
});
$container->singleton(UnitOfWork::class, function (Container $container) {
$persistence = $container->make(Persistence::class);
$eventStore = new EventStore($persistence);
$contractResolver = new SimplePhpFqcnContractResolver();
return new UnitOfWork(
$eventStore,
new AggregateRootManipulator(
new NamedConstructorInstantiator(),
new PublicMethodReconstituter(),
new PublicMethodIdentifier(),
new PublicMethodVersionReader(),
new PublicMethodChangesExtractor(),
new PublicMethodChangesClearor()
),
new AggregateRootChangeManipulator(
new PublicMethodsChangeReader(),
new NamedConstructorChangeWriter(EventEnvelope::class)
),
$contractResolver,
$contractResolver,
null,
new CallableCommitIdGenerator(function () {
return CommitId::fromString(uniqid());
})
);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment