Deliver   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A sendSmsPost() 0 6 1
A send() 0 13 2
A buildQueryString() 0 13 3
A throwLog() 0 5 1
A __construct() 0 3 1
A sendOtpGet() 0 8 1
1
<?php
2
namespace Sender;
3
4
use Sender\Log\Log;
5
use GuzzleHttp\Psr7;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Psr7\Request;
8
use GuzzleHttp\Psr7\Response;
9
use GuzzleHttp\Exception\ClientException;
10
11
/**
12
 * This Class for send data using GET and POST method
13
 *
14
 * @package    Sender\Deliver
15
 * @author     VenkatS <[email protected]>
16
 * @link       https://github.com/tesark/msg91-php
17
 * @license    MIT
18
 */
19
20
class Deliver
21
{
22
    protected $client;
23
    protected $logger;
24
    public function __construct()
25
    {
26
        $this->logger = new Log("Req & Res");
27
    }
28
    /**
29
     * Send POST method
30
     * @param string $uri MSG91 URI string
31
     * @param string $xml String of XML data
32
     *
33
     * @throws ClientException missing parameters or return empty
34
     * @return string MSG91 response
35
     */
36
    public function sendSmsPost($uri, $xml)
37
    {
38
        $contentType = 'text/xml; charset=UTF8';
39
        $response = $this->send('POST', $uri, $contentType, $xml);
40
        // $this->addLogFile("response", $ResponseData); //issue unable to log Response
41
        return $response;
42
    }
43
    /**
44
     * Send GET method
45
     * @param string $uri
46
     * @param array  $query
47
     *
48
     * @throws ParameterException missing parameters or return empty
49
     * @return string MSG91 response
50
     */
51
    public function sendOtpGet($uri, $query)
52
    {
53
        $paramStr = $this->buildQueryString($query);
54
        $contentType = 'application/json; charset=UTF8';
55
        $url = $uri.$paramStr;
56
        $response = $this->send('GET', $url, $contentType, null);
57
        // $this->addLogFile("response", $ResponseData); //issue unable to log Response
58
        return $response;
59
    }
60
    /**
61
     * This function send the parameters
62
     * @param string $method
63
     * @param string $uri
64
     *
65
     */
66
    protected function send($method, $uri, $contentType, $xml = null)
67
    {
68
        try {
69
            $this->logger->info(["Request:"], [$uri], [$xml]);
70
            $headers = ['Content-Type' => $contentType];
71
            $client  = new Client();
72
            $request = new Request($method, 'http://api.msg91.com/api/'.$uri, $headers, $xml);
73
            $response = $client->send($request);
74
            return $response->getBody()->getContents();
75
        } catch (ClientException $e) {
76
            $this->throwLog($e);
77
        } finally {
78
            $this->logger->deleteOldFiles();
79
        }
80
    }
81
    /**
82
     * This function for Build the query string
83
     * @param array $query
84
     *
85
     * @return string
86
     */
87
    protected function buildQueryString($query)
88
    {
89
        $paramStr = "";
90
        $flag = 1;
91
        foreach ($query as $key => $value) {
92
            if ($flag) {
93
                $paramStr .= '?'.$key.'='.urlencode(trim($value));
94
                $flag = 0;
95
            } else {
96
                $paramStr .= "&".$key.'='.urlencode(trim($value));
97
            }
98
        }
99
        return $paramStr;
100
    }
101
    /**
102
     * This function for get the network Guzzle Error
103
     *
104
     */
105
    protected function throwLog($e)
106
    {
107
        $request  = Psr7\str($e->getRequest());
0 ignored issues
show
Bug introduced by
The function str was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

107
        $request  = /** @scrutinizer ignore-call */ Psr7\str($e->getRequest());
Loading history...
108
        $response = Psr7\str($e->getResponse());
109
        $this->logger->error(["Exception:"], [$request], [$response]);
110
    }
111
}
112