Skip to content

Commit 84060cb

Browse files
committed
Some improvements
1 parent 6d3915a commit 84060cb

6 files changed

+92
-143
lines changed

Mailer/Factory/SparkpostTransportFactory.php

-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
use Symfony\Component\Mailer\Transport\Dsn;
1414
use Symfony\Component\Mailer\Transport\TransportInterface;
1515
use Symfony\Contracts\HttpClient\HttpClientInterface;
16-
use Symfony\Contracts\Translation\TranslatorInterface;
1716

1817
class SparkpostTransportFactory extends AbstractTransportFactory
1918
{
2019
public function __construct(
21-
private TranslatorInterface $translator,
2220
EventDispatcherInterface $eventDispatcher,
2321
private TransportCallback $transportCallback,
2422
HttpClientInterface $client = null,
@@ -41,7 +39,6 @@ public function create(Dsn $dsn): TransportInterface
4139
return new SparkpostTransport(
4240
$this->getPassword($dsn),
4341
$dsn->getOption('region'),
44-
$this->translator,
4542
$this->transportCallback,
4643
$this->client,
4744
$this->dispatcher,

Mailer/Transport/SparkpostTransport.php

+31-17
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
3030
use Symfony\Contracts\HttpClient\HttpClientInterface;
3131
use Symfony\Contracts\HttpClient\ResponseInterface;
32-
use Symfony\Contracts\Translation\TranslatorInterface;
3332

3433
class SparkpostTransport extends AbstractApiTransport implements TokenTransportInterface
3534
{
@@ -55,8 +54,7 @@ class SparkpostTransport extends AbstractApiTransport implements TokenTransportI
5554

5655
public function __construct(
5756
private string $apiKey,
58-
private string $region,
59-
private TranslatorInterface $translator,
57+
string $region,
6058
private TransportCallback $callback,
6159
HttpClientInterface $client = null,
6260
EventDispatcherInterface $dispatcher = null,
@@ -81,30 +79,33 @@ public function __toString(): string
8179
protected function doSendApi(SentMessage $sentMessage, Email $email, Envelope $envelope): ResponseInterface
8280
{
8381
try {
84-
$payload = $this->getSparkPostPayload($sentMessage);
82+
$payload = $this->getSparkpostPayload($sentMessage);
8583
$this->checkTemplateIsValid($payload);
86-
$response = $this->getSparkPostResponse('transmissions', $payload);
84+
$response = $this->getSparkpostResponse('transmissions', $payload);
8785
$this->handleError($response);
8886

8987
if ($errorMessage = $this->getErrorMessageFromResponseBody($response->toArray())) {
9088
/** @var MauticMessage $message */
9189
$message = $sentMessage->getOriginalMessage();
9290
$this->processImmediateSendFeedback($payload, $response->toArray(), $message->getMetadata());
93-
$this->throwException($errorMessage);
91+
throw new TransportException($errorMessage);
9492
}
9593

9694
return $response;
9795
} catch (\Exception $e) {
98-
$this->throwException($e->getMessage());
96+
throw new TransportException($e->getMessage());
9997
}
10098
}
10199

102-
private function getSparkPostPayload(SentMessage $message): array
100+
/**
101+
* @return array<mixed>
102+
*/
103+
private function getSparkpostPayload(SentMessage $message): array
103104
{
104105
$email = $message->getOriginalMessage();
105106

106107
if (!$email instanceof MauticMessage) {
107-
$this->throwException('Message must be an instance of '.MauticMessage::class);
108+
throw new TransportException('Message must be an instance of '.MauticMessage::class);
108109
}
109110

110111
$metadata = $email->getMetadata();
@@ -147,11 +148,9 @@ private function getSparkPostPayload(SentMessage $message): array
147148
];
148149
}
149150

150-
private function throwException(string $message): void
151-
{
152-
throw new TransportException($message);
153-
}
154-
151+
/**
152+
* @return array<mixed>
153+
*/
155154
private function buildContent(MauticMessage $message): array
156155
{
157156
$fromAddress = current($message->getFrom());
@@ -169,6 +168,9 @@ private function buildContent(MauticMessage $message): array
169168
];
170169
}
171170

171+
/**
172+
* @return array<mixed>
173+
*/
172174
private function buildHeaders(MauticMessage $message): array
173175
{
174176
$result = [];
@@ -183,6 +185,9 @@ private function buildHeaders(MauticMessage $message): array
183185
return $result;
184186
}
185187

188+
/**
189+
* @return array<mixed>
190+
*/
186191
private function buildAttachments(MauticMessage $message): array
187192
{
188193
$result = [];
@@ -203,6 +208,9 @@ private function buildAttachments(MauticMessage $message): array
203208
return $result;
204209
}
205210

211+
/**
212+
* @return array<mixed>
213+
*/
206214
private function buildRecipients(MauticMessage $message, array $metadata, array $mergeVars): array
207215
{
208216
$recipients = [];
@@ -226,6 +234,9 @@ private function buildRecipients(MauticMessage $message, array $metadata, array
226234
return $recipients;
227235
}
228236

237+
/**
238+
* @return array<mixed>
239+
*/
229240
private function buildRecipient(Address $to, array $metadata, array $mergeVars): array
230241
{
231242
$recipient = [
@@ -265,6 +276,9 @@ private function buildRecipient(Address $to, array $metadata, array $mergeVars):
265276
return $recipient;
266277
}
267278

279+
/**
280+
* @return array<mixed>
281+
*/
268282
private function buildCopyRecipient(Address $to, Address $copy, array $recipient): array
269283
{
270284
$copyRecipient = [
@@ -317,7 +331,7 @@ private function checkTemplateIsValid(array $payload): void
317331
unset($payload['recipients']);
318332
}
319333

320-
$response = $this->getSparkPostResponse('utils/content-previewer', $payload);
334+
$response = $this->getSparkpostResponse('utils/content-previewer', $payload);
321335

322336
if (403 === $response->getStatusCode()) {
323337
// We cannot fail as it would be a BC break. Throw a warning and continue.
@@ -333,7 +347,7 @@ private function checkTemplateIsValid(array $payload): void
333347
/**
334348
* @throws TransportExceptionInterface
335349
*/
336-
private function getSparkPostResponse(
350+
private function getSparkpostResponse(
337351
string $endpoint,
338352
array $payload,
339353
string $method = Request::METHOD_POST
@@ -364,7 +378,7 @@ private function handleError(ResponseInterface $response): void
364378
}
365379

366380
$data = json_decode($response->getContent(false), true);
367-
$this->getLogger()->error('SparkPostApiTransport error response', $data);
381+
$this->getLogger()->error('SparkpostApiTransport error response', $data);
368382

369383
throw new HttpTransportException(json_encode($data['errors']), $response, $response->getStatusCode());
370384
}

Tests/Unit/Mailer/Factory/SparkpostClientFactoryTest.php

-26
This file was deleted.

Tests/Unit/Mailer/Factory/SparkpostTransportFactoryTest.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace MauticPlugin\SparkpostBundle\Tests\Unit\Mailer\Factory;
66

77
use Mautic\EmailBundle\Model\TransportCallback;
8-
use MauticPlugin\SparkpostBundle\Mailer\Factory\SparkpostClientFactory;
98
use MauticPlugin\SparkpostBundle\Mailer\Factory\SparkpostTransportFactory;
109
use MauticPlugin\SparkpostBundle\Mailer\Transport\SparkpostTransport;
1110
use PHPUnit\Framework\Assert;
@@ -19,18 +18,18 @@
1918

2019
class SparkpostTransportFactoryTest extends TestCase
2120
{
21+
private SparkpostTransportFactory $sparkpostTransportFactory;
22+
2223
protected function setUp(): void
2324
{
2425
$translatorMock = $this->createMock(TranslatorInterface::class);
25-
$sparkpostClientFactoryMock = $this->createMock(SparkpostClientFactory::class);
2626
$eventDispatcherMock = $this->createMock(EventDispatcherInterface::class);
2727
$transportCallbackMock = $this->createMock(TransportCallback::class);
2828
$httpClientMock = $this->createMock(HttpClientInterface::class);
2929
$loggerMock = $this->createMock(LoggerInterface::class);
3030

3131
$this->sparkpostTransportFactory = new SparkpostTransportFactory(
3232
$translatorMock,
33-
$sparkpostClientFactoryMock,
3433
$eventDispatcherMock,
3534
$transportCallbackMock,
3635
$httpClientMock,
@@ -56,7 +55,7 @@ public function testUnsupportedScheme(): void
5655
{
5756
$this->expectException(UnsupportedSchemeException::class);
5857
$dsn = new Dsn(
59-
'mautic+sparkpost',
58+
'some+unsupported+scheme',
6059
'host',
6160
null,
6261
'sparkpost_api_key',

0 commit comments

Comments
 (0)