Mailer   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 33
dl 0
loc 71
c 0
b 0
f 0
ccs 32
cts 32
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sendEmail() 0 22 3
A onFinish() 0 20 5
A __construct() 0 7 1
1
<?php
2
3
/**
4
 * This file is part of the `tvi/monitor-bundle` project.
5
 *
6
 * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
11
12
namespace Tvi\MonitorBundle\Reporter;
13
14
use Swift_Mailer;
15
use Swift_Message;
16
use ZendDiagnostics\Check\CheckInterface;
17
use ZendDiagnostics\Result\Collection as ResultsCollection;
18
use ZendDiagnostics\Result\ResultInterface;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @author louis <[email protected]>, Vladimir Turnaev <[email protected]>
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
22
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
23
class Mailer extends ReporterAbstract
24
{
25
    protected $mailer;
26
    protected $recipient;
27
    protected $subject;
28
    protected $sender;
29
    protected $sendOnWarning;
30
31
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $mailer should have a doc-comment as per coding-style.
Loading history...
32
     * @param string $recipient
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $recipient does not match actual variable name $mailer
Loading history...
33
     * @param string $sender
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $sender does not match actual variable name $recipient
Loading history...
34
     * @param string $subject
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $subject does not match actual variable name $sender
Loading history...
35
     * @param bool   $sendOnWarning
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $sendOnWarning does not match actual variable name $subject
Loading history...
36
     */
37 8
    public function __construct(Swift_Mailer $mailer, $recipient, $sender, $subject, $sendOnWarning = true)
38
    {
39 8
        $this->mailer = $mailer;
40 8
        $this->recipient = $recipient;
41 8
        $this->sender = $sender;
42 8
        $this->subject = $subject;
43 8
        $this->sendOnWarning = $sendOnWarning;
44 8
    }
45
46
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $results should have a doc-comment as per coding-style.
Loading history...
47
     * {@inheritdoc}
48
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
49 7
    public function onFinish(ResultsCollection $results)
50
    {
51 7
        parent::onFinish($results);
52
53 7
        if ($results->getUnknownCount() > 0) {
54 1
            $this->sendEmail($results);
55
56 1
            return;
57
        }
58
59 6
        if ($results->getWarningCount() > 0 && $this->sendOnWarning) {
60 1
            $this->sendEmail($results);
61
62 1
            return;
63
        }
64
65 5
        if ($results->getFailureCount() > 0) {
66 2
            $this->sendEmail($results);
67
68 2
            return;
69
        }
70 3
    }
71
72 4
    private function sendEmail(ResultsCollection $results)
0 ignored issues
show
Coding Style introduced by
Private method name "Mailer::sendEmail" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing doc comment for function sendEmail()
Loading history...
73
    {
74 4
        $body = '';
75
76 4
        foreach ($results as $check) {
77
            /* @var $check  CheckInterface */
78
            /* @var $result ResultInterface */
79 4
            $result = $results[$check] ?? null;
80
81 4
            if ($result instanceof ResultInterface) {
82 4
                $body .= sprintf("Check: %s\n", $check->getLabel());
83 4
                $body .= sprintf("Message: %s\n\n", $result->getMessage());
84
            }
85
        }
86
87 4
        $message = (new Swift_Message())
88 4
            ->setSubject($this->subject)
89 4
            ->setFrom($this->sender)
90 4
            ->setTo($this->recipient)
91 4
            ->setBody($body);
92
93 4
        $this->mailer->send($message);
94 4
    }
95
}
96