EmailAlerter::setRecipient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Innmind\ProvisionerBundle\Alert;
4
5
use Swift_Mailer;
6
use Swift_Message;
7
8
/**
9
 * Send a mail when a provision alert is raised
10
 */
11
class EmailAlerter implements AlerterInterface
12
{
13
    protected $mailer;
14
    protected $host;
15
    protected $recipient;
16
17
    /**
18
     * Set the swift mailer
19
     *
20
     * @param Swift_Mailer $mailer
21
     */
22 9
    public function setMailer(Swift_Mailer $mailer)
23
    {
24 9
        $this->mailer = $mailer;
25 9
    }
26
27
    /**
28
     * Set host name used to build 'From' email
29
     *
30
     * @param string $host
31
     */
32 9
    public function setHost($host)
33
    {
34 9
        $this->host = (string) $host;
35 9
    }
36
37
    /**
38
     * Set the emails recipient
39
     *
40
     * @param string $recipient
41
     */
42
    public function setRecipient($recipient)
43
    {
44
        $this->recipient = (string) $recipient;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 9
    public function alert(Alert $alert)
51
    {
52 9
        switch (true) {
53 9
            case $alert->isUnderUsed():
54 3
                $message = Swift_Message::newInstance()
55 3
                    ->setSubject('[Provision alert] Server under used')
56 3
                    ->setFrom(sprintf(
57 3
                        'provisioner@%s',
58 3
                        $this->host
59 3
                    ))
60 3
                    ->setTo($this->recipient)
61 3
                    ->setBody(
62 3
                        'Command: '.$alert->getCommandName()."\n".
63 3
                        'Command input: '.(string) $alert->getCommandInput()."\n".
64 3
                        'CPU usage: '.$alert->getCpuUsage()."\n".
65 3
                        'Load average: '.$alert->getLoadAverage()."\n"
66 3
                    );
67 3
                break;
68 6
            case $alert->isOverUsed():
69 3
                $message = Swift_Message::newInstance()
70 3
                    ->setSubject('[Provision alert] Server over used')
71 3
                    ->setFrom('[email protected]')
72 3
                    ->setTo($this->recipient)
73 3
                    ->setBody(
74 3
                        'Command: '.$alert->getCommandName()."\n".
75 3
                        'Command input: '.(string) $alert->getCommandInput()."\n".
76 3
                        'CPU usage: '.$alert->getCpuUsage()."\n".
77 3
                        'Load average: '.$alert->getLoadAverage()."\n".
78 3
                        'Processes required: '.$alert->getLeftOver()."\n",
79 3
                        'Processes running: '.$alert->getRunningProcesses()."\n"
80 3
                    );
81 3
                break;
82
        }
83
84 9
        if (isset($message)) {
85 6
            $this->mailer->send($message);
86 6
        }
87 9
    }
88
}
89