Skip to content

Commit ad833aa

Browse files
committed
refactor Email::generateBody()
1 parent 1c2ac11 commit ad833aa

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

src/Symfony/Component/Mailer/Mailer.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313

1414
use Psr\EventDispatcher\EventDispatcherInterface;
1515
use Symfony\Component\Mailer\Event\MessageEvent;
16-
use Symfony\Component\Mailer\Exception\LogicException;
1716
use Symfony\Component\Mailer\Messenger\SendEmailMessage;
1817
use Symfony\Component\Mailer\Transport\TransportInterface;
1918
use Symfony\Component\Messenger\MessageBusInterface;
20-
use Symfony\Component\Mime\Message;
2119
use Symfony\Component\Mime\RawMessage;
2220

2321
/**
@@ -38,10 +36,6 @@ public function __construct(TransportInterface $transport, MessageBusInterface $
3836

3937
public function send(RawMessage $message, Envelope $envelope = null): void
4038
{
41-
if ($message instanceof Message && '1' === $message->getHeaders()->getHeaderBody('X-Unsent')) {
42-
throw new LogicException('Cannot send messages marked as "draft".');
43-
}
44-
4539
if (null === $this->bus) {
4640
$this->transport->send($message, $envelope);
4741

src/Symfony/Component/Mailer/Tests/MailerTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,4 @@ public function dispatch($message, array $stamps = []): Envelope
6969
self::assertCount(1, $bus->messages);
7070
self::assertSame($email, $bus->messages[0]->getMessage());
7171
}
72-
73-
public function testCannotSendDraftEmails()
74-
{
75-
$transport = new Mailer($this->createMock(TransportInterface::class), $this->createMock(MessageBusInterface::class), $this->createMock(EventDispatcherInterface::class));
76-
$email = new Email();
77-
$email->getHeaders()->addTextHeader('X-Unsent', '1');
78-
79-
$this->expectException(LogicException::class);
80-
81-
$transport->send($email);
82-
}
8372
}

src/Symfony/Component/Mime/Email.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,17 +386,22 @@ public function getBody(): AbstractPart
386386

387387
public function ensureValidity()
388388
{
389-
if (null === $this->text && null === $this->html && !$this->attachments) {
390-
throw new LogicException('A message must have a text or an HTML part or attachments.');
391-
}
389+
$this->ensureBodyValid();
392390

393391
if ('1' === $this->getHeaders()->getHeaderBody('X-Unsent')) {
394-
return;
392+
throw new LogicException('Cannot send messages marked as "draft".');
395393
}
396394

397395
parent::ensureValidity();
398396
}
399397

398+
private function ensureBodyValid(): void
399+
{
400+
if (null === $this->text && null === $this->html && !$this->attachments) {
401+
throw new LogicException('A message must have a text or an HTML part or attachments.');
402+
}
403+
}
404+
400405
/**
401406
* Generates an AbstractPart based on the raw body of a message.
402407
*
@@ -419,7 +424,7 @@ public function ensureValidity()
419424
*/
420425
private function generateBody(): AbstractPart
421426
{
422-
$this->ensureValidity();
427+
$this->ensureBodyValid();
423428

424429
[$htmlPart, $attachmentParts, $inlineParts] = $this->prepareParts();
425430

src/Symfony/Component/Mime/Tests/DraftEmailTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,17 @@ public function testMustHaveBody()
4242

4343
(new DraftEmail())->toString();
4444
}
45+
46+
public function testEnsureValidityAlwaysFails()
47+
{
48+
$email = (new DraftEmail())
49+
->to('alice@example.com')
50+
->from('webmaster@example.com')
51+
->text('some text')
52+
;
53+
54+
$this->expectException(LogicException::class);
55+
56+
$email->ensureValidity();
57+
}
4558
}

0 commit comments

Comments
 (0)