Passed
Pull Request — master (#448)
by
unknown
13:50
created

ContactMailer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 67.74%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 38
c 3
b 1
f 0
dl 0
loc 63
ccs 21
cts 31
cp 0.6774
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
B send() 0 41 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Contact;
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
 * ContactMailer sends an email from the contact form
18
 */
19
final class ContactMailer
20
{
21
    private FlashInterface $flash;
22
    private LoggerInterface $logger;
23
    private MailerInterface $mailer;
24
    private string $sender;
25
    private string $to;
26
27 6
    public function __construct(
28
        FlashInterface $flash,
29
        LoggerInterface $logger,
30
        MailerInterface $mailer,
31
        string $sender,
32
        string $to
33
    ) {
34 6
        $this->flash = $flash;
35 6
        $this->logger = $logger;
36 6
        $this->mailer = $mailer->withTemplate(new MessageBodyTemplate(__DIR__ . '/mail/'));
37 6
        $this->sender = $sender;
38 6
        $this->to = $to;
39
    }
40
41 2
    public function send(FormModelInterface $form, ServerRequestInterface $request)
42
    {
43 2
        $message = $this->mailer->compose(
44
            'contact-email',
45
            [
46 2
                'content' => $form->getAttributeValue('body'),
47
            ]
48
        )
49 2
            ->withSubject($form->getAttributeValue('subject'))
50 2
            ->withFrom([$form->getAttributeValue('email') => $form->getAttributeValue('name')])
51 2
            ->withSender($this->sender)
52 2
            ->withTo($this->to);
53
54 2
        $attachFiles = $request->getUploadedFiles();
55 2
        foreach ($attachFiles as $attachFile) {
56
            foreach ($attachFile as $file) {
57
                if ($file[0]?->getError() === UPLOAD_ERR_OK) {
58
                    $message = $message->withAttached(
59
                        File::fromContent(
60
                            (string)$file->getStream(),
61
                            $file->getClientFilename(),
62
                            $file->getClientMediaType()
63
                        ),
64
                    );
65
                }
66
            }
67
        }
68
69
        try {
70 2
            $this->mailer->send($message);
71 2
            $flashMsg = 'Thank you for contacting us, we\'ll get in touch with you as soon as possible.';
72
        } catch (Exception $e) {
73
            $flashMsg = $e->getMessage();
74
            $this->logger->error($flashMsg);
75 2
        } finally {
76 2
            $this->flash->add(
77 2
                isset($e) ? 'danger' : 'success',
78
                [
79 2
                    '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...
80
                ],
81
                true
82
            );
83
        }
84
    }
85
}
86