-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInboxProcessSetup.php
66 lines (54 loc) · 1.77 KB
/
InboxProcessSetup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace ShipSaasInboxProcess;
use Illuminate\Http\JsonResponse;
use ShipSaasInboxProcess\Http\Requests\AbstractInboxRequest;
use ShipSaasInboxProcess\Http\Requests\DefaultCustomInboxRequest;
final class InboxProcessSetup
{
/**
* @var \ArrayAccess<string, AbstractInboxRequest>
*/
private static array $topicRequestMap = [];
/**
* @var \ArrayAccess<string, callable>
*/
private static array $topicResponseMap = [];
/**
* @var \ArrayAccess<string, string[]>
*/
private static array $topicHandlersMap = [];
public static function addRequest(string $topic, AbstractInboxRequest $request): void
{
self::$topicRequestMap[$topic] = $request;
}
public static function getRequest(string $topic): AbstractInboxRequest
{
if (!isset(self::$topicRequestMap[$topic])) {
return new DefaultCustomInboxRequest();
}
return self::$topicRequestMap[$topic];
}
public static function addResponse(string $topic, callable $responseGenerator): void
{
self::$topicResponseMap[$topic] = $responseGenerator;
}
public static function getResponse(string $topic): callable
{
return self::$topicResponseMap[$topic]
?? fn () => new JsonResponse('ok');
}
/**
* Register an inbox handler for the given topic
*
* @param string $topic The topic to register
* @param string|callable $processor The handler, can be a classpath or a closure
*/
public static function addProcessor(string $topic, string|callable $processor): void
{
self::$topicHandlersMap[$topic][] = $processor;
}
public static function getProcessors(string $topic): array
{
return self::$topicHandlersMap[$topic] ?? [];
}
}