Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ffdf909

Browse filesBrowse files
committed
feat(notifier): add SentMessage additional info
1 parent dfcc142 commit ffdf909
Copy full SHA for ffdf909

File tree

3 files changed

+104
-0
lines changed
Filter options

3 files changed

+104
-0
lines changed

‎src/Symfony/Component/Notifier/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/CHANGELOG.md
+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add `SentMessage` additional info array
8+
49
7.2
510
---
611

‎src/Symfony/Component/Notifier/Message/SentMessage.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Message/SentMessage.php
+44
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111

1212
namespace Symfony\Component\Notifier\Message;
1313

14+
use Symfony\Contracts\HttpClient\ResponseInterface;
15+
1416
/**
1517
* @author Jérémy Romey <jeremy@free-agent.fr>
1618
*/
1719
class SentMessage
1820
{
1921
private ?string $messageId = null;
22+
private array $info = [];
2023

2124
public function __construct(
2225
private MessageInterface $original,
@@ -43,4 +46,45 @@ public function getMessageId(): ?string
4346
{
4447
return $this->messageId;
4548
}
49+
50+
/**
51+
* Returns any arbitrary data attached to the message. If an HTTP response has been set by the Transport,
52+
* it should be available in 'http_response' key.
53+
*
54+
* @param string|null $key if null, the whole info array will be returned, else returns the info value or null
55+
*/
56+
public function getInfo(?string $key = null): mixed
57+
{
58+
if (null !== $key) {
59+
return $this->info[$key] ?? null;
60+
}
61+
62+
return $this->info;
63+
}
64+
65+
/**
66+
* Attaches an arbitrary info to the message.
67+
* This can be used in order to set any HTTP response body if the Transport uses an HttpClient, for instance.
68+
*
69+
* @return $this
70+
*/
71+
public function setInfo(string $key, mixed $value): static
72+
{
73+
if (null === $value) {
74+
unset($this->info[$key]);
75+
} else {
76+
$this->info[$key] = $value;
77+
}
78+
79+
return $this;
80+
}
81+
82+
/**
83+
* As many of the Transports are using HTTP requests,
84+
* this shortcut attaches an HTTP response to the message in a standardised way.
85+
*/
86+
public function setHttpResponseInfo(ResponseInterface $response): void
87+
{
88+
$this->setInfo('http_response', $response->getInfo() + ['body' => $response->toArray(false)]);
89+
}
4690
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Tests\Message;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Message\SentMessage;
16+
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
17+
use Symfony\Contracts\HttpClient\ResponseInterface;
18+
19+
class SentMessageTest extends TestCase
20+
{
21+
public function test()
22+
{
23+
$originalMessage = new DummyMessage();
24+
25+
$sentMessage = new SentMessage($originalMessage, 'any');
26+
$sentMessage->setMessageId('the_id');
27+
$sentMessage->setInfo('foo', 'bar');
28+
29+
$this->assertSame($originalMessage, $sentMessage->getOriginalMessage());
30+
$this->assertSame('any', $sentMessage->getTransport());
31+
$this->assertSame('the_id', $sentMessage->getMessageId());
32+
$this->assertSame(['foo' => 'bar'], $sentMessage->getInfo());
33+
$this->assertSame('bar', $sentMessage->getInfo('foo'));
34+
$this->assertNull($sentMessage->getInfo('not_existing'));
35+
}
36+
37+
public function testSetHttpResponseInfo()
38+
{
39+
$httpResponse = $this->createMock(ResponseInterface::class);
40+
$httpResponse->method('getInfo')->willReturn(['http_code' => 200, 'http_method' => 'POST']);
41+
$httpResponse->method('toArray')->willReturn(['foo' => 'bar']);
42+
43+
$sentMessage = new SentMessage(new DummyMessage(), 'any');
44+
$sentMessage->setHttpResponseInfo($httpResponse);
45+
46+
$httpResponseInfo = $sentMessage->getInfo('http_response');
47+
$this->assertIsArray($httpResponseInfo);
48+
49+
$this->assertArrayHasKey('http_method', $httpResponseInfo);
50+
$this->assertEquals('POST', $httpResponseInfo['http_method']);
51+
52+
$this->assertArrayHasKey('body', $httpResponseInfo);
53+
$this->assertEquals(['foo' => 'bar'], $httpResponseInfo['body']);
54+
}
55+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.