Completed
Push — master ( e557bf...3aaf58 )
by Tristan
10s
created

Statistics::getStatsByPlatform()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 24
Ratio 100 %

Importance

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