Test Failed
Pull Request — master (#494)
by
unknown
03:32
created

MailSender::send()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 28
nc 20
nop 2
dl 0
loc 42
rs 8.8497
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Modules\Mail;
6
7
use Exception;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Log\LoggerInterface;
10
use Yiisoft\Form\FormModelInterface;
11
use Yiisoft\Mailer\File;
12
use Yiisoft\Mailer\MailerInterface;
13
use Yiisoft\Mailer\MessageBodyTemplate;
14
use Yiisoft\Session\Flash\FlashInterface;
15
16
/**
17
 * MailSender sends an email from the contact form
18
 */
19
final class MailSender
20
{
21
    public function __construct(
22
        private FlashInterface $flash,
23
        private LoggerInterface $logger,
24
        private MailerInterface $mailer,
25
        private string $sender,
26
        private string $to
27
    ) {
28
        $this->mailer = $this->mailer->withTemplate(new MessageBodyTemplate(__DIR__ . '/views/'));
29
    }
30
31
    public function send(FormModelInterface $form, ServerRequestInterface $request): void
32
    {
33
        $message = $this->mailer
34
            ->compose(
35
                'contact-email',
36
                [
37
                    'content' => $form->getAttributeValue('body'),
38
                ]
39
            )
40
            ->withSubject($form->getAttributeValue('subject'))
41
            ->withFrom([$form->getAttributeValue('email') => $form->getAttributeValue('name')])
42
            ->withSender($this->sender)
43
            ->withTo($this->to);
44
45
        $attachFiles = $request->getUploadedFiles();
46
        foreach ($attachFiles as $attachFile) {
47
            foreach ($attachFile as $file) {
48
                if ($file[0]?->getError() === UPLOAD_ERR_OK) {
49
                    $message = $message->withAttached(
50
                        File::fromContent(
51
                            (string)$file->getStream(),
52
                            $file->getClientFilename(),
53
                            $file->getClientMediaType()
54
                        ),
55
                    );
56
                }
57
            }
58
        }
59
60
        try {
61
            $this->mailer->send($message);
62
            $flashMsg = 'Thank you for contacting us, we\'ll get in touch with you as soon as possible.';
63
        } catch (Exception $e) {
64
            $flashMsg = $e->getMessage();
65
            $this->logger->error($flashMsg);
66
        } finally {
67
            $this->flash->add(
68
                isset($e) ? 'danger' : 'success',
69
                [
70
                    'body' => $flashMsg,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $flashMsg does not seem to be defined for all execution paths leading up to this point.
Loading history...
71
                ],
72
                true
73
            );
74
        }
75
    }
76
}
77