Completed
Pull Request — master (#219)
by Markus
11:05
created

SymfonyMailerReporter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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