Completed
Pull Request — master (#13)
by Alexandre
13:17 queued 11:14
created

Sms::notifyOwner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
c 0
b 0
f 0
rs 9.2
cc 2
eloc 15
nc 2
nop 1
1
<?php
2
3
namespace AppBundle\Service;
4
5
use Psr\Log\LoggerInterface;
6
7
class Sms
8
{
9
    private $user;
10
    private $password;
11
12
    /**
13
     * @var LoggerInterface
14
     */
15
    private $logger;
16
17
    public function __construct($user, $password, LoggerInterface $logger = null)
18
    {
19
        $this->user = $user;
20
        $this->password = $password;
21
22
        $this->logger = $logger;
23
    }
24
25
    public function notifyOwner($message)
26
    {
27
        $url = 'https://smsapi.free-mobile.fr/sendmsg?user='
28
            .urlencode($this->user)
29
            .'&pass='.urlencode($this->password)
30
            .'&msg='.urlencode($message)
31
        ;
32
33
        $handle = curl_init($url);
34
        curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);
35
        $content = curl_exec($handle);
36
        $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
37
        curl_close($handle);
38
39
        if ($httpCode !== 200) {
40
            $this->logger->error(sprintf(
41
                "Error while sending SMS, API returned code %s with the following content:\n%s",
42
                $httpCode,
43
                $content
44
            ));
45
        }
46
    }
47
}
48