brandembassy / unit-of-work
Installs: 101 098
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 7
Forks: 0
Open Issues: 1
Requires
- php: ^7.4 || >=8.1
- beberlei/assert: ^3.2
- psr/log: ^1.1
Requires (Dev)
- brandembassy/coding-standard: 10.0.0-beta
- mockery/mockery: ^1.5.1
- nette/utils: ^2.4 || ^3.0
- phpunit/phpunit: ^9.6.6
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-10-26 15:54:59 UTC
README
This library provides tooling for post-processing. Lets explain it on the example:
You have some domain entity (lets call it a Document
). And in your logic you do a lot of
different changes and data transformations and you want to persist those changes
for example into ElasticSearch database.
The problem is that if you save it right in place after the change, you may end up with multiple writes.
By using this library you can track all of those changes and merge them.
function doBoringStuff(Document $document): UnitOfWork { $document->changeBoringStuff(); $unitOfWork = new UnitOfWork(); $unitOfWork->registerOperation(new SaveDocumentOperation($document)); return $unitOfWork; } function doFunkyStuff(Document $document): UnitOfWork { $document->changeFunkyStuff(); $unitOfWork = new UnitOfWork(); $unitOfWork->registerOperation(new SaveDocumentOperation($document)); return $unitOfWork; } $boringWork = doBoringStuff($document); $funkyWork = doFunkyStuff($document) $unitOfWork = $boringWork->concatenate($funkyWork); // Every Processor has its own Accessor for Dependecy Injection Container $accessors = [new SaveDocumentOperationProcessorAccessor()]; $naiveExecutor = new NaiveUnitOfWorkExecutor($accesors); $executor = new ReducingUnitOfWorkExecutor($naiveExecutor); // Decorator pattern to separate merging logic // And document will be saved only once if your SaveDocumentOperation is implemented // as mergable. See: `MergeableOperation` as example. $executor->execute($unitOfWork);
Example of usage inside Nette Dependecy Injection container:
unitOfWorkExecutor: BrandEmbassy\UnitOfWork\ReducingUnitOfWorkExecutor( BrandEmbassy\UnitOfWork\NaiveUnitOfWorkExecutor([@saveDocumentOperationProcessorAccessor]) ) - Foo\Bar\SaveDocumentOperationProcessor saveDocumentOperationProcessorAccessor: Foo\Bar\SaveDocumentOperationProcessorAccessor