Failed Conditions
Pull Request — master (#142)
by Zac
03:56
created

EmailReporter::notify()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 16
ccs 7
cts 11
cp 0.6364
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 4.7691
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