|
1
|
|
|
<?php |
|
2
|
|
|
namespace PSB\Core\Serialization\Pipeline; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use PSB\Core\HeaderTypeEnum; |
|
6
|
|
|
use PSB\Core\Pipeline\Outgoing\OutgoingContextFactory; |
|
7
|
|
|
use PSB\Core\Pipeline\Outgoing\StageContext\OutgoingLogicalMessageContext; |
|
8
|
|
|
use PSB\Core\Pipeline\Outgoing\StageContext\OutgoingPhysicalMessageContext; |
|
9
|
|
|
use PSB\Core\Pipeline\StageConnectorInterface; |
|
10
|
|
|
use PSB\Core\Serialization\MessageSerializerInterface; |
|
11
|
|
|
|
|
12
|
|
|
class SerializeMessageConnector implements StageConnectorInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var MessageSerializerInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $messageSerializer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var OutgoingContextFactory |
|
21
|
|
|
*/ |
|
22
|
|
|
private $contextFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param MessageSerializerInterface $messageSerializer |
|
26
|
|
|
* @param OutgoingContextFactory $contextFactory |
|
27
|
|
|
*/ |
|
28
|
5 |
|
public function __construct(MessageSerializerInterface $messageSerializer, OutgoingContextFactory $contextFactory) |
|
29
|
|
|
{ |
|
30
|
5 |
|
$this->messageSerializer = $messageSerializer; |
|
31
|
5 |
|
$this->contextFactory = $contextFactory; |
|
32
|
5 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param OutgoingLogicalMessageContext $context |
|
36
|
|
|
* @param callable $next |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function invoke($context, callable $next) |
|
39
|
|
|
{ |
|
40
|
2 |
|
$context->setHeader(HeaderTypeEnum::CONTENT_TYPE, $this->messageSerializer->getContentType()); |
|
41
|
2 |
|
$context->setHeader(HeaderTypeEnum::ENCLOSED_CLASS, $context->getMessage()->getMessageClass()); |
|
42
|
|
|
|
|
43
|
2 |
|
$body = $this->messageSerializer->serialize($context->getMessage()->getMessageInstance()); |
|
44
|
2 |
|
$outgoingPhysicalContext = $this->contextFactory->createPhysicalMessageContext($body, $context); |
|
45
|
|
|
|
|
46
|
2 |
|
$next($outgoingPhysicalContext); |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public static function getStageContextClass() |
|
53
|
|
|
{ |
|
54
|
1 |
|
return OutgoingLogicalMessageContext::class; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public static function getNextStageContextClass() |
|
61
|
|
|
{ |
|
62
|
1 |
|
return OutgoingPhysicalMessageContext::class; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|