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

ContactMailer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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