-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from patchlevel/attribute-apply-trait
add attribute based apply methods
- Loading branch information
Showing
14 changed files
with
724 additions
and
67 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ parameters: | |
level: max | ||
paths: | ||
- src | ||
checkGenericClassInNonGenericObjectType: false |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Aggregate; | ||
|
||
use function sprintf; | ||
|
||
final class ApplyAttributeNotFound extends AggregateException | ||
{ | ||
public function __construct(AggregateRoot $aggregate, AggregateChanged $event) | ||
{ | ||
parent::__construct( | ||
sprintf( | ||
'Apply method in "%s" could not be found for the event "%s"', | ||
$aggregate::class, | ||
$event::class | ||
) | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Aggregate; | ||
|
||
use Patchlevel\EventSourcing\Attribute\Apply; | ||
use Patchlevel\EventSourcing\Attribute\SuppressMissingApply; | ||
use ReflectionClass; | ||
|
||
use function array_key_exists; | ||
use function method_exists; | ||
|
||
/** | ||
* @psalm-require-extends AggregateRoot | ||
*/ | ||
trait AttributeApplyMethod | ||
{ | ||
/** @var array<class-string<AggregateChanged>, true> */ | ||
private static array $suppressEvents = []; | ||
private static bool $suppressAll = false; | ||
|
||
/** @var array<class-string<AggregateChanged>, string>|null */ | ||
private static ?array $aggregateChangeMethodMap = null; | ||
|
||
protected function apply(AggregateChanged $event): void | ||
{ | ||
$map = self::aggregateChangeMethodMap(); | ||
|
||
if (!array_key_exists($event::class, $map)) { | ||
if (!self::$suppressAll && !array_key_exists($event::class, self::$suppressEvents)) { | ||
throw new ApplyAttributeNotFound($this, $event); | ||
} | ||
|
||
return; | ||
} | ||
|
||
$method = $map[$event::class]; | ||
|
||
if (!method_exists($this, $method)) { | ||
return; | ||
} | ||
|
||
$this->$method($event); | ||
} | ||
|
||
/** | ||
* @return array<class-string<AggregateChanged>, string> | ||
*/ | ||
private static function aggregateChangeMethodMap(): array | ||
{ | ||
if (self::$aggregateChangeMethodMap !== null) { | ||
return self::$aggregateChangeMethodMap; | ||
} | ||
|
||
$reflector = new ReflectionClass(self::class); | ||
$attributes = $reflector->getAttributes(SuppressMissingApply::class); | ||
|
||
foreach ($attributes as $attribute) { | ||
$instance = $attribute->newInstance(); | ||
|
||
if ($instance->suppressAll()) { | ||
self::$suppressAll = true; | ||
|
||
continue; | ||
} | ||
|
||
foreach ($instance->suppressEvents() as $event) { | ||
self::$suppressEvents[$event] = true; | ||
} | ||
} | ||
|
||
$methods = $reflector->getMethods(); | ||
|
||
self::$aggregateChangeMethodMap = []; | ||
|
||
foreach ($methods as $method) { | ||
$attributes = $method->getAttributes(Apply::class); | ||
|
||
foreach ($attributes as $attribute) { | ||
$instance = $attribute->newInstance(); | ||
$eventClass = $instance->aggregateChangedClass(); | ||
|
||
if (array_key_exists($eventClass, self::$aggregateChangeMethodMap)) { | ||
throw new DuplicateApplyMethod( | ||
self::class, | ||
$eventClass, | ||
self::$aggregateChangeMethodMap[$eventClass], | ||
$method->getName() | ||
); | ||
} | ||
|
||
self::$aggregateChangeMethodMap[$eventClass] = $method->getName(); | ||
} | ||
} | ||
|
||
return self::$aggregateChangeMethodMap; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Aggregate; | ||
|
||
use function sprintf; | ||
|
||
final class DuplicateApplyMethod extends AggregateException | ||
{ | ||
/** | ||
* @param class-string<AggregateRoot> $aggregate | ||
* @param class-string<AggregateChanged> $event | ||
*/ | ||
public function __construct(string $aggregate, string $event, string $fistMethod, string $secondMethod) | ||
{ | ||
parent::__construct( | ||
sprintf( | ||
'Two methods "%s" and "%s" on the aggregate "%s" want to apply the same event "%s".', | ||
$fistMethod, | ||
$secondMethod, | ||
$aggregate, | ||
$event | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.