SmsUpManager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
c 0
b 0
f 0
dl 0
loc 131
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getBalance() 0 21 3
A __construct() 0 4 1
A verifyPhone() 0 22 3
A sendMessages() 0 25 3
1
<?php
2
3
namespace SquareetLabs\LaravelSmsUp;
4
5
use GuzzleHttp\Client;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Class SmsUp
10
 * @package SquareetLabs\LaravelSmsUp
11
 */
12
class SmsUpManager
13
{
14
    /**
15
     * @const The API URL for SmsUp
16
     */
17
    const API_URI = 'https://api.gateway360.com/api/';
18
19
    /**
20
     * @const The API endpoint to send messages
21
     */
22
    const ENDPOINT_SEND = '3.0/sms/send';
23
24
    /**
25
     * @const The API endpoint to send messages with link
26
     */
27
    const ENDPOINT_SEND_LINK = '3.0/sms/send-link';
28
29
    /**
30
     * @const The API endpoint to verify phone number
31
     */
32
    const ENDPOINT_VERIFY = 'hlr/request';
33
34
    /**
35
     * @const The API endpoint to get account balance
36
     */
37
    const ENDPOINT_BALANCE = '3.0/account/get-balance';
38
39
    /**
40
     * @var Client
41
     */
42
    private $client;
43
44
    /**
45
     * @var
46
     */
47
    private $config;
48
49
    /**
50
     * SmsUp constructor.
51
     * @param array $config
52
     */
53
    public function __construct(array $config)
54
    {
55
        $this->client = new Client();
56
        $this->config = $config;
57
    }
58
59
    /**
60
     * @param array $messages
61
     * @return ResponseInterface
62
     */
63
    public function sendMessages(array $messages)
64
    {
65
        $data = [
66
            'api_key' => $this->config['key'],
67
            'report_url' => route('smsup.report'),
0 ignored issues
show
Bug introduced by
The function route 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

67
            'report_url' => /** @scrutinizer ignore-call */ route('smsup.report'),
Loading history...
68
            'concat' => 1,
69
            'fake' => $this->config['test_mode'] ? 1 : 0,
70
            'messages' => $messages
71
        ];
72
        If (!empty($messages[0]['link'])) {
73
            $endpoint = self::ENDPOINT_SEND_LINK;
74
            $data['link'] = $messages[0]['link'];
75
        } else {
76
            $endpoint = self::ENDPOINT_SEND;
77
        }
78
79
        $response = $this->client->post(self::API_URI . $endpoint, [
80
            'headers' => [
81
                'Content-Type' => 'application/json',
82
                'Accept' => 'application/json',
83
            ],
84
            'body' => \GuzzleHttp\json_encode($data),
85
        ]);
86
87
        return $response;
88
    }
89
90
    /**
91
     * @param string $phone
92
     * @return bool
93
     */
94
    public function verifyPhone($phone)
95
    {
96
        $data = [
97
            'api_key' => $this->config['key'],
98
            'msisdn' => $phone
99
        ];
100
        $response = $this->client->post(self::API_URI . self::ENDPOINT_VERIFY, [
101
            'headers' => [
102
                'Content-Type' => 'application/json',
103
                'Accept' => 'application/json',
104
            ],
105
            'body' => \GuzzleHttp\json_encode($data),
106
        ]);
107
108
        $responseArray = [];
109
        array_push($responseArray, \GuzzleHttp\json_decode($response->getBody(), true));
110
        $responseArray = $responseArray[0];
111
112
        if (isset($responseArray['status']) && $responseArray['status'] == 'ok') {
113
            return $responseArray['result']['success'];
114
        } else {
115
            return false;
116
        }
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getBalance()
123
    {
124
        $data = [
125
            'api_key' => $this->config['key']
126
        ];
127
        $response = $this->client->post(self::API_URI . self::ENDPOINT_BALANCE, [
128
            'headers' => [
129
                'Content-Type' => 'application/json',
130
                'Accept' => 'application/json',
131
            ],
132
            'body' => \GuzzleHttp\json_encode($data),
133
        ]);
134
135
        $responseArray = [];
136
        array_push($responseArray, \GuzzleHttp\json_decode($response->getBody(), true));
137
        $responseArray = $responseArray[0];
138
139
        if (isset($responseArray['status']) && $responseArray['status'] == 'ok') {
140
            return $responseArray['result']['balance'];
141
        } else {
142
            return 'error';
143
        }
144
    }
145
}