|
4 | 4 |
|
5 | 5 | namespace Era269\MessageProcessor;
|
6 | 6 |
|
7 |
| -use Era269\MessageProcessor\Traits\CacheAwareTrait; |
8 |
| -use Era269\MessageProcessor\Traits\CanApplyPrivateEventsTrait; |
| 7 | +use Cache\Adapter\PHPArray\ArrayCachePool; |
| 8 | +use Era269\MessageProcessor\MethodMap\ExcludeMethodMapDecorator; |
| 9 | +use Era269\MessageProcessor\MethodMap\OneOrLessMethodMapDecorator; |
| 10 | +use Era269\MessageProcessor\MethodMap\OneOrMoreMethodMapDecorator; |
| 11 | +use Era269\MessageProcessor\Traits\Aware\CacheAwareTrait; |
| 12 | +use Era269\MessageProcessor\Traits\CanApplyEventsTrait; |
9 | 13 | use Era269\MessageProcessor\Traits\CanProcessMessage;
|
| 14 | +use Era269\MessageProcessor\Traits\CanPublishEventsTrait; |
| 15 | +use Era269\MethodMap\ClassNameMethodMap; |
| 16 | +use Era269\MethodMap\InterfaceMethodMap; |
| 17 | +use Era269\MethodMap\MethodMapCacheDecorator; |
| 18 | +use Era269\MethodMap\MethodMapCollectionDecorator; |
| 19 | +use Era269\MethodMap\MethodMapInterface; |
| 20 | +use Psr\EventDispatcher\EventDispatcherInterface; |
| 21 | +use Psr\SimpleCache\CacheInterface; |
| 22 | +use ReflectionMethod; |
10 | 23 |
|
11 |
| -abstract class AbstractMessageProcessor implements MessageProcessorInterface |
| 24 | +abstract class AbstractMessageProcessor implements |
| 25 | + MessageProcessorInterface, |
| 26 | + CacheAwareInterface, |
| 27 | + EventDispatcherAwareInterface, |
| 28 | + ApplyEventMethodMapAwareInterface, |
| 29 | + ProcessMessageMethodMapAwareInterface |
12 | 30 | {
|
13 | 31 | use CacheAwareTrait;
|
14 | 32 | use CanProcessMessage;
|
15 |
| - use CanApplyPrivateEventsTrait; |
| 33 | + use CanApplyEventsTrait; |
| 34 | + use CanPublishEventsTrait; |
| 35 | + |
| 36 | + public function __construct(EventDispatcherInterface $eventDispatcher, ?MethodMapInterface $processMessageMethodMap = null, ?MethodMapInterface $applyEventMethodMap = null, ?CacheInterface $cache = null) |
| 37 | + { |
| 38 | + $this->setEventDispatcher($eventDispatcher); |
| 39 | + $this->setCache($cache ?? new ArrayCachePool()); |
| 40 | + $this->setProcessMessageMethodMap( |
| 41 | + $processMessageMethodMap |
| 42 | + ?? new MethodMapCacheDecorator( |
| 43 | + new OneOrMoreMethodMapDecorator( |
| 44 | + new OneOrLessMethodMapDecorator( |
| 45 | + new ExcludeMethodMapDecorator( |
| 46 | + new MethodMapCollectionDecorator( |
| 47 | + new ClassNameMethodMap(static::class), |
| 48 | + new InterfaceMethodMap(static::class) |
| 49 | + ), |
| 50 | + ['process'] |
| 51 | + ) |
| 52 | + ) |
| 53 | + ), |
| 54 | + $this->getCache(), |
| 55 | + static::class |
| 56 | + ) |
| 57 | + ); |
| 58 | + $this->setApplyEventMethodMap( |
| 59 | + $applyEventMethodMap |
| 60 | + ?? new MethodMapCacheDecorator( |
| 61 | + new OneOrMoreMethodMapDecorator( |
| 62 | + new OneOrLessMethodMapDecorator( |
| 63 | + new ClassNameMethodMap( |
| 64 | + static::class, |
| 65 | + ReflectionMethod::IS_PROTECTED |
| 66 | + ) |
| 67 | + ) |
| 68 | + ), |
| 69 | + $this->getCache(), |
| 70 | + static::class |
| 71 | + ) |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + protected function applyAndPublish(object ...$events): void |
| 76 | + { |
| 77 | + foreach ($events as $event) { |
| 78 | + $this->applyThat($event); |
| 79 | + $this->publishThat($event); |
| 80 | + } |
| 81 | + } |
16 | 82 | }
|
0 commit comments