Completed
Push — master ( 7e4f78...5ca817 )
by Filippo
02:47
created

SMSFactor::contactList()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of Laravel SMSFactor.
5
 *
6
 * (c) Filippo Galante <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace IlGala\SMSFactor;
13
14
use IlGala\SMSFactor\Exceptions\SMSFactorException;
15
use IlGala\SMSFactor\Adapters\AdapterInterface;
16
17
/**
18
 * @author Filippo Galante <[email protected]>
19
 */
20
class SMSFactor
21
{
22
23
    /**
24
     * @var string
25
     */
26
    const ENDPOINT = 'https://api.smsfactor.com/';
27
28
    /**
29
     * @var string
30
     */
31
    private $endpoint;
32
33
    /**
34
     * @var string
35
     */
36
    private $content_type;
37
38
    /**
39
     * @var AdapterInterface
40
     */
41
    protected $adapter;
42
43
    /**
44
     * @param AdapterInterface $adapter
45
     */
46
    public function __construct(AdapterInterface $adapter, $content_type, $endpoint = null)
47
    {
48
        $this->adapter = $adapter;
49
        $this->content_type = strtolower($content_type);
50
        $this->endpoint = $endpoint ?: self::ENDPOINT;
51
    }
52
53
    /**
54
     * Create an account or sub account.
55
     *
56
     * @return mixed
57
     */
58 View Code Duplication
    public function createAccount($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        // Http request
61
        $response = $this->adapter->post(sprintf('%s/account', $this->endpoint), $params);
62
63
        // Result
64
        if ($this->content_type == 'application/json') {
65
            return json_decode($response);
66
        } else {
67
            return $response;
68
        }
69
    }
70
71
    /**
72
     * Get current credits.
73
     *
74
     * @return mixed
75
     */
76 View Code Duplication
    public function credits()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        // Http request
79
        $response = $this->adapter->get(sprintf('%s/credits', $this->endpoint));
80
81
        // Result
82
        if ($this->content_type == 'application/json') {
83
            return json_decode($response);
84
        } else {
85
            return $response;
86
        }
87
    }
88
89
    /**
90
     * Send or simulate sending of single or multiple SMSs.
91
     *
92
     * @return mixed
93
     */
94
    public function send($params, $method, $simulate = false)
95
    {
96
        $path = '%s/send';
0 ignored issues
show
Unused Code introduced by
$path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
97
        if (strtoupper($method) == 'POST') {
98
            // Http request
99
            $path = sprintf('%s/send', $this->endpoint);
100
            if ($simulate) {
101
                $path .= '/simulate';
102
            }
103
104
            // Http request
105
            $response = $this->adapter->post($path, $params);
106
107
            // Result
108
            if ($this->content_type == 'application/json') {
109
                return json_decode($response);
110
            } else {
111
                return $response;
112
            }
113
        } elseif (strtoupper($method) == 'GET') {
114
            // Http request
115
            $path = sprintf('%s/send?username=%s&password=%s&text=%s&to=%s', $this->endpoint, $params['username'], $params['password'], $params['text'], $params['to']);
116
117
            if (array_key_exists('delay', $params)) {
118
                if ($this->isValidDate($params['delay'])) {
119
                    $path .= sprintf('&delay=%s', $params['delay']);
120
                }
121
            }
122
123
            if (array_key_exists('sender', $params)) {
124
                $path .= sprintf('&sender=%s', $params['sender']);
125
            }
126
127
            if (array_key_exists('gsmsmsid', $params)) {
128
                $path .= sprintf('&gsmsmsid=%s', $params['gsmsmsid']);
129
            }
130
131
            // Http request
132
            $response = $this->adapter->get($path, $params);
0 ignored issues
show
Unused Code introduced by
The call to AdapterInterface::get() has too many arguments starting with $params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
133
134
            // Result
135
            if ($this->content_type == 'application/json') {
136
                return json_decode($response);
137
            } else {
138
                return $response;
139
            }
140
        } else {
141
            return null;
142
        }
143
    }
144
145
    /**
146
     * Send or simulate sending of SMSs to selected lists.
147
     *
148
     * @return mixed
149
     */
150 View Code Duplication
    public function sendLists($params, $simulate = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $path = sprintf('%s/send/lists', $this->endpoint);
153
        if ($simulate) {
154
            $path .= '/simulate';
155
        }
156
157
        // Http request
158
        $response = $this->adapter->post($path, $params);
159
160
        // Result
161
        if ($this->content_type == 'application/json') {
162
            return json_decode($response);
163
        } else {
164
            return $response;
165
        }
166
    }
167
168
    /**
169
     * Cancel the sending with selected id.
170
     *
171
     * @return mixed
172
     */
173 View Code Duplication
    public function delete($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
    {
175
        // Http request
176
        $response = $this->adapter->delete(sprintf('%s/send/%d', $this->endpoint, $id));
177
178
        // Result
179
        if ($this->content_type == 'application/json') {
180
            return json_decode($response);
181
        } else {
182
            return $response;
183
        }
184
    }
185
186
    /**
187
     * Create or update a contact list.
188
     *
189
     * @return mixed
190
     */
191 View Code Duplication
    public function contactList($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
    {
193
        // Http request
194
        $response = $this->adapter->post(sprintf('%s/list', $this->endpoint), $params);
195
196
        // Result
197
        if ($this->content_type == 'application/json') {
198
            return json_decode($response);
199
        } else {
200
            return $response;
201
        }
202
    }
203
204
    /**
205
     * Retrieve contact lists.
206
     *
207
     * @return mixed
208
     */
209 View Code Duplication
    public function getContactList($id = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
    {
211
        $path = sprintf('%s/send', $this->endpoint);
212
        if ($id) {
213
            $path .= sprintf('/%d', $id);
214
        }
215
216
        // Http request
217
        $response = $this->adapter->get($path);
218
219
        // Result
220
        if ($this->content_type == 'application/json') {
221
            return json_decode($response);
222
        } else {
223
            return $response;
224
        }
225
    }
226
227
    /**
228
     * Remove duplicated contacts from list.
229
     *
230
     * @return mixed
231
     */
232 View Code Duplication
    public function deduplicate($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
    {
234
        $response = $this->adapter->put(sprintf('%s/deduplicate/%d', $this->endpoint, $id));
235
236
        // Result
237
        if ($this->content_type == 'application/json') {
238
            return json_decode($response);
239
        } else {
240
            return $response;
241
        }
242
    }
243
244
    /**
245
     * @return mixed
246
     */
247 View Code Duplication
    public function deleteContact($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
248
    {
249
        $response = $this->adapter->delete(sprintf('%s/list/contact/%d', $this->endpoint, $id));
250
251
        // Result
252
        if ($this->content_type == 'application/json') {
253
            return json_decode($response);
254
        } else {
255
            return $response;
256
        }
257
    }
258
259
    /**
260
     * Retrieve blacklist contacts.
261
     *
262
     * @return mixed
263
     */
264 View Code Duplication
    public function getBlacklist()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
265
    {
266
        // Http request
267
        $response = $this->adapter->get(sprintf('%s/blacklist', $this->endpoint));
268
269
        // Result
270
        if ($this->content_type == 'application/json') {
271
            return json_decode($response);
272
        } else {
273
            return $response;
274
        }
275
    }
276
277
    /**
278
     * @return mixed
279
     */
280 View Code Duplication
    public function deliveryReport($params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
    {
282
        if ($this->content_type == 'application/json') {
283
            throw new SMSFactorException("Delivery report is available accepts only XML");
284
        }
285
286
        $response = $this->adapter->post(sprintf('%s/dr', $this->endpoint), $params);
287
288
        // Result
289
        return $response;
290
    }
291
292
    private function isValidDate($date)
293
    {
294
        if (preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/', $date, $parts) == true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/^(\\d{4})-(...2})Z$/', $date, $parts) of type integer to the boolean true. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
295
            $time = gmmktime($parts[4], $parts[5], $parts[6], $parts[2], $parts[3], $parts[1]);
296
297
            $input_time = strtotime($date);
298
            if ($input_time === false) {
299
                return false;
300
            }
301
302
            return $input_time == $time;
303
        } else {
304
            return false;
305
        }
306
    }
307
308
}
309