Failed Conditions
Pull Request — master (#142)
by Zac
04:15
created

EmailReporter::sendEmail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 1

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 17
ccs 14
cts 14
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Overwatch\ServiceBundle\Reporter;
4
5
use Overwatch\ResultBundle\Entity\TestResult;
6
use Overwatch\ResultBundle\Reporter\ResultReporterInterface;
7
8
/**
9
 * EmailReporter
10
 */
11
class EmailReporter implements ResultReporterInterface
12
{
13
    private $container;
14
    private $config;
15
    
16 6
    public function __construct($container, $config)
17
    {
18 6
        $this->container = $container;
19 6
        $this->config = $config;
20 6
    }
21
    
22 1
    public function notify(TestResult $result)
23
    {
24 1
        if ($this->config['enabled'] === false) {
25
            return;
26
        }
27
        
28 1
        $recipients = [];
29
        
30 1
        foreach ($result->getTest()->getGroup()->getUsers() as $user) {
31
            if ($user->shouldBeAlerted($result)) {
32
                $recipients[] = $user->getEmail();
33
            }
34 1
        }
35
        
36 1
        $this->sendEmail($result, $recipients);
37 1
    }
38
    
39 1
    private function sendEmail(TestResult $result, array $users)
40
    {
41 1
        $message = \Swift_Message::newInstance()
42 1
            ->setSubject($result->getTest()->getName() . ' ' . $result->getStatus())
43 1
            ->setFrom($this->config['report_from'])
44 1
            ->setTo($users)
45 1
            ->setBody(
46 1
                $this->container->get('templating')->render(
47 1
                    'OverwatchServiceBundle:Email:result.txt.twig',
48 1
                    ['result' => $result]
49 1
                ),
50
                'text\plain'
51 1
            )
52 1
        ;
53
        
54 1
        $this->container->get('mailer')->send($message);
55 1
    }
56
}
57