Skip to content

Commit

Permalink
[Mailer] fix PR tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PEtanguy committed Jun 8, 2023
1 parent 8baa57c commit 59e14a0
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mailer/Bridge/Brevo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ CHANGELOG
6.3
---

* Added the bridge as a replacement of the deprecated Sendiblue one.
* Added the bridge as a replacement of the deprecated Sendinblue one.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Mailer/Bridge/Brevo/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Brevo Bridge
=================
============

Provides Brevo integration for Symfony Mailer.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class BrevoApiTransportTest extends TestCase
*/
public function testToString(BrevoApiTransport $transport, string $expected)
{
$this->assertSame($expected, (string)$transport);
$this->assertSame($expected, (string) $transport);
}

public static function getTransportData()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


/*
* This file is part of the Symfony package.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public function __toString(): string

protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface
{
// TODO check this
$response = $this->client->request('POST', 'https://' . $this->getEndpoint() . '/v3/smtp/email', [
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/v3/smtp/email', [
'json' => $this->getPayload($email, $envelope),
'headers' => [
'api-key' => $this->key,
Expand All @@ -60,48 +59,51 @@ protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $e
$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);
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 Brevo server.', $response, 0, $e);
}

if (201 !== $statusCode) {
throw new HttpTransportException('Unable to send an email: ' . $result['message'] . sprintf(' (code %d).', $statusCode), $response);
throw new HttpTransportException('Unable to send an email: '.$result['message'].sprintf(' (code %d).', $statusCode), $response);
}

$sentMessage->setMessageId($result['messageId']);

return $response;
}

protected function stringifyAddresses(array $addresses): array
/**
* @return list<array{email: string, name?: string}>
*/
private function formatAddresses(array $addresses): array
{
$stringifiedAddresses = [];
$formattedAddresses = [];
foreach ($addresses as $address) {
$stringifiedAddresses[] = $this->stringifyAddress($address);
$formattedAddresses[] = $this->formatAddress($address);
}

return $stringifiedAddresses;
return $formattedAddresses;
}

private function getPayload(Email $email, Envelope $envelope): array
{
$payload = [
'sender' => $this->stringifyAddress($envelope->getSender()),
'to' => $this->stringifyAddresses($this->getRecipients($email, $envelope)),
'sender' => $this->formatAddress($envelope->getSender()),
'to' => $this->formatAddresses($this->getRecipients($email, $envelope)),
'subject' => $email->getSubject(),
];
if ($attachements = $this->prepareAttachments($email)) {
$payload['attachment'] = $attachements;
}
if ($emails = $email->getReplyTo()) {
$payload['replyTo'] = current($this->stringifyAddresses($emails));
$payload['replyTo'] = current($this->formatAddresses($emails));
}
if ($emails = $email->getCc()) {
$payload['cc'] = $this->stringifyAddresses($emails);
$payload['cc'] = $this->formatAddresses($emails);
}
if ($emails = $email->getBcc()) {
$payload['bcc'] = $this->stringifyAddresses($emails);
$payload['bcc'] = $this->formatAddresses($emails);
}
if ($email->getTextBody()) {
$payload['textContent'] = $email->getTextBody();
Expand Down Expand Up @@ -148,12 +150,12 @@ private function prepareHeadersAndTags(Headers $headers): array
continue;
}
if ($header instanceof MetadataHeader) {
$headersAndTags['headers']['X-Mailin-' . ucfirst(strtolower($header->getKey()))] = $header->getValue();
$headersAndTags['headers']['X-Mailin-'.ucfirst(strtolower($header->getKey()))] = $header->getValue();

continue;
}
if ('templateid' === $name) {
$headersAndTags[$header->getName()] = (int)$header->getValue();
$headersAndTags[$header->getName()] = (int) $header->getValue();

continue;
}
Expand All @@ -168,20 +170,19 @@ private function prepareHeadersAndTags(Headers $headers): array
return $headersAndTags;
}

private function stringifyAddress(Address $address): array
private function formatAddress(Address $address): array
{
$stringifiedAddress = ['email' => $address->getAddress()];
$formattedAddress = ['email' => $address->getAddress()];

if ($address->getName()) {
$stringifiedAddress['name'] = $address->getName();
$formattedAddress['name'] = $address->getName();
}

return $stringifiedAddress;
return $formattedAddress;
}

private function getEndpoint(): ?string
{
// TODO check this
return ($this->host ?: 'api.brevo.com') . ($this->port ? ':' . $this->port : '');
return ($this->host ?: 'api.brevo.com').($this->port ? ':'.$this->port : '');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function create(Dsn $dsn): TransportInterface
return (new BrevoApiTransport($this->getUser($dsn), $this->client, $this->dispatcher, $this->logger))
->setHost('default' === $dsn->getHost() ? null : $dsn->getHost())
->setPort($dsn->getPort())
;
;
}

return new $transport($this->getUser($dsn), $this->getPassword($dsn), $this->dispatcher, $this->logger);
Expand Down
64 changes: 32 additions & 32 deletions src/Symfony/Component/Mailer/Bridge/Brevo/composer.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
{
"name": "symfony/brevo-mailer",
"type": "symfony-mailer-bridge",
"description": "Symfony Brevo Mailer Bridge - formerly Sendinblue",
"keywords": [],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Pierre Tanguy",
"homepage": "https://github.com/petanguy"
"name": "symfony/brevo-mailer",
"type": "symfony-mailer-bridge",
"description": "Symfony Brevo Mailer Bridge",
"keywords": [],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Pierre Tanguy",
"homepage": "https://github.com/petanguy"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": ">=8.1",
"symfony/mailer": "^5.4.21|^6.2.7"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": ">=8.1",
"symfony/mailer": "^5.4.21|^6.2.7"
},
"require-dev": {
"symfony/http-client": "^5.4|^6.0"
},
"conflict": {
"symfony/mime": "<6.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Mailer\\Bridge\\Brevo\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev"
"require-dev": {
"symfony/http-client": "^5.4|^6.0"
},
"conflict": {
"symfony/mime": "<6.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Mailer\\Bridge\\Brevo\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev"
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class SendinblueTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TransportInterface
{
trigger_deprecation('symfony/sendinblue-mailer', '6.3', 'The "%s" class is deprecated, use "%s" instead.', SendinblueTransportFactory::class, BrevoTransportFactory::class);
trigger_deprecation('symfony/sendinblue-mailer', '6.3', 'The "%s" class is deprecated, use "%s" instead.', self::class, BrevoTransportFactory::class);

if (!\in_array($dsn->getScheme(), $this->getSupportedSchemes(), true)) {
throw new UnsupportedSchemeException($dsn, 'sendinblue', $this->getSupportedSchemes());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "symfony/sendinblue-mailer",
"type": "symfony-mailer-bridge",
"description": "Symfony Sendinblue Mailer Bridge - deprecated",
"description": "Symfony Sendinblue Mailer Bridge",
"keywords": [],
"homepage": "https://symfony.com",
"license": "MIT",
Expand Down

0 comments on commit 59e14a0

Please sign in to comment.