EmailChannel::generateSubject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chemaclass\StockTicker\Domain\Notifier\Channel\Email;
6
7
use Chemaclass\StockTicker\Domain\Notifier\Channel\TemplateGeneratorInterface;
8
use Chemaclass\StockTicker\Domain\Notifier\ChannelInterface;
9
use Chemaclass\StockTicker\Domain\Notifier\NotifyResult;
10
use Symfony\Component\Mailer\MailerInterface;
11
use Symfony\Component\Mime\Email;
12
13
final class EmailChannel implements ChannelInterface
14
{
15
    private const NO_REPLY_EMAIL = '[email protected]';
16
17
    private string $toAddress;
18
19
    private MailerInterface $mailer;
20
21
    private TemplateGeneratorInterface $templateGenerator;
22
23 1
    public function __construct(
24
        string $toAddress,
25
        MailerInterface $mailer,
26
        TemplateGeneratorInterface $templateGenerator,
27
    ) {
28 1
        $this->toAddress = $toAddress;
29 1
        $this->mailer = $mailer;
30 1
        $this->templateGenerator = $templateGenerator;
31 1
    }
32
33 1
    public function send(NotifyResult $notifyResult): void
34
    {
35 1
        $email = (new Email())
36 1
            ->to($this->toAddress)
37 1
            ->from(self::NO_REPLY_EMAIL)
38 1
            ->subject($this->generateSubject($notifyResult))
39 1
            ->html($this->templateGenerator->generateHtml($notifyResult));
40
41 1
        $this->mailer->send($email);
42 1
    }
43
44 1
    private function generateSubject(NotifyResult $notifyResult): string
45
    {
46 1
        $symbols = implode(', ', array_values($notifyResult->symbols()));
47
48 1
        return "StockTicker NEWS for {$symbols}";
49
    }
50
}
51