Description
Symfony version(s) affected: 5.2.*
Description
The Notifier tries to read the ID after successfully sending a message to RocketChat but the array key doesn't exist.
Notice: Undefined index: message
It happens in the class Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransport in symfony/rocket-chat-notifier/RocketChatTransport which can be found here https://github.com/symfony/rocket-chat-notifier/blob/5.x/RocketChatTransport.php#L95 and for context reasons here the code:
protected function doSend(MessageInterface $message): SentMessage
{
...
$result = $response->toArray(false);
if (!$result['success']) {
throw new TransportException(sprintf('Unable to post the RocketChat message: %s.', $result['error']), $response);
}
$success = $response->toArray(false);
$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($success['message']['_id']);
return $sentMessage;
}
The success response I'm getting from RocketChat consists of only:
array (size=1)
'success' => boolean true
No message
key and therefor also no id
key.
The change was included a while ago with the commit symfony/rocket-chat-notifier@edfd293 and I'm not sure if I'm the only one having this issue since it wasn't raised since then, at least I couldn't find any issues here or on Stackoverflow regarding this topic. Maybe I'm in a special case here too since we host our RocketChat ourselves, but the array key shouldn't be accessed without checking if it exists.
Possible Solution
Either return the message directly without adding a message ID
return new SentMessage($message, (string) $this);
or at least check if that array key exists before accessing it
$sentMessage = new SentMessage($message, (string) $this);
if (array_key_exists('message', $success) && array_key_exists('_id', $success['message'])) {
$sentMessage->setMessageId($success['message']['_id']);
}
return $sentMessage;
or if it's ok to send an empty string as message ID instead of null if it doesn't exist
$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($success['message']['_id'] ?? '');
return $sentMessage;
I hope I did not forget anything important.
Best regards, Sinan