Completed
Push — master ( cc31a5...4bc072 )
by romain
11s
created

Statistics::callTracking()   B

Complexity

Conditions 9
Paths 40

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 7.5353
c 0
b 0
f 0
cc 9
nc 40
nop 12

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * WannaSpeak API Bundle
5
 *
6
 * @author Jean-Baptiste Blanchon <[email protected]>
7
 */
8
9
namespace Yproximite\WannaSpeakBundle\Api;
10
11
use Http\Discovery\MessageFactoryDiscovery;
12
use Http\Discovery\StreamFactoryDiscovery;
13
use Http\Discovery\UriFactoryDiscovery;
14
use Http\Message\MultipartStream\MultipartStreamBuilder;
15
use Psr\Http\Message\ResponseInterface;
16
use Symfony\Component\HttpFoundation\File\UploadedFile;
17
18
/**
19
 * Class Statistics
20
 *
21
 * @see http://fr.wannaspeak.com/
22
 */
23
class Statistics implements StatisticsInterface
24
{
25
    const API_BASE_STAT_PARAMETER  = 'stat';
26
    const API_BASE_CT_PARAMETER    = 'ct';
27
    const API_BASE_SOUND_PARAMETER = 'sound';
28
    const BEGIN_DATE               = '01-01-2015';
29
30
    /**
31
     * @var WannaSpeakHttpClient
32
     */
33
    private $httpClient;
34
35
    /**
36
     * __construct
37
     *
38
     * @param WannaSpeakHttpClient $httpClient
39
     */
40
    public function __construct(WannaSpeakHttpClient $httpClient)
41
    {
42
        $this->httpClient = $httpClient;
43
    }
44
45
    /**
46
     * @param string $method
47
     *
48
     * @return mixed
49
     *
50
     * @throws \Exception
51
     */
52 View Code Duplication
    public function getNumbers($method)
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...
53
    {
54
        $args = [
55
            'api'    => self::API_BASE_CT_PARAMETER,
56
            'method' => $method,
57
        ];
58
59
        $response = $this->httpClient->createAndSendRequest($args);
60
        $data     = $this->processResponse($response);
61
62
        return $data['data']['dids'];
63
    }
64
65
    /**
66
     * Process the API response, provides error handling
67
     *
68
     * @param ResponseInterface $response
69
     *
70
     * @throws \Exception
71
     *
72
     * @return array
73
     */
74
    public function processResponse(ResponseInterface $response)
75
    {
76
        $data = json_decode($response->getBody()->getContents(), true);
77
78
        if ($data['error']) {
79
            throw new \Exception('WannaSpeak API: '.$data['error']['txt']);
80
        }
81
82
        return $data;
83
    }
84
85
    /**
86
     * We store the platformId in tag1
87
     *          and siteId     in tag2
88
     *
89
     * @param string      $method
90
     * @param string      $name
91
     * @param string      $phoneDest
92
     * @param string      $phoneDid
93
     * @param string      $platformId
94
     * @param string      $siteId
95
     * @param bool        $callerId
96
     * @param string|null $leg1
97
     * @param string|null $leg2
98
     * @param string|null $phoneMobileNumberForMissedCall
99
     *
100
     * @return array
101
     */
102
    public function callTracking(
103
        $method,
104
        $name,
105
        $phoneDest,
106
        $phoneDid,
107
        $platformId,
108
        $siteId,
109
        $callerId = false,
110
        $leg1 = null,
111
        $leg2 = null,
112
        $phoneMobileNumberForMissedCall = null,
113
        $smsSenderName = null,
114
        $smsCompanyName = null
115
    ) {
116
        $args = [
117
            'api'         => self::API_BASE_CT_PARAMETER,
118
            'method'      => $method,
119
            'destination' => $phoneDest,
120
            'tag1'        => $platformId,
121
            'tag2'        => $siteId,
122
            'tag3'        => ($callerId === true) ? 'callerid:'.$phoneDid : '',
123
            'did'         => $phoneDid,
124
            'name'        => $name,
125
        ];
126
127
        if ($leg1 !== null) {
128
            $args['leg1'] = $leg1;
129
        }
130
131
        if ($leg2 !== null) {
132
            $args['leg2'] = $leg2;
133
        }
134
135
        if ($phoneMobileNumberForMissedCall !== null) {
136
            $args['sms'] = $phoneMobileNumberForMissedCall;
137
138
            if ($smsSenderName !== null && $smsSenderName !== '') {
139
                $args['var4'] = $smsSenderName;
140
            }
141
142
            if ($smsCompanyName !== null && $smsCompanyName !== '') {
143
                $args['var5'] = $smsCompanyName;
144
            }
145
        }
146
147
        $response = $this->httpClient->createAndSendRequest($args);
148
        $data     = $this->processResponse($response);
149
150
        return $data;
151
    }
152
153
    /**
154
     * @param string    $didPhone
155
     * @param \DateTime $expirationDate
156
     *
157
     * @return array
158
     */
159
    public function callTrackingExpiresAt($didPhone, \DateTime $expirationDate = null)
160
    {
161
        if (!$expirationDate) {
162
            $expirationDate = new \DateTime('now');
163
        }
164
165
        $args = [
166
            'api'     => self::API_BASE_CT_PARAMETER,
167
            'method'  => 'modify',
168
            'did'     => $didPhone,
169
            'enddate' => $expirationDate->format('Y-m-d H:i:s'),
170
        ];
171
172
        $response = $this->httpClient->createAndSendRequest($args);
173
        $data     = $this->processResponse($response);
174
175
        return $data;
176
    }
177
178
    /**
179
     * Will fetch all datas from your account
180
     * from $beginDate to $endDate
181
     *
182
     * if there are no dates, the API's default behaviour will return
183
     * today's calls. we provide defaults dates in order to have all
184
     * calls from the begining of the time to now
185
     *
186
     * @param \DateTime $beginDate
187
     * @param \DateTime $endDate
188
     *
189
     * @return array
190
     */
191
    public function getAllStats(\DateTime $beginDate = null, \DateTime $endDate = null)
192
    {
193
        if (!$beginDate) {
194
            $beginDate = new \DateTime(self::BEGIN_DATE);
195
        }
196
197
        if (!$endDate) {
198
            $endDate = new \DateTime('NOW');
199
        }
200
201
        $args = [
202
            'api'       => self::API_BASE_STAT_PARAMETER,
203
            'method'    => 'did',
204
            'starttime' => $beginDate->format('Y-m-d H:i:s'),
205
            'stoptime'  => $endDate->format('Y-m-d H:i:s'),
206
        ];
207
208
        $response = $this->httpClient->createAndSendRequest($args);
209
        $data     = $this->processResponse($response);
210
211
        return $data;
212
    }
213
214
    /**
215
     * Will fetch all datas from your account
216
     * from $beginDate to $endDate
217
     *
218
     * if there are no dates, the API's default behaviour will return
219
     * today's calls. we provide defaults dates in order to have all
220
     * calls from the begining of the time to now
221
     *
222
     * @param string    $platformId
223
     * @param \DateTime $beginDate
224
     * @param \DateTime $endDate
225
     *
226
     * @return array
227
     */
228 View Code Duplication
    public function getStatsByPlatform($platformId, \DateTime $beginDate = null, \DateTime $endDate = 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...
229
    {
230
        if (!$beginDate) {
231
            $beginDate = new \DateTime(self::BEGIN_DATE);
232
        }
233
234
        if (!$endDate) {
235
            $endDate = new \DateTime('NOW');
236
        }
237
238
        $args = [
239
            'api'       => self::API_BASE_STAT_PARAMETER,
240
            'method'    => 'did',
241
            'nodid'     => '1',
242
            'tag1'      => $platformId,
243
            'starttime' => $beginDate->format('Y-m-d 00:00:00'),
244
            'stoptime'  => $endDate->format('Y-m-d 23:59:59'),
245
        ];
246
247
        $response = $this->httpClient->createAndSendRequest($args);
248
        $data     = $this->processResponse($response);
249
250
        return $data;
251
    }
252
253
    /**
254
     * Will fetch all datas from your account
255
     * from $beginDate to $endDate
256
     *
257
     * if there are no dates, the API's default behaviour will return
258
     * today's calls. we provide defaults dates in order to have all
259
     * calls from the begining of the time to now
260
     *
261
     * @param string    $siteId
262
     * @param \DateTime $beginDate
263
     * @param \DateTime $endDate
264
     *
265
     * @return array
266
     */
267 View Code Duplication
    public function getStatsBySite($siteId, \DateTime $beginDate = null, \DateTime $endDate = 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...
268
    {
269
        if (!$beginDate) {
270
            $beginDate = new \DateTime(self::BEGIN_DATE);
271
        }
272
273
        if (!$endDate) {
274
            $endDate = new \DateTime('NOW');
275
        }
276
277
        $args = [
278
            'api'       => self::API_BASE_STAT_PARAMETER,
279
            'method'    => 'did',
280
            'nodid'     => '1',
281
            'tag2'      => $siteId,
282
            'starttime' => $beginDate->format('Y-m-d 00:00:00'),
283
            'stoptime'  => $endDate->format('Y-m-d 23:59:59'),
284
        ];
285
286
        $response = $this->httpClient->createAndSendRequest($args);
287
        $data     = $this->processResponse($response);
288
289
        return $data;
290
    }
291
292
    /**
293
     *
294
     * @param string $didPhone
295
     *
296
     * @return array
297
     */
298 View Code Duplication
    public function callTrackingDelete($didPhone)
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...
299
    {
300
        $args = [
301
            'api'    => self::API_BASE_CT_PARAMETER,
302
            'method' => 'delete',
303
            'did'    => $didPhone,
304
        ];
305
306
        $response = $this->httpClient->createAndSendRequest($args);
307
        $data     = $this->processResponse($response);
308
309
        return $data;
310
    }
311
312
    /**
313
     * @param int $link
314
     *
315
     * @return array
316
     */
317 View Code Duplication
    public function listSounds($link = 0)
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...
318
    {
319
        $args = [
320
            'api'    => self::API_BASE_SOUND_PARAMETER,
321
            'method' => 'available',
322
            'link'   => $link,
323
        ];
324
325
        $response = $this->httpClient->createAndSendRequest($args);
326
        $data     = $this->processResponse($response);
327
328
        return $data;
329
    }
330
331
    /**
332
     * @param UploadedFile $message
333
     *
334
     * @return array
335
     */
336
    public function uploadMessageToWannaspeak(UploadedFile $message)
337
    {
338
        $name    = str_replace('.mp3', '', $message->getClientOriginalName());
339
        $args    = [
340
            'api'    => 'sound',
341
            'method' => 'upload',
342
            'name'   => $name,
343
        ];
344
        $options = [
345
            'filename' => $name,
346
            'headers'  => [
347
                'Content-Type' => 'application/octet-stream',
348
            ],
349
        ];
350
351
        $boundary      = '--------------------------'.microtime(true);
352
        $streamFactory = StreamFactoryDiscovery::find();
353
        $builder       = new MultipartStreamBuilder($streamFactory);
354
355
        $builder->setBoundary($boundary);
356
357
        $fp   = fopen($message->getRealPath(), 'rb');
358
        $data = stream_get_contents($fp);
359
        fclose($fp);
360
361
        $builder->addResource('sound', $data, $options);
362
363
        $body    = $builder->build();
364
        $headers = ['Content-Type' => 'multipart/form-data; boundary="'.$boundary.'"'];
365
366
        $response = $this->httpClient->createAndSendRequest($args, $headers, $body);
367
        $data     = $this->processResponse($response);
368
369
        return $data;
370
    }
371
372
    /**
373
     * @param string $name
374
     *
375
     * @return array
376
     */
377 View Code Duplication
    public function deleteMessageWannaspeak($name)
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...
378
    {
379
        $args = [
380
            'api'    => 'sound',
381
            'method' => 'delete',
382
            'name'   => $name,
383
        ];
384
385
        $response = $this->httpClient->createAndSendRequest($args);
386
        $data     = $this->processResponse($response);
387
388
        return $data;
389
    }
390
}
391