|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Era269\MessageProcessor\Traits; |
| 6 | + |
| 7 | +use Era269\MessageProcessor\Message\EventInterface; |
| 8 | +use Era269\MessageProcessor\MethodMap\OneOrLessMethodMapDecorator; |
| 9 | +use Era269\MessageProcessor\MethodMap\OneOrMoreMethodMapDecorator; |
| 10 | +use Era269\MethodMap\ClassNameMethodMap; |
| 11 | +use Era269\MethodMap\MethodMapCacheDecorator; |
| 12 | +use Era269\MethodMap\MethodMapInterface; |
| 13 | +use Psr\SimpleCache\CacheInterface; |
| 14 | +use ReflectionMethod; |
| 15 | + |
| 16 | +trait CanGetMethodNameByEventTrait |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var MethodMapInterface |
| 20 | + */ |
| 21 | + private $applyEventMap; |
| 22 | + |
| 23 | + /** |
| 24 | + * @param ClassNameMethodMap $applyEventMap |
| 25 | + */ |
| 26 | + public function setApplyEventMap(MethodMapInterface $applyEventMap): void |
| 27 | + { |
| 28 | + $this->applyEventMap = $this->getDecoratedByExactlyOneMethodMap($applyEventMap); |
| 29 | + } |
| 30 | + |
| 31 | + private function getApplyEventMethodName(EventInterface $event): string |
| 32 | + { |
| 33 | + return current( |
| 34 | + $this->getApplyEventMap()->getMethodNames($event) |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + private function getApplyEventMap(): MethodMapInterface |
| 39 | + { |
| 40 | + if (!isset($this->applyEventMap)) { |
| 41 | + $this->applyEventMap = |
| 42 | + new MethodMapCacheDecorator( |
| 43 | + $this->getDecoratedByExactlyOneMethodMap( |
| 44 | + new ClassNameMethodMap( |
| 45 | + static::class, |
| 46 | + ReflectionMethod::IS_PROTECTED |
| 47 | + ) |
| 48 | + ), |
| 49 | + $this->getCache(), |
| 50 | + static::class |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + return $this->applyEventMap; |
| 55 | + } |
| 56 | + |
| 57 | + private function getDecoratedByExactlyOneMethodMap(MethodMapInterface $methodMap): MethodMapInterface |
| 58 | + { |
| 59 | + return new OneOrMoreMethodMapDecorator( |
| 60 | + new OneOrLessMethodMapDecorator( |
| 61 | + $methodMap |
| 62 | + ) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + abstract protected function getCache(): CacheInterface; |
| 67 | +} |
0 commit comments