SmsReporter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 81.25%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 49
ccs 26
cts 32
cp 0.8125
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B notify() 0 19 5
A sendSms() 0 15 2
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 132
    public function __construct($container, $config, Services_Twilio $twilio = null)
20
    {
21 132
        $this->container = $container;
22 132
        $this->config = $config;
23 132
        $this->twilio = $twilio;
24 132
    }
25
    
26 127
    public function notify(TestResult $result)
27
    {
28 127
        if ($this->config['enabled'] === false) {
29 127
            return;
30
        }
31
        
32 1
        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 1
        foreach ($result->getTest()->getGroup()->getUsers() as $user) {
40 1
            if ($user->shouldBeAlerted($result)) {
41 1
                $this->sendSms($result, $user);
42 1
            }
43 1
        }
44 1
    }
45
    
46 1
    private function sendSms(TestResult $result, User $user)
47
    {
48 1
        if (empty($user->getTelephoneNumber())) {
49
            return;
50
        }
51
        
52 1
        $this->twilio->account->messages->sendMessage(
53 1
            $this->config['twilio_from_number'],
54 1
            $user->getTelephoneNumber(),
55 1
            $this->container->get('templating')->render(
56 1
                'OverwatchServiceBundle:Sms:result.txt.twig',
57 1
                ['result' => $result]
58 1
            )
59 1
        );
60 1
    }
61
}
62