Passed
Push — master ( 0828ff...caf769 )
by Alexander
08:45
created

ContactMailer::send()   B

Complexity

Conditions 6
Paths 20

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.048

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 26
c 2
b 0
f 0
nc 20
nop 2
dl 0
loc 40
ccs 16
cts 26
cp 0.6153
crap 8.048
rs 8.8817
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\Session\Flash\FlashInterface;
14
15
/**
16
 * ContactMailer sends an email from the contact form
17
 */
18
class ContactMailer
19
{
20
    private FlashInterface $flash;
21
    private LoggerInterface $logger;
22
    private MailerInterface $mailer;
23
    private string $to;
24
25 6
    public function __construct(
26
        FlashInterface $flash,
27
        LoggerInterface $logger,
28
        MailerInterface $mailer,
29
        string $to
30
    ) {
31 6
        $this->flash = $flash;
32 6
        $this->logger = $logger;
33 6
        $this->mailer = $mailer;
34 6
        $this->to = $to;
35 6
    }
36
37 2
    public function send(FormModelInterface $form, ServerRequestInterface $request)
38
    {
39 2
        $message = $this->mailer->compose(
40 2
            'contact',
41
            [
42 2
                'content' => $form->getAttributeValue('body'),
43
            ]
44
        )
45 2
            ->withSubject($form->getAttributeValue('subject'))
46 2
            ->withFrom([$form->getAttributeValue('email') => $form->getAttributeValue('name')])
47 2
            ->withTo($this->to);
48
49 2
        $attachFiles = $request->getUploadedFiles();
50 2
        foreach ($attachFiles as $attachFile) {
51
            foreach ($attachFile as $file) {
52
                if ($file->getError() === UPLOAD_ERR_OK) {
53
                    $message = $message->withAttached(
54
                        File::fromContent(
55
                            (string) $file->getStream(),
56
                            $file->getClientFilename(),
57
                            $file->getClientMediaType()
58
                        ),
59
                    );
60
                }
61
            }
62
        }
63
64
        try {
65 2
            $this->mailer->send($message);
66 2
            $flashMsg = 'Thank you for contacting us, we\'ll get in touch with you as soon as possible.';
67
        } catch (Exception $e) {
68
            $flashMsg = $e->getMessage();
69
            $this->logger->error($flashMsg);
70 2
        } finally {
71 2
            $this->flash->add(
72 2
                isset($e) ? 'danger' : 'success',
73
                [
74 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...
75
                ],
76 2
                true
77
            );
78
        }
79 2
    }
80
}
81