Completed
Pull Request — master (#219)
by Kevin
01:39
created

SymfonyMailerReporter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A sendEmail() 0 23 3
1
<?php
2
3
namespace Liip\MonitorBundle\Helper;
4
5
use Symfony\Component\Mailer\MailerInterface;
6
use Symfony\Component\Mime\Email;
7
use ZendDiagnostics\Check\CheckInterface;
8
use ZendDiagnostics\Result\Collection as ResultsCollection;
9
use ZendDiagnostics\Result\ResultInterface;
10
11
class SymfonyMailerReporter extends AbstractMailReporter
12
{
13
    /**
14
     * @var MailerInterface
15
     */
16
    private $mailer;
17
18
    public function __construct(MailerInterface $mailer, array $recipients, string $sender, string $subject, bool $sendOnWarning = true)
19
    {
20
        $this->mailer = $mailer;
21
22
        parent::__construct($recipients, $sender, $subject, $sendOnWarning);
23
    }
24
25
    protected function sendEmail(ResultsCollection $results): void
26
    {
27
        $body = '';
28
29
        foreach ($results as $check) {
30
            /* @var $check  CheckInterface */
31
            /* @var $result ResultInterface */
32
            $result = $results[$check] ?? null;
33
34
            if ($result instanceof ResultInterface) {
35
                $body .= sprintf('[%s] %s', $check->getLabel(), $result->getMessage());
36
            }
37
        }
38
39
40
        $message = (new Email())
41
            ->subject($this->subject)
42
            ->from($this->sender)
43
            ->to(...$this->recipients)
44
            ->text($body);
45
46
        $this->mailer->send($message);
47
    }
48
}
49