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

SmsReporter::notify()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 17.8

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 19
ccs 3
cts 15
cp 0.2
rs 8.8571
cc 5
eloc 10
nc 7
nop 1
crap 17.8
1
<?php
2
3
namespace Overwatch\ServiceBundle\Reporter;
4
5
use Overwatch\ResultBundle\Entity\TestResult;
6
use Overwatch\ResultBundle\Reporter\ResultReporterInterface;
7
use Overwatch\UserBundle\Entity\User;
8
use Services_Twilio;
9
10
/**
11
 * SmsReporter
12
 */
13
class SmsReporter implements ResultReporterInterface
14
{
15
    private $container;
16
    private $config;
17
    private $twilio = null;
18
19 6
    public function __construct($container, $config, Services_Twilio $twilio = null)
20
    {
21 6
        $this->container = $container;
22 6
        $this->config = $config;
23 6
        $this->twilio = $twilio;
24 6
    }
25
    
26 1
    public function notify(TestResult $result)
27
    {
28 1
        if ($this->config['enabled'] === false) {
29 1
            return;
30
        }
31
        
32
        if ($this->twilio === null) {
33
            $this->twilio = new \Services_Twilio(
34
                $this->config['twilio_account_sid'],
35
                $this->config['twilio_auth_token']
36
            );
37
        }
38
        
39
        foreach ($result->getTest()->getGroup()->getUsers() as $user) {
40
            if ($user->shouldBeAlerted($result)) {
41
                $this->sendSms($result, $user);
42
            }
43
        }
44
    }
45
    
46
    private function sendSms(TestResult $result, User $user)
47
    {
48
        if (empty($user->getTelephoneNumber())) {
49
            return;
50
        }
51
        
52
        $this->twilio->account->messages->sendMessage(
53
            $this->config['twilio_from_number'],
54
            $user->getTelephoneNumber(),
55
            $this->container->get('templating')->render(
56
                'OverwatchServiceBundle:Sms:result.txt.twig',
57
                ['result' => $result]
58
            )
59
        );
60
    }
61
}
62