Passed
Push — master ( b68383...74043c )
by Alexander
05:41
created

ContactMailer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 70.97%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 59
ccs 22
cts 31
cp 0.7097
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B send() 0 40 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\MailerInterface;
12
use Yiisoft\Session\Flash\FlashInterface;
13
14
/**
15
 * ContactMailer sends an email from the contact form
16
 */
17
class ContactMailer
18
{
19
    private FlashInterface $flash;
20
    private LoggerInterface $logger;
21
    private MailerInterface $mailer;
22
    private string $to;
23
24 6
    public function __construct(
25
        FlashInterface $flash,
26
        LoggerInterface $logger,
27
        MailerInterface $mailer,
28
        string $to
29
    ) {
30 6
        $this->flash = $flash;
31 6
        $this->logger = $logger;
32 6
        $this->mailer = $mailer;
33 6
        $this->to = $to;
34 6
    }
35
36 2
    public function send(FormModelInterface $form, ServerRequestInterface $request)
37
    {
38 2
        $message = $this->mailer->compose(
39 2
            'contact',
40
            [
41 2
                'content' => $form->getAttributeValue('body'),
42
            ]
43
        )
44 2
            ->setSubject($form->getAttributeValue('subject'))
45 2
            ->setFrom([$form->getAttributeValue('email') => $form->getAttributeValue('name')])
46 2
            ->setTo($this->to);
47
48 2
        $attachFiles = $request->getUploadedFiles();
49 2
        foreach ($attachFiles as $attachFile) {
50
            foreach ($attachFile as $file) {
51
                if ($file->getError() === UPLOAD_ERR_OK) {
52
                    $message->attachContent(
53
                        (string) $file->getStream(),
54
                        [
55
                            'fileName' => $file->getClientFilename(),
56
                            'contentType' => $file->getClientMediaType(),
57
                        ]
58
                    );
59
                }
60
            }
61
        }
62
63
        try {
64 2
            $message->send();
65 2
            $flashMsg = 'Thank you for contacting us, we\'ll get in touch with you as soon as possible.';
66
        } catch (Exception $e) {
67
            $flashMsg = $e->getMessage();
68
            $this->logger->error($flashMsg);
69 2
        } finally {
70 2
            $this->flash->add(
71 2
                isset($e) ? 'danger' : 'success',
72
                [
73 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...
74
                ],
75 2
                true
76
            );
77
        }
78 2
    }
79
}
80