Passed
Push — master ( e2b180...2e0dfd )
by Alexander
07:55 queued 05:11
created

ContactMailer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 82.35%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 32
ccs 14
cts 17
cp 0.8235
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 21 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Contact;
6
7
use App\Service\Mailer;
8
use App\Service\Message;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Yiisoft\Form\FormModelInterface;
11
12
/**
13
 * ContactMailer sends an email from the contact form
14
 */
15
final class ContactMailer
16
{
17
    private Mailer $mailer;
18
    private string $to;
19
20 6
    public function __construct(Mailer $mailer, string $to)
21
    {
22 6
        $this->mailer = $mailer;
23 6
        $this->to = $to;
24 6
    }
25
26 2
    public function send(FormModelInterface $form, ServerRequestInterface $request)
27
    {
28 2
        $message = new Message(
29 2
            $form->getAttributeValue('username'),
30 2
            $form->getAttributeValue('email'),
31 2
            $form->getAttributeValue('subject'),
32 2
            $form->getAttributeValue('body'),
33 2
            $this->to,
34
        );
35
36 2
        $attachFiles = $request->getUploadedFiles();
37
38 2
        foreach ($attachFiles as $attachFile) {
39
            foreach ($attachFile as $file) {
40
                if ($file->getError() === UPLOAD_ERR_OK) {
41
                    $message->addFile($file);
42
                }
43
            }
44
        }
45
46 2
        $this->mailer->send($message);
47 2
    }
48
}
49