ContactMailer::send()   B
last analyzed

Complexity

Conditions 6
Paths 20

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 27
nc 20
nop 1
dl 0
loc 41
rs 8.8657
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Contact;
6
7
use Exception;
8
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Yiisoft\Mailer\File;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Mailer\File was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Yiisoft\Mailer\MailerInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Mailer\MailerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Yiisoft\Mailer\MessageBodyTemplate;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Mailer\MessageBodyTemplate was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Yiisoft\Session\Flash\FlashInterface;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Session\Flash\FlashInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
/**
15
 * ContactMailer sends an email from the contact form.
16
 */
17
final class ContactMailer
18
{
19
    public function __construct(
20
        private FlashInterface $flash,
21
        private LoggerInterface $logger,
22
        private MailerInterface $mailer,
23
        private string $sender,
24
        private string $to
25
    ) {
26
        $this->mailer = $this->mailer->withTemplate(new MessageBodyTemplate(__DIR__ . '/mail/'));
27
    }
28
29
    public function send(ContactForm $form): void
30
    {
31
        $message = $this->mailer
32
            ->compose(
33
                'contact-email',
34
                [
35
                    'content' => $form->getPropertyValue('body'),
36
                ]
37
            )
38
            ->withSubject($form->getPropertyValue('subject'))
39
            ->withFrom([$form->getPropertyValue('email') => $form->getPropertyValue('name')])
40
            ->withSender($this->sender)
41
            ->withTo($this->to);
42
43
        foreach ($form->getPropertyValue('attachFiles') as $attachFile) {
44
            foreach ($attachFile as $file) {
45
                if ($file[0]?->getError() === UPLOAD_ERR_OK) {
46
                    $message = $message->withAttached(
47
                        File::fromContent(
48
                            (string) $file->getStream(),
49
                            $file->getClientFilename(),
50
                            $file->getClientMediaType()
51
                        ),
52
                    );
53
                }
54
            }
55
        }
56
57
        try {
58
            $this->mailer->send($message);
59
            $flashMsg = 'Thank you for contacting us, we\'ll get in touch with you as soon as possible.';
60
        } catch (Exception $e) {
61
            $flashMsg = $e->getMessage();
62
            $this->logger->error($flashMsg);
63
        } finally {
64
            $this->flash->add(
65
                isset($e) ? 'danger' : 'success',
66
                [
67
                    '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...
68
                ],
69
                true
70
            );
71
        }
72
    }
73
}
74