Repository: symfony/symfony
File: src/Symfony/Component/Mailer/Bridge/Sendgrid/Transport/SendgridSmtpTransport.php (PHP)

Code before:
 * @author Kevin Verschaeve
 */
class SendgridSmtpTransport extends EsmtpTransport
{
    public function __construct(#[\SensitiveParameter] string $key, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null, private ?string $region = null)
    {
        parent::__construct(null !== $region ? \sprintf('smtp.%s.sendgrid.net', $region) : 'smtp.sendgrid.net', 465, true, $dispatcher, $logger);

        $this->setUsername('apikey');
        $this->setPassword($key);
    }

    public function send(RawMessage $message, ?Envelope $envelope = null): ?SentMessage
    {
        if ($message instanceof Message) {
            $this->addSendgridHeaders($message);
        }

        return parent::send($message, $envelope);
    }

    private function addSendgridHeaders(Message $message): void
    {
        $headers = $message->getHeaders();

        if (!$headers->has('X-SMTPAPI')) {
            $suppressionHeader = null;

            foreach ($headers->all() as $header) {
                if ($header instanceof SuppressionGroupHeader) {
                    $suppressionHeader = $header;
                    break;
                }
            }

            if ($suppressionHeader) {
                $payload = [
                    'asm' => [
                        'group_id' => $suppressionHeader->getGroupId(),
                    ],
                ];

                $groupsToDisplay = $suppressionHeader->getGroupsToDisplay();
                if ($groupsToDisplay) {
                    $payload['asm']['groups_to_display'] = $groupsToDisplay;
                }

                $headers->addTextHeader('X-SMTPAPI', json_encode($payload, \JSON_UNESCAPED_SLASHES));
                $headers->remove('X-Sendgrid-SuppressionGroup');
            }
        }

Review comment (suggestion):
let's use early return statements (same below)
```suggestion
        if ($headers->has('X-SMTPAPI')) {
            return;
```

Code after:
 * @author Kevin Verschaeve
 */
class SendgridSmtpTransport extends EsmtpTransport
{
    public function __construct(#[\SensitiveParameter] string $key, ?EventDispatcherInterface $dispatcher = null, ?LoggerInterface $logger = null, private ?string $region = null)
    {
        parent::__construct(null !== $region ? \sprintf('smtp.%s.sendgrid.net', $region) : 'smtp.sendgrid.net', 465, true, $dispatcher, $logger);

        $this->setUsername('apikey');
        $this->setPassword($key);
    }

    public function send(RawMessage $message, ?Envelope $envelope = null): ?SentMessage
    {
        if ($message instanceof Message) {
            $this->addSendgridHeaders($message);
        }

        return parent::send($message, $envelope);
    }

    private function addSendgridHeaders(Message $message): void
    {
        $headers = $message->getHeaders();

        if ($headers->has('X-SMTPAPI')) {
            return;
        }

        $suppressionHeader = null;

        foreach ($headers->all() as $header) {
            if ($header instanceof SuppressionGroupHeader) {
                $suppressionHeader = $header;
                break;
            }
        }

        if ($suppressionHeader) {
            $payload = [
                'asm' => [
                    'group_id' => $suppressionHeader->getGroupId(),
                ],
            ];

            $groupsToDisplay = $suppressionHeader->getGroupsToDisplay();
            if ($groupsToDisplay) {
                $payload['asm']['groups_to_display'] = $groupsToDisplay;
            }

            $headers->addTextHeader('X-SMTPAPI', json_encode($payload, \JSON_UNESCAPED_SLASHES));