Skip to content

Commit 8d20e10

Browse files
committed
Merge branch 'sw-26672/fix-mail-log-entry-bug' into '5.7'
SW-26672 - Fix mail log contact reference loading See merge request shopware/5/product/shopware!801
2 parents 334db6c + e56b257 commit 8d20e10

File tree

3 files changed

+46
-43
lines changed

3 files changed

+46
-43
lines changed

.phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7895,16 +7895,6 @@ parameters:
78957895
count: 1
78967896
path: engine/Shopware/Bundle/MailBundle/Service/LogEntryBuilder.php
78977897

7898-
-
7899-
message: "#^PHPDoc tag @var for variable \\$collection contains generic class Doctrine\\\\Common\\\\Collections\\\\ArrayCollection but does not specify its types\\: TKey, T$#"
7900-
count: 1
7901-
path: engine/Shopware/Bundle/MailBundle/Service/LogEntryBuilder.php
7902-
7903-
-
7904-
message: "#^Parameter \\#1 \\$subject of method Shopware\\\\Models\\\\Mail\\\\Log\\:\\:setSubject\\(\\) expects string\\|null, string\\|false given\\.$#"
7905-
count: 1
7906-
path: engine/Shopware/Bundle/MailBundle/Service/LogEntryBuilder.php
7907-
79087898
-
79097899
message: "#^Parameter \\#1 \\$body of method Zend_Mail\\:\\:createAttachment\\(\\) expects string, string\\|false given\\.$#"
79107900
count: 2

engine/Shopware/Bundle/MailBundle/Service/LogEntryBuilder.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* Shopware 5
46
* Copyright (c) shopware AG
@@ -48,20 +50,11 @@ class LogEntryBuilder implements LogEntryBuilderInterface
4850
public const SHOP_ASSOCIATION = 'shop';
4951
public const SHOP_ID_ASSOCIATION = 'shopId';
5052

51-
/**
52-
* @var EntityManagerInterface
53-
*/
54-
private $entityManager;
53+
private EntityManagerInterface $entityManager;
5554

56-
/**
57-
* @var MailRepository
58-
*/
59-
private $mailRepository;
55+
private MailRepository $mailRepository;
6056

61-
/**
62-
* @var OrderRepository
63-
*/
64-
private $orderRepository;
57+
private OrderRepository $orderRepository;
6558

6659
public function __construct(EntityManagerInterface $entityManager)
6760
{
@@ -77,9 +70,9 @@ public function build(Enlight_Components_Mail $mail): Log
7770
{
7871
$logEntry = new Log();
7972

80-
$logEntry->setSubject(iconv_mime_decode($mail->getSubject()));
73+
$logEntry->setSubject((string) iconv_mime_decode($mail->getSubject()));
8174
$logEntry->setSender($mail->getFrom());
82-
$logEntry->setSentAt(new DateTime($mail->getDate()));
75+
$logEntry->setSentAt(new DateTime((string) $mail->getDate()));
8376
$logEntry->setContentText($mail->getPlainBodyText());
8477

8578
if ($mail->getBodyHtml() instanceof Zend_Mime_Part) {
@@ -111,7 +104,6 @@ protected function assignType(Log $logEntry, ?string $templateName): void
111104

112105
protected function assignOrder(Log $logEntry, Enlight_Components_Mail $mail): void
113106
{
114-
/** @var Order|null $order */
115107
$order = $this->resolveOrderByAssociation($mail);
116108

117109
if ($order !== null) {
@@ -120,7 +112,6 @@ protected function assignOrder(Log $logEntry, Enlight_Components_Mail $mail): vo
120112
return;
121113
}
122114

123-
/** @var Order|null $order */
124115
$order = $this->resolveOrderByType($logEntry->getType());
125116

126117
if ($order !== null) {
@@ -137,7 +128,6 @@ protected function assignShop(Log $logEntry, Enlight_Components_Mail $mail): voi
137128
}
138129

139130
if ($mail->getAssociation(self::SHOP_ID_ASSOCIATION) !== null) {
140-
/** @var Shop $shop */
141131
$shop = $this->entityManager->getPartialReference(
142132
Shop::class,
143133
$mail->getAssociation(self::SHOP_ID_ASSOCIATION)
@@ -227,10 +217,13 @@ protected function assignRecipients(Log $logEntry, array $recipients = []): void
227217
foreach ($knownRecipients as $recipient) {
228218
unset($unknownRecipients[$recipient['mail_address']]);
229219

230-
$associatedContacts[] = $this->entityManager->getPartialReference(
220+
$contact = $this->entityManager->getReference(
231221
Contact::class,
232222
$recipient['id']
233223
);
224+
if ($contact instanceof Contact) {
225+
$associatedContacts[] = $contact;
226+
}
234227
}
235228

236229
foreach (array_keys($unknownRecipients) as $recipient) {
@@ -239,13 +232,15 @@ protected function assignRecipients(Log $logEntry, array $recipients = []): void
239232

240233
$this->persistContact($contact);
241234

242-
$associatedContacts[] = $this->entityManager->getPartialReference(
235+
$fetchedContact = $this->entityManager->getReference(
243236
Contact::class,
244237
$contact->getId()
245238
);
239+
if ($fetchedContact instanceof Contact) {
240+
$associatedContacts[] = $fetchedContact;
241+
}
246242
}
247243

248-
/** @var ArrayCollection<Contact> $collection */
249244
$collection = new ArrayCollection($associatedContacts);
250245
$logEntry->setRecipients($collection);
251246
}
@@ -275,8 +270,7 @@ protected function assignDocuments(Log $logEntry, Enlight_Components_Mail $mail)
275270
return;
276271
}
277272

278-
$orderRepository = $this->entityManager->getRepository(Order::class);
279-
$documents = $orderRepository->getDocuments([$logEntry->getOrder()->getId()]);
273+
$documents = $this->entityManager->getRepository(Order::class)->getDocuments([$logEntry->getOrder()->getId()]);
280274
$filenameIdMap = [];
281275

282276
foreach ($documents as $document) {
@@ -287,15 +281,15 @@ protected function assignDocuments(Log $logEntry, Enlight_Components_Mail $mail)
287281
}
288282
}
289283

290-
/** @var Zend_Mime_Part $part */
291284
foreach ($mail->getParts() as $part) {
292285
if (isset($filenameIdMap[$part->filename])) {
293-
/** @var Document $document */
294286
$document = $this->entityManager->getPartialReference(
295287
Document::class,
296288
$filenameIdMap[$part->filename]
297289
);
298-
$logEntry->addDocument($document);
290+
if ($document instanceof Document) {
291+
$logEntry->addDocument($document);
292+
}
299293
}
300294
}
301295
}

tests/Functional/Bundle/MailBundle/LogEntryBuilderTest.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,45 @@ class LogEntryBuilderTest extends TestCase
4040
use DatabaseTransactionBehaviour;
4141
use MailBundleTestTrait;
4242

43+
private const TEST_MAIL = '[email protected]';
44+
4345
private LogEntryBuilderInterface $builder;
4446

47+
private ModelManager $modelManager;
48+
4549
protected function setUp(): void
4650
{
4751
parent::setUp();
4852

49-
$entityManager = $this->getContainer()->get(ModelManager::class);
50-
$this->builder = new LogEntryBuilder($entityManager);
53+
$this->modelManager = $this->getContainer()->get(ModelManager::class);
54+
$this->builder = new LogEntryBuilder($this->modelManager);
5155
}
5256

5357
public function testBuildWithSimpleMail(): void
5458
{
5559
$mail = $this->createSimpleMail();
56-
$entry = $this->builder->build($mail);
60+
$log = $this->builder->build($mail);
61+
62+
static::assertSame($mail->getSubject(), $log->getSubject());
63+
static::assertSame($mail->getFrom(), $log->getSender());
64+
static::assertInstanceOf(Contact::class, $log->getRecipients()->first());
65+
static::assertSame($mail->getPlainBodyText(), $log->getContentText());
66+
}
5767

58-
static::assertNotNull($entry);
68+
public function testBuildWithDetachedRecipient(): void
69+
{
70+
$contact = new Contact();
71+
$contact->setMailAddress(self::TEST_MAIL);
72+
$this->modelManager->persist($contact);
73+
$this->modelManager->flush($contact);
74+
$this->modelManager->detach($contact);
75+
76+
$mail = $this->createSimpleMail();
77+
$mail->clearRecipients();
78+
$mail->addTo(self::TEST_MAIL);
5979

60-
static::assertSame($mail->getSubject(), $entry->getSubject());
61-
static::assertSame($mail->getFrom(), $entry->getSender());
62-
static::assertInstanceOf(Contact::class, $entry->getRecipients()->first());
63-
static::assertSame($mail->getPlainBodyText(), $entry->getContentText());
80+
$recipient = $this->builder->build($mail)->getRecipients()->first();
81+
static::assertInstanceOf(Contact::class, $recipient);
82+
static::assertSame(self::TEST_MAIL, $recipient->getMailAddress());
6483
}
6584
}

0 commit comments

Comments
 (0)