Completed
Push — master ( 3ccb4d...f0ec0e )
by Vladimir
05:12
created

SwiftMailerReporter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
crap 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]>
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 SwiftMailerReporter extends AbstractReporter
24
{
25
    private $mailer;
0 ignored issues
show
Coding Style introduced by
Private member variable "mailer" must be prefixed with an underscore
Loading history...
26
    private $recipient;
0 ignored issues
show
Coding Style introduced by
Private member variable "recipient" must be prefixed with an underscore
Loading history...
27
    private $subject;
0 ignored issues
show
Coding Style introduced by
Private member variable "subject" must be prefixed with an underscore
Loading history...
28
    private $sender;
0 ignored issues
show
Coding Style introduced by
Private member variable "sender" must be prefixed with an underscore
Loading history...
29
    private $sendOnWarning;
0 ignored issues
show
Coding Style introduced by
Private member variable "sendOnWarning" must be prefixed with an underscore
Loading history...
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 7
    public function __construct(Swift_Mailer $mailer, $recipient, $sender, $subject, $sendOnWarning = true)
38
    {
39 7
        $this->mailer = $mailer;
40 7
        $this->recipient = $recipient;
41 7
        $this->sender = $sender;
42 7
        $this->subject = $subject;
43 7
        $this->sendOnWarning = $sendOnWarning;
44 7
    }
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
        if ($results->getUnknownCount() > 0) {
52 1
            $this->sendEmail($results);
53
54 1
            return;
55
        }
56
57 6
        if ($results->getWarningCount() > 0 && $this->sendOnWarning) {
58 1
            $this->sendEmail($results);
59
60 1
            return;
61
        }
62
63 5
        if ($results->getFailureCount() > 0) {
64 2
            $this->sendEmail($results);
65
66 2
            return;
67
        }
68 3
    }
69
70 4
    private function sendEmail(ResultsCollection $results)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function sendEmail()
Loading history...
Coding Style introduced by
Private method name "SwiftMailerReporter::sendEmail" must be prefixed with an underscore
Loading history...
71
    {
72 4
        $body = '';
73
74 4
        foreach ($results as $check) {
75
            /* @var $check  CheckInterface */
76
            /* @var $result ResultInterface */
77 4
            $result = $results[$check] ?? null;
78
79 4
            if ($result instanceof ResultInterface) {
80 4
                $body .= sprintf("Check: %s\n", $check->getLabel());
81 4
                $body .= sprintf("Message: %s\n\n", $result->getMessage());
82
            }
83
        }
84
85 4
        $message = Swift_Message::newInstance()
86 4
            ->setSubject($this->subject)
87 4
            ->setFrom($this->sender)
88 4
            ->setTo($this->recipient)
89 4
            ->setBody($body);
90
91 4
        $this->mailer->send($message);
92 4
    }
93
}
94