1 | <?php |
||
8 | use Netgen\InformationCollection\API\Value\DataTransfer\EmailContent; |
||
9 | use Netgen\InformationCollection\API\Mailer\MailerInterface; |
||
10 | |||
11 | class Mailer implements MailerInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var \Swift_Mailer |
||
15 | */ |
||
16 | protected $internalMailer; |
||
17 | |||
18 | /** |
||
19 | * Mailer constructor. |
||
20 | * |
||
21 | * @param \Swift_Mailer $internalMailer |
||
22 | */ |
||
23 | public function __construct(\Swift_Mailer $internalMailer) |
||
27 | |||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | public function createAndSendMessage(EmailContent $data): void |
||
32 | { |
||
33 | $message = new \Swift_Message(); |
||
34 | |||
35 | try { |
||
36 | $message->setTo($data->getRecipient()); |
||
37 | } catch (\Swift_RfcComplianceException $e) { |
||
38 | throw new EmailNotSentException('recipient', $e->getMessage()); |
||
39 | } |
||
40 | |||
41 | try { |
||
42 | $message->setFrom($data->getSender()); |
||
43 | } catch (\Swift_RfcComplianceException $e) { |
||
44 | throw new EmailNotSentException('sender', $e->getMessage()); |
||
45 | } |
||
46 | |||
47 | $message->setSubject($data->getSubject()); |
||
48 | $message->setBody($data->getBody(), 'text/html'); |
||
49 | |||
50 | if ($data->hasAttachments()) { |
||
51 | foreach ($data->getAttachments() as $attachment) { |
||
|
|||
52 | $message->attach( |
||
53 | \Swift_Attachment::fromPath($attachment->inputUri, $attachment->mimeType) |
||
54 | ->setFilename($attachment->fileName) |
||
55 | ); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | if (!$this->internalMailer->send($message)) { |
||
60 | throw new EmailNotSentException('send', 'invalid mailer configuration?'); |
||
61 | } |
||
64 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.