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

-3
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

+2-1
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

-8
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

+70-4
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
}
+12
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

+12
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

+12
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+
}
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+
}
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

-11
This file was deleted.

src/Message/MessageCollection.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Era269\MessageProcessor\Message;
6+
7+
use Era269\MessageProcessor\MessageInterface;
8+
9+
final class MessageCollection implements MessageCollectionInterface
10+
{
11+
/**
12+
* @var MessageInterface[]
13+
*/
14+
private $messages;
15+
/**
16+
* @var int
17+
*/
18+
private $position = 0;
19+
20+
public function __construct(
21+
MessageInterface ...$messages
22+
)
23+
{
24+
$this->messages = $messages;
25+
}
26+
27+
public function current(): MessageInterface
28+
{
29+
return $this->messages[$this->position]
30+
?? new NullMessage();
31+
}
32+
33+
public function next(): void
34+
{
35+
++$this->position;
36+
}
37+
38+
public function key(): int
39+
{
40+
return $this->position;
41+
}
42+
43+
public function valid(): bool
44+
{
45+
return isset($this->messages[$this->position]);
46+
}
47+
48+
public function rewind(): void
49+
{
50+
$this->position = 0;
51+
}
52+
53+
public function attach(MessageInterface $message): void
54+
{
55+
$this->messages[] = $message;
56+
}
57+
58+
public function count(): int
59+
{
60+
return count($this->messages);
61+
}
62+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Era269\MessageProcessor\Message;
6+
7+
use Countable;
8+
use Era269\MessageProcessor\MessageInterface;
9+
use Iterator;
10+
11+
/**
12+
* @extends Iterator<int, MessageInterface>
13+
*/
14+
interface MessageCollectionInterface extends Iterator, Countable, MessageInterface
15+
{
16+
public function current(): MessageInterface;
17+
18+
public function next(): void;
19+
20+
public function key(): int;
21+
22+
public function valid(): bool;
23+
24+
public function rewind(): void;
25+
26+
public function attach(MessageInterface $event): void;
27+
}

src/MessageProcessorInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ interface MessageProcessorInterface
1010
/**
1111
* @throws RuntimeException
1212
*/
13-
public function process(MessageInterface $message): MessageInterface;
13+
public function process(MessageInterface $message): object;
1414
}

src/MethodMap/AbstractMethodMapDecorator.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ public function getMethodNames($object): array
3131

3232
/**
3333
* @param string[] $methodNames
34-
* @param object $message
3534
*
3635
* @return string[]
3736
*/
38-
abstract protected function getDecorated(array $methodNames, $message): array;
37+
abstract protected function getDecorated(array $methodNames, object $message): array;
3938
}

src/MethodMap/ExcludeMethodMapDecorator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(MethodMapInterface $methodMap, array $exclude)
2525
/**
2626
* @inheritDoc
2727
*/
28-
protected function getDecorated(array $methodNames, $message): array
28+
protected function getDecorated(array $methodNames, object $message): array
2929
{
3030
return array_diff(
3131
$methodNames,

src/MethodMap/OneOrLessMethodMapDecorator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class OneOrLessMethodMapDecorator extends AbstractMethodMapDecorator imple
1212
/**
1313
* @inheritDoc
1414
*/
15-
protected function getDecorated(array $methodNames, $message): array
15+
protected function getDecorated(array $methodNames, object $message): array
1616
{
1717
if (count($methodNames) > 1) {
1818
throw new LogicException(sprintf(

src/MethodMap/OneOrMoreMethodMapDecorator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class OneOrMoreMethodMapDecorator extends AbstractMethodMapDecorator imple
1212
/**
1313
* @inheritDoc
1414
*/
15-
protected function getDecorated(array $methodNames, $message): array
15+
protected function getDecorated(array $methodNames, object $message): array
1616
{
1717
if (empty($methodNames)) {
1818
throw new LogicException(sprintf(
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 ProcessMessageMethodMapAwareInterface
10+
{
11+
public function setProcessMessageMethodMap(MethodMapInterface $processMessageMethodMap): void;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Era269\MessageProcessor\Traits\Aware;
6+
7+
use Era269\MessageProcessor\Exception\ParameterIsNotSetLogicException;
8+
use Era269\MethodMap\MethodMapInterface;
9+
10+
trait ApplyEventMethodMapAwareTrait
11+
{
12+
/**
13+
* @var MethodMapInterface
14+
*/
15+
private $applyEventMethodMap;
16+
17+
protected function getApplyEventMethodMap(): MethodMapInterface
18+
{
19+
if (!isset($this->applyEventMethodMap)) {
20+
throw new ParameterIsNotSetLogicException(static::class, 'applyEventMethodMap');
21+
}
22+
23+
return $this->applyEventMethodMap;
24+
}
25+
26+
public function setApplyEventMethodMap(MethodMapInterface $applyEventMethodMap): void
27+
{
28+
$this->applyEventMethodMap = $applyEventMethodMap;
29+
}
30+
}

0 commit comments

Comments
 (0)