Skip to content

Commit 158be09

Browse files
authored
event-publish (#6)
* event-publish
1 parent d89d071 commit 158be09

34 files changed

+672
-275
lines changed

.github/workflows/main.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ jobs:
1717
phpunit-versions: ['latest']
1818
phpstan-versions: ['latest']
1919
include:
20-
- php-versions: '7.1'
21-
phpunit-versions: '7.5.20'
22-
phpstan-versions: ['latest']
2320
- php-versions: '7.2'
2421
phpunit-versions: '8.5.15'
2522
phpstan-versions: ['latest']

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
"license": "MIT",
77
"prefer-stable": true,
88
"require": {
9-
"php": "^8.0|^7.1",
9+
"php": "^8.0|^7.2",
1010
"cache/array-adapter": "^1.1",
1111
"era269/method-map": "^0.1.0",
12+
"psr/event-dispatcher": "^1.0",
1213
"psr/simple-cache": "^1.0"
1314
},
1415
"require-dev": {

phpstan.neon

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,3 @@ parameters:
22
level: 8
33
paths:
44
- src
5-
ignoreErrors:
6-
-
7-
message: '/Method Era269\\MessageProcessor\\AbstractMessageProcessor::getMethodName\(\) should return string but returns string\|false./'
8-
path: ./src/Traits/CanGetMethodNameByMessageTrait.php
9-
-
10-
message: '/Method Era269\\MessageProcessor\\AbstractMessageProcessor::getApplyEventMethodName\(\) should return string but returns string\|false./'
11-
path: ./src/Traits/CanGetMethodNameByEventTrait.php
12-

src/AbstractMessageProcessor.php

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,79 @@
44

55
namespace Era269\MessageProcessor;
66

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;
913
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;
1023

11-
abstract class AbstractMessageProcessor implements MessageProcessorInterface
24+
abstract class AbstractMessageProcessor implements
25+
MessageProcessorInterface,
26+
CacheAwareInterface,
27+
EventDispatcherAwareInterface,
28+
ApplyEventMethodMapAwareInterface,
29+
ProcessMessageMethodMapAwareInterface
1230
{
1331
use CacheAwareTrait;
1432
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+
}
1682
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Era269\MessageProcessor;
6+
7+
use Era269\MethodMap\MethodMapInterface;
8+
9+
interface ApplyEventMethodMapAwareInterface
10+
{
11+
public function setApplyEventMethodMap(MethodMapInterface $applyEventMethodMap): void;
12+
}

src/CacheAwareInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Era269\MessageProcessor;
6+
7+
use Psr\SimpleCache\CacheInterface;
8+
9+
interface CacheAwareInterface
10+
{
11+
public function setCache(CacheInterface $cache): void;
12+
}

src/EventDispatcherAwareInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Era269\MessageProcessor;
6+
7+
use Psr\EventDispatcher\EventDispatcherInterface;
8+
9+
interface EventDispatcherAwareInterface
10+
{
11+
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void;
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Era269\MessageProcessor\Exception;
6+
7+
use Throwable;
8+
9+
interface MessageProcessorExceptionInterface extends Throwable
10+
{
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Era269\MessageProcessor\Exception;
6+
7+
use LogicException;
8+
9+
final class ParameterIsNotSetLogicException extends LogicException implements MessageProcessorExceptionInterface
10+
{
11+
private const FORMAT_MESSAGE = '%s::%s is not set.';
12+
public function __construct(string $className, string $parameterName)
13+
{
14+
parent::__construct(sprintf(
15+
self::FORMAT_MESSAGE,
16+
$className,
17+
$parameterName
18+
));
19+
}
20+
}

src/Message/EventInterface.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)