Test Failed
Push — master ( c59641...db9bda )
by James
07:26 queued 55s
created

OperationalRTService::slaHistogram()   B

Complexity

Conditions 8
Paths 18

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 40
rs 8.4444
c 0
b 0
f 0
cc 8
nc 18
nop 4
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: james
5
 * Date: 25/07/2018
6
 * Time: 08:43
7
 */
8
9
namespace CwsOps\LivePerson\Services;
10
11
use CwsOps\LivePerson\Rest\Request;
12
use Psr\Log\LogLevel;
13
14
/**
15
 * Class OperationalRTService
16
 * @see https://developers.liveperson.com/data-operational-realtime-overview.html
17
 * @package CwsOps\LivePerson\Services
18
 * @author James Parker <[email protected]>
19
 */
20
class OperationalRTService extends AbstractService
21
{
22
    /**
23
     * Gets the queue heath metrics at account or skill level.
24
     * @see https://developers.liveperson.com/data-operational-realtime-queue-health.html
25
     *
26
     * @param int $timeFrame The time range in minutes max value is 1440 minutes (24 hours)
27
     * @param array $skillIds A list of skills to filter the data by or null for account level
28
     * @param int|null $interval the intervals to get data at.
29
     *
30
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
31
     * @throws \CwsOps\LivePerson\Rest\URLNotBuiltException
32
     */
33
    public function queueHealth(int $timeFrame = 60, array $skillIds = [], $interval = null)
34
    {
35
36
37
        if (!$this->isTimeFrameValid($timeFrame)) {
38
            throw new \InvalidArgumentException(sprintf('The $timeframe must be between 0 and 1440, you passed %d', $timeFrame));
39
        }
40
        if (!$this->isIntervalValid($timeFrame, $interval)) {
41
            throw new \InvalidArgumentException(sprintf('The $interval you passed was not valid or not dividable by the $timeframe (%d), you passed %d', $timeFrame, $interval));
42
        }
43
44
45
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
46
            ->setService($this->getService())
47
            ->setAccount($this->config->getAccountId())
48
            ->setAction('queuehealth')
49
            ->hasQueryParam(true)
50
            ->addQueryParam('timeframe', $timeFrame);
51
52
        if ([] === $skillIds) {
53
            // Implode the array into a comma separated string and add to the url.
54
            $this->urlBuilder->addQueryParam('skillIds', $this->arrayToList($skillIds));
55
        }
56
57
        if (null !== $interval) {
58
            $this->urlBuilder->addQueryParam('interval', $interval);
59
        }
60
61
        $this->urlBuilder->build()->getUrl();
62
63
        $this->handle($payload = [], Request::METHOD_GET);
64
    }
65
66
    /**
67
     * Retrieves engagement activity-related metrics at the account, skill, or agent level
68
     * @see https://developers.liveperson.com/data-operational-realtime-engagement-activity.html
69
     *
70
     * @param int $timeFrame The time range in minutes max value is 1440 minutes (24 hours)
71
     * @param array $agents an array of agents to get data for, this can be left empty to return data for all agents
72
     * @param array $skillIds an array of skills to filter by, this can be left empty to get data for all skills.
73
     * @param int|null $interval the interval to filter at.
74
     *
75
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
76
     * @throws \CwsOps\LivePerson\Rest\URLNotBuiltException
77
     */
78
    public function engagementActivity(int $timeFrame = 60, array $agents = [], array $skillIds = [], int $interval = null)
79
    {
80
        if (!$this->isTimeFrameValid($timeFrame)) {
81
            $message = sprintf('The $timeframe must be between 0 and 1440, you passed %d', $timeFrame);
82
            throw new \InvalidArgumentException($message);
83
        }
84
        if (!$this->isIntervalValid($timeFrame, $interval)) {
85
            $message = sprintf('The $interval you passed was not valid or not dividable by the $timeframe (%d), you passed %d', $timeFrame, $interval);
86
            throw new \InvalidArgumentException($message);
87
        }
88
89
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
90
            ->setService($this->getService())
91
            ->setAccount($this->config->getAccountId())
92
            ->setAction('engactivity')
93
            ->hasQueryParam(true)
94
            ->addQueryParam('timeframe', $timeFrame);
95
96
        if (0 !== count($agents)) {
97
            $this->urlBuilder->addQueryParam('agentIds', $this->arrayToList($agents));
98
        }
99
        if (0 !== count($skillIds)) {
100
            $this->urlBuilder->addQueryParam('skillIds', $this->arrayToList($skillIds));
101
        }
102
        if (null !== $interval) {
103
            $this->urlBuilder->addQueryParam('interval', $interval);
104
        }
105
106
        $payload = [];
107
108
        $this->urlBuilder->build()->getUrl();
109
110
        $this->handle($payload);
111
    }
112
113
114
    /**
115
     * Retrieves the distribution of visitors’ wait time in the queue, before an agent replies to their chat.
116
     * @see https://developers.liveperson.com/data-operational-realtime-sla-histogram.html
117
     *
118
     * @param int $timeFrame the timeframe in minutes to filter the data from.
119
     * @param array $skillIds an optional array of skills to filter the data by.
120
     * @param array $groupIds an optional array of groups filter agents by.
121
     * @param array $histogram an array of histogram values to provide. All values must be multiples of 5.
122
     *
123
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
124
     * @throws \CwsOps\LivePerson\Rest\URLNotBuiltException
125
     */
126
    public function slaHistogram($timeFrame = 60, array $skillIds = [], array $groupIds = [], array $histogram = [])
127
    {
128
        if (!$this->isTimeFrameValid($timeFrame)) {
129
            throw new \InvalidArgumentException(sprintf('The $timeframe must be between 0 and 1440, you passed %d', $timeFrame));
130
        }
131
132
        // Check if the histogram values are multiples of 5.
133
        if (count($histogram) > 0) {
134
            foreach ($histogram as $value) {
135
                if ($value % 5 != 0) {
136
                    // One or more of the values is not a multiple of 5.
137
                    $message = sprintf('One or more of your histogram values is not a multiple of 5. You passed %s', $this->arrayToList($histogram));
138
139
                    $this->log($message, LogLevel::ERROR, $histogram);
140
141
                    throw new \InvalidArgumentException($message);
142
                }
143
            }
144
        }
145
146
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
147
            ->setService($this->getService())
148
            ->setAccount($this->config->getAccountId())
149
            ->setAction('sla')
150
            ->hasQueryParam(true)
151
            ->addQueryParam('timeframe', $timeFrame);
152
153
        if (0 !== count($skillIds)) {
154
            $this->urlBuilder->addQueryParam('skillIds', $this->arrayToList($skillIds));
155
        }
156
        if (0 !== count($groupIds)) {
157
            $this->urlBuilder->addQueryParam('groupIds', $this->arrayToList($groupIds));
158
        }
159
        if (0 !== count($histogram)) {
160
            $this->urlBuilder->addQueryParam('histogram', $this->arrayToList($histogram));
161
        }
162
163
        $this->urlBuilder->build()->getUrl();
164
165
        $this->handle();
166
    }
167
168
    /**
169
     * Retrieves Agent State Distribution data
170
     * @see https://developers.liveperson.com/data-operational-realtime-agent-activity.html
171
     *
172
     * @param int $timeFrame The time range in minutes max value is 1440 minutes (24 hours)
173
     * @param array $agents an array of agents to get data for, this can be left empty to return data for all agents
174
     * @param int|null $interval $interval the interval to filter at.
175
     *
176
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
177
     * @throws \CwsOps\LivePerson\Rest\URLNotBuiltException
178
     */
179
    public function agentActivity(int $timeFrame = 60, array $agents = [], $interval = null)
180
    {
181
        if (!$this->isTimeFrameValid($timeFrame)) {
182
            throw new \InvalidArgumentException(sprintf('The $timeframe must be between 0 and 1440, you passed %d', $timeFrame));
183
        }
184
        if (!$this->isIntervalValid($timeFrame, $interval)) {
185
            throw new \InvalidArgumentException(sprintf('The $interval you passed was not valid or not dividable by the $timeframe (%d), you passed %d', $timeFrame, $interval));
186
        }
187
188
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
189
            ->setService('operations')
190
            ->setAccount($this->config->getAccountId())
191
            ->setAction('agentactivity');
192
193
        // the API requests that if there is a long list of AgentsId, these should be passed in a POST request.
194
        // So  we check here if the agent list is more than 3 and if so we will instead send a post request.
195
        $payLoad = [];
196
197
        if (count($agents) < 3) {
198
            $method = Request::METHOD_GET;
199
            $this->urlBuilder
200
                ->hasQueryParam(true)
201
                ->addQueryParam('timeframe', $timeFrame);
202
203
            if (0 !== count($agents)) {
204
                $this->urlBuilder->addQueryParam('agentIds', rtrim(implode(self::GLUE_CHAR, $agents), self::GLUE_CHAR));
205
            } else {
206
                $this->urlBuilder->addQueryParam('agentsIds', 'all');
207
            }
208
209
            if (null !== $interval) {
210
                $this->urlBuilder->addQueryParam('interval', $interval);
211
            }
212
        } else {
213
            $method = Request::METHOD_POST;
214
            // agents is more than 3, so were going to send a POST request.
215
            $payLoad['timeframe'] = $timeFrame;
216
            $payLoad['agentsIds'] = $this->arrayToList($agents);
217
            if (null !== $interval) {
218
                $payLoad['interval'] = $interval;
219
            }
220
        }
221
222
        $this->urlBuilder->build()->getUrl();
223
224
        $this->handle($payLoad, $method);
225
    }
226
227
228
    /**
229
     * Gets the current Queue State
230
     *
231
     * @see https://developers.liveperson.com/data-operational-realtime-current-queue-state.html
232
     *
233
     * @param array $skillIds an optional array of skills to filter the queue on.
234
     *
235
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
236
     * @throws \CwsOps\LivePerson\Rest\URLNotBuiltException
237
     */
238
    public function currentQueueState(array $skillIds = [])
239
    {
240
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
241
            ->setService($this->getService())
242
            ->setAccount($this->config->getAccountId())
243
            ->setAction('queuestate');
244
245
        if (0 !== count($skillIds)) {
246
            $this->urlBuilder->hasQueryParam(true);
247
            $this->urlBuilder->addQueryParam('skillIds', $this->arrayToList($skillIds));
248
        }
249
250
        $this->urlBuilder->build()->getUrl();
251
252
        $this->handle();
253
    }
254
255
256
    /**
257
     * Should provide the Live person service, this service will query against
258
     *
259
     * @return string
260
     */
261
    protected function getDomain(): string
262
    {
263
        return 'leDataReporting';
264
    }
265
266
267
    /**
268
     * Should provide the Live Person service the service will query against.
269
     *
270
     * @return string
271
     */
272
    protected function getService(): string
273
    {
274
        return 'operations';
275
    }
276
277
    /**
278
     * Checks if the timeframe is valid.
279
     * i.e. the time is only valid between 0 and 1440 minutes (0 hrs to 24hrs).
280
     *
281
     * @param int $timeFrame the number of minutes.
282
     *
283
     * @return bool true or false if the timeframe is valid.
284
     */
285
    private function isTimeFrameValid(int $timeFrame): bool
286
    {
287
        if ($timeFrame >= 0 && $timeFrame <= 1440) {
288
            return true;
289
        }
290
        return false;
291
    }
292
293
    /**
294
     * Checks if the interval is valid.
295
     * The interval must be less than the time frame and must be dividable by the timeframe.
296
     *
297
     * @param int $timeFrame the timeframe value.
298
     * @param int $interval the interval to check.
299
     *
300
     * @return bool true or false if the interval is valid.
301
     */
302
    private function isIntervalValid($timeFrame, $interval): bool
303
    {
304
        if (null === $interval || $interval <= $timeFrame && $timeFrame % $interval === 0) {
305
            return true;
306
        }
307
        return false;
308
    }
309
}
310