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

Code before:
/**
 * @author Kevin Bond <kevinbond@gmail.com>
 */
final class MailtrapApiTransport extends AbstractApiTransport
{
    private const HOST = 'send.api.mailtrap.io';
    private const HEADERS_TO_BYPASS = ['from', 'to', 'cc', 'bcc', 'subject', 'content-type', 'sender'];
    protected ?string $inboxId = null;

    public function __construct(
        #[\SensitiveParameter] private string $token,
        ?HttpClientInterface $client = null,
        ?EventDispatcherInterface $dispatcher = null,
        ?LoggerInterface $logger = null,
    ) {
        parent::__construct($client, $dispatcher, $logger);
    }

    public function __toString(): string
    {
        return \sprintf('mailtrap+api://%s', $this->getEndpoint());
    }

    public function setInboxId(?string $inboxId): static
    {
        $this->inboxId = is_numeric( $inboxId ) ? $inboxId : null;

        return $this;
    }

    protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface
    {
        $formedUri = 'https://'.$this->getEndpoint().'/api/send';
        $formedUri = $this->inboxId ? "$formedUri/{$this->inboxId}" : $formedUri;
        $response = $this->client->request('POST', $formedUri, [
            'json' => $this->getPayload($email, $envelope),
            'auth_bearer' => $this->token,
        ]);

        try {
            $statusCode = $response->getStatusCode();
            $result = $response->toArray(false);
        } catch (DecodingExceptionInterface) {
            throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).\sprintf(' (code %d).', $statusCode), $response);
        } catch (TransportExceptionInterface $e) {
            throw new HttpTransportException('Could not reach the remote Mailtrap server.', $response, 0, $e);
        }

        if (200 !== $statusCode) {
            throw new HttpTransportException(\sprintf('Unable to send email: "%s" (status code %d).', implode(', ', $result['errors']), $statusCode), $response);
        }

Review comment (suggestion):
```suggestion
    /**
     * @return $this
     */
    public function setInboxId(int $inboxId): static
    {
        $this->inboxId = $inboxId;
```

Code after:
/**
 * @author Kevin Bond <kevinbond@gmail.com>
 */
class MailtrapApiTransport extends AbstractApiTransport
{
    protected const HOST = 'send.api.mailtrap.io';
    private const HEADERS_TO_BYPASS = ['from', 'to', 'cc', 'bcc', 'subject', 'content-type', 'sender'];

    public function __construct(
        #[\SensitiveParameter] private string $token,
        ?HttpClientInterface $client = null,
        ?EventDispatcherInterface $dispatcher = null,
        ?LoggerInterface $logger = null,
    ) {
        parent::__construct($client, $dispatcher, $logger);
    }

    public function __toString(): string
    {
        return \sprintf('mailtrap+api://%s%s', $this->host ?: static::HOST, $this->port ? ':'.$this->port : '');
    }

    protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface
    {
        $response = $this->client->request('POST', 'https://'.$this->getEndpoint(), [
            'json' => $this->getPayload($email, $envelope),
            'auth_bearer' => $this->token,
        ]);

        try {
            $statusCode = $response->getStatusCode();
            $result = $response->toArray(false);
        } catch (DecodingExceptionInterface) {
            throw new HttpTransportException('Unable to send an email: '.$response->getContent(false).\sprintf(' (code %d).', $statusCode), $response);
        } catch (TransportExceptionInterface $e) {
            throw new HttpTransportException('Could not reach the remote Mailtrap server.', $response, 0, $e);
        }

        if (200 !== $statusCode) {
            throw new HttpTransportException(\sprintf('Unable to send email: "%s" (status code %d).', implode(', ', $result['errors']), $statusCode), $response);
        }

        return $response;
    }

    private function getPayload(Email $email, Envelope $envelope): array
    {
        $payload = [
            'from' => self::encodeEmail($envelope->getSender()),
            'to' => array_map(self::encodeEmail(...), $email->getTo()),
            'cc' => array_map(self::encodeEmail(...), $email->getCc()),