InfobipSmsService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 96
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setPostData() 0 6 1
A send() 0 35 3
1
<?php
2
3
namespace Pnlinh\InfobipSms;
4
5
class InfobipSmsService
6
{
7
    /** @var int retry time */
8
    public const RETRY_TIME = 3;
9
10
    /** @var string */
11
    private $username;
12
13
    /** @var string */
14
    private $password;
15
16
    /** @var string */
17
    private $postUrl = 'https://api.infobip.com/sms/1/text/single';
18
19
    /** @var array */
20
    private $header = ['Content-Type:application/json', 'Accept:application/json'];
21
22
    /** @var string */
23
    private $from;
24
25
    /**
26
     * InfobipSmsService constructor.
27
     *
28
     * @param $from
29
     * @param $username
30
     * @param $password
31
     */
32
    public function __construct($from, $username, $password)
33
    {
34
        $this->from = $from;
35
        $this->username = $username;
36
        $this->password = $password;
37
    }
38
39
    /**
40
     * Set post data.
41
     *
42
     * @param $to
43
     * @param $text
44
     *
45
     * @return array
46
     */
47
    private function setPostData($to, $text)
48
    {
49
        return [
50
            'from' => $this->from,
51
            'to'   => (array) $to,
52
            'text' => $text,
53
        ];
54
    }
55
56
    /**
57
     * Do send sms.
58
     *
59
     * @see https://www.infobip.com/en/blog/step-by-step-sms-api-php-tutorial-create-your-new-web-app
60
     *
61
     * @param $to
62
     * @param $text
63
     *
64
     * @return array
65
     */
66
    public function send($to, $text)
67
    {
68
        $postDataJson = json_encode($this->setPostData($to, $text));
69
70
        // Set retry time if response is null or empty
71
        $retrytime = 0;
72
        retry_from_here:
73
74
        $ch = curl_init();
75
        // Setting options
76
        curl_setopt($ch, CURLOPT_URL, $this->postUrl);
77
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header);
78
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
79
        curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password);
80
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
81
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
82
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
83
        curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
84
        curl_setopt($ch, CURLOPT_POST, 1);
85
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
86
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataJson);
87
88
        // Response of the POST request
89
        $response = curl_exec($ch);
90
91
        if ($response === '' && $retrytime <= static::RETRY_TIME) {
92
            $retrytime++;
93
            goto retry_from_here;
94
        }
95
96
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
97
        $responseBody = json_decode($response);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
        $responseBody = json_decode(/** @scrutinizer ignore-type */ $response);
Loading history...
98
        curl_close($ch);
99
100
        return [$httpCode, $responseBody];
101
    }
102
}
103