Completed
Push — master ( 926d98...7d5bdb )
by Elodie
13s
created

Statistics::listSounds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 13
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
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 Psr\Http\Message\ResponseInterface;
12
13
/**
14
 * Class Statistics
15
 *
16
 * @see http://fr.wannaspeak.com/
17
 */
18
class Statistics
19
{
20
    const API_BASE_STAT_PARAMETER  = 'stat';
21
    const API_BASE_CT_PARAMETER    = 'ct';
22
    const API_BASE_SOUND_PARAMETER = 'sound';
23
    const BEGIN_DATE               = '01-01-2015';
24
25
    /**
26
     * @var WannaSpeakHttpClient
27
     */
28
    private $httpClient;
29
30
    /**
31
     * __construct
32
     *
33
     * @param WannaSpeakHttpClient $httpClient
34
     */
35
    public function __construct(WannaSpeakHttpClient $httpClient)
36
    {
37
        $this->httpClient = $httpClient;
38
    }
39
40
    /**
41
     * @param string $method
42
     *
43
     * @return mixed
44
     *
45
     * @throws \Exception
46
     */
47 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...
48
    {
49
        $args = [
50
            'api'    => self::API_BASE_CT_PARAMETER,
51
            'method' => $method,
52
        ];
53
54
        $response = $this->httpClient->createAndSendRequest($args);
55
        $data     = $this->processResponse($response);
56
57
        return $data['data']['dids'];
58
    }
59
60
    /**
61
     * Process the API response, provides error handling
62
     *
63
     * @param ResponseInterface $response
64
     *
65
     * @throws \Exception
66
     *
67
     * @return array
68
     */
69
    public function processResponse(ResponseInterface $response)
70
    {
71
        $data = json_decode($response->getBody()->getContents(), true);
72
73
        if ($data['error']) {
74
            throw new \Exception('WannaSpeak API: '.$data['error']['txt']);
75
        }
76
77
        return $data;
78
    }
79
80
    /**
81
     * We store the platformId in tag1
82
     *          and siteId     in tag2
83
     *
84
     * @param string      $method
85
     * @param string      $name
86
     * @param string      $phoneDest
87
     * @param string      $phoneDid
88
     * @param string      $platformId
89
     * @param string      $siteId
90
     * @param bool        $callerId
91
     * @param string|null $leg1
92
     * @param string|null $leg2
93
     *
94
     * @return array
95
     */
96
    public function callTracking($method, $name, $phoneDest, $phoneDid, $platformId, $siteId, $callerId = false, $leg1 = null, $leg2 = null)
97
    {
98
        $args = [
99
            'api'         => self::API_BASE_CT_PARAMETER,
100
            'method'      => $method,
101
            'destination' => $phoneDest,
102
            'tag1'        => $platformId,
103
            'tag2'        => $siteId,
104
            'tag3'        => ($callerId === true) ? 'callerid:'.$phoneDid : '',
105
            'did'         => $phoneDid,
106
            'name'        => $name,
107
        ];
108
109
        if ($leg1 !== null) {
110
            $args['leg1'] = $leg1;
111
        }
112
113
        if ($leg2 !== null) {
114
            $args['leg2'] = $leg2;
115
        }
116
117
        $response = $this->httpClient->createAndSendRequest($args);
118
        $data     = $this->processResponse($response);
119
120
        return $data;
121
    }
122
123
    /**
124
     * Will fetch all datas from your account
125
     * from $beginDate to $endDate
126
     *
127
     * if there are no dates, the API's default behaviour will return
128
     * today's calls. we provide defaults dates in order to have all
129
     * calls from the begining of the time to now
130
     *
131
     * @param \DateTime $beginDate
132
     * @param \DateTime $endDate
133
     *
134
     * @return array
135
     */
136
    public function getAllStats(\DateTime $beginDate = null, \DateTime $endDate = null)
137
    {
138
        if (!$beginDate) {
139
            $beginDate = new \DateTime(self::BEGIN_DATE);
140
        }
141
142
        if (!$endDate) {
143
            $endDate = new \DateTime('NOW');
144
        }
145
146
        $args = [
147
            'api'       => self::API_BASE_STAT_PARAMETER,
148
            'method'    => 'did',
149
            'starttime' => $beginDate->format('Y-m-d H:i:s'),
150
            'stoptime'  => $endDate->format('Y-m-d H:i:s'),
151
        ];
152
153
        $response = $this->httpClient->createAndSendRequest($args);
154
        $data     = $this->processResponse($response);
155
156
        return $data;
157
    }
158
159
    /**
160
     * Will fetch all datas from your account
161
     * from $beginDate to $endDate
162
     *
163
     * if there are no dates, the API's default behaviour will return
164
     * today's calls. we provide defaults dates in order to have all
165
     * calls from the begining of the time to now
166
     *
167
     * @param string    $platformId
168
     * @param \DateTime $beginDate
169
     * @param \DateTime $endDate
170
     *
171
     * @return array
172
     */
173 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...
174
    {
175
        if (!$beginDate) {
176
            $beginDate = new \DateTime(self::BEGIN_DATE);
177
        }
178
179
        if (!$endDate) {
180
            $endDate = new \DateTime('NOW');
181
        }
182
183
        $args = [
184
            'api'       => self::API_BASE_STAT_PARAMETER,
185
            'method'    => 'did',
186
            'nodid'     => '1',
187
            'tag1'      => $platformId,
188
            'starttime' => $beginDate->format('Y-m-d 00:00:00'),
189
            'stoptime'  => $endDate->format('Y-m-d 23:59:59'),
190
        ];
191
192
        $response = $this->httpClient->createAndSendRequest($args);
193
        $data     = $this->processResponse($response);
194
195
        return $data;
196
    }
197
198
    /**
199
     * Will fetch all datas from your account
200
     * from $beginDate to $endDate
201
     *
202
     * if there are no dates, the API's default behaviour will return
203
     * today's calls. we provide defaults dates in order to have all
204
     * calls from the begining of the time to now
205
     *
206
     * @param string    $siteId
207
     * @param \DateTime $beginDate
208
     * @param \DateTime $endDate
209
     *
210
     * @return array
211
     */
212 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...
213
    {
214
        if (!$beginDate) {
215
            $beginDate = new \DateTime(self::BEGIN_DATE);
216
        }
217
218
        if (!$endDate) {
219
            $endDate = new \DateTime('NOW');
220
        }
221
222
        $args = [
223
            'api'       => self::API_BASE_STAT_PARAMETER,
224
            'method'    => 'did',
225
            'nodid'     => '1',
226
            'tag2'      => $siteId,
227
            'starttime' => $beginDate->format('Y-m-d 00:00:00'),
228
            'stoptime'  => $endDate->format('Y-m-d 23:59:59'),
229
        ];
230
231
        $response = $this->httpClient->createAndSendRequest($args);
232
        $data     = $this->processResponse($response);
233
234
        return $data;
235
    }
236
237
    /**
238
     *
239
     * @param string $didPhone
240
     *
241
     * @return array
242
     */
243 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...
244
    {
245
        $args = [
246
            'api'    => self::API_BASE_CT_PARAMETER,
247
            'method' => 'delete',
248
            'did'    => $didPhone,
249
        ];
250
251
        $response = $this->httpClient->createAndSendRequest($args);
252
        $data     = $this->processResponse($response);
253
254
        return $data;
255
    }
256
257
    /**
258
     * @param int $link
259
     *
260
     * @return array
261
     */
262 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...
263
    {
264
        $args = [
265
            'api'    => self::API_BASE_SOUND_PARAMETER,
266
            'method' => 'available',
267
            'link'   => $link,
268
        ];
269
270
        $response = $this->httpClient->createAndSendRequest($args);
271
        $data     = $this->processResponse($response);
272
273
        return $data;
274
    }
275
}
276