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

Code before:
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'];
    private int $inboxId = 0;

    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());
    }

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

        return $this;
    }

    protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface
    {
        $response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/send'.(0 < $this->inboxId ? '/'.$this->inboxId : ''), [
            '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;

Review comment (suggestion):
```suggestion
    public function setInboxId(int $inboxId): static
    {
        $this->inboxId = $inboxId;
```

The cast will happen automatically when calling `setInboxId()`.

Code after:
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()),
            'bcc' => array_map(self::encodeEmail(...), $email->getBcc()),
            'subject' => $email->getSubject(),
            'text' => $email->getTextBody(),