OperationalRTService::isIntervalValid()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 2
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
     */
32
    public function queueHealth(int $timeFrame = 60, array $skillIds = [], $interval = null)
33
    {
34
35
36
        if (!$this->isTimeFrameValid($timeFrame)) {
37
            throw new \InvalidArgumentException(sprintf('The $timeframe must be between 0 and 1440, you passed %d', $timeFrame));
38
        }
39
        if (!$this->isIntervalValid($timeFrame, $interval)) {
40
            throw new \InvalidArgumentException(sprintf('The $interval you passed was not valid or not dividable by the $timeframe (%d), you passed %d', $timeFrame, $interval));
41
        }
42
43
44
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
45
            ->setService($this->getService())
46
            ->setAccount($this->config->getAccountId())
47
            ->setAction('queuehealth')
48
            ->hasQueryParam(true)
49
            ->addQueryParam('timeframe', $timeFrame);
50
51
        if (!empty($skillIds)) {
52
            // Implode the array into a comma separated string and add to the url.
53
            $this->urlBuilder->addQueryParam('skillIds', $this->arrayToList($skillIds));
54
        } else {
55
            $this->urlBuilder->addQueryParam('skillIds', 'all');
56
        }
57
58
        if (null !== $interval) {
59
            $this->urlBuilder->addQueryParam('interval', $interval);
60
        }
61
62
        $this->urlBuilder->build();
63
64
        $this->handle($payload = [], Request::METHOD_GET);
65
    }
66
67
    /**
68
     * Retrieves engagement activity-related metrics at the account, skill, or agent level
69
     * @see https://developers.liveperson.com/data-operational-realtime-engagement-activity.html
70
     *
71
     * @param int $timeFrame The time range in minutes max value is 1440 minutes (24 hours)
72
     * @param array $agents an array of agents to get data for, this can be left empty to return data for all agents
73
     * @param array $skillIds an array of skills to filter by, this can be left empty to get data for all skills.
74
     * @param int|null $interval the interval to filter at.
75
     *
76
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
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();
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
     */
125
    public function slaHistogram($timeFrame = 60, array $skillIds = [], array $groupIds = [], array $histogram = [])
126
    {
127
        if (!$this->isTimeFrameValid($timeFrame)) {
128
            throw new \InvalidArgumentException(sprintf('The $timeframe must be between 0 and 1440, you passed %d', $timeFrame));
129
        }
130
131
        // Check if the histogram values are multiples of 5.
132
        if (count($histogram) > 0) {
133
            foreach ($histogram as $value) {
134
                if ($value % 5 != 0) {
135
                    // One or more of the values is not a multiple of 5.
136
                    $message = sprintf('One or more of your histogram values is not a multiple of 5. You passed %s', $this->arrayToList($histogram));
137
138
                    $this->log($message, LogLevel::ERROR, $histogram);
139
140
                    throw new \InvalidArgumentException($message);
141
                }
142
            }
143
        }
144
145
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
146
            ->setService($this->getService())
147
            ->setAccount($this->config->getAccountId())
148
            ->setAction('sla')
149
            ->hasQueryParam(true)
150
            ->addQueryParam('timeframe', $timeFrame);
151
152
        if (0 !== count($skillIds)) {
153
            $this->urlBuilder->addQueryParam('skillIds', $this->arrayToList($skillIds));
154
        }
155
        if (0 !== count($groupIds)) {
156
            $this->urlBuilder->addQueryParam('groupIds', $this->arrayToList($groupIds));
157
        }
158
        if (0 !== count($histogram)) {
159
            $this->urlBuilder->addQueryParam('histogram', $this->arrayToList($histogram));
160
        }
161
162
        $this->urlBuilder->build();
163
164
        $this->handle();
165
    }
166
167
    /**
168
     * Retrieves Agent State Distribution data
169
     * @see https://developers.liveperson.com/data-operational-realtime-agent-activity.html
170
     *
171
     * @param int $timeFrame The time range in minutes max value is 1440 minutes (24 hours)
172
     * @param array $agents an array of agents to get data for, this can be left empty to return data for all agents
173
     * @param int|null $interval $interval the interval to filter at.
174
     *
175
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
176
     * @throws \CwsOps\LivePerson\Rest\URLNotBuiltException
177
     */
178
    public function agentActivity(int $timeFrame = 1440, array $agents = [], $interval = null)
179
    {
180
        if (!$this->isTimeFrameValid($timeFrame)) {
181
            throw new \InvalidArgumentException(sprintf('The $timeframe must be between 0 and 1440, you passed %d', $timeFrame));
182
        }
183
        if (!$this->isIntervalValid($timeFrame,$interval)) {
184
            throw new \InvalidArgumentException(sprintf('The $interval you passed was not valid or not dividable by the $timeframe (%d), you passed %d', $timeFrame, $interval));
185
        }
186
187
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
188
            ->setService('operations')
189
            ->setAccount($this->config->getAccountId())
190
            ->setAction('agentactivity');
191
192
        // the API requests that if there is a long list of AgentsId, these should be passed in a POST request.
193
        // So  we check here if the agent list is more than 3 and if so we will instead send a post request.
194
        $payLoad = [];
195
196
        if (count($agents) < 3) {
197
            $method = Request::METHOD_GET;
198
            $this->urlBuilder
199
                ->hasQueryParam(true)
200
                ->addQueryParam('timeframe', $timeFrame);
201
202
            if (0 !== count($agents)) {
203
                $this->urlBuilder->addQueryParam('agentIds', rtrim(implode(self::GLUE_CHAR, $agents), self::GLUE_CHAR));
204
            } else {
205
                $this->urlBuilder->addQueryParam('agentIds', 'all');
206
            }
207
208
            if (null !== $interval) {
209
                $this->urlBuilder->addQueryParam('interval', $interval);
210
            }
211
        } else {
212
            $method = Request::METHOD_POST;
213
            // agents is more than 3, so were going to send a POST request.
214
            $payLoad['timeframe'] = $timeFrame;
215
            $payLoad['agentsIds'] = $this->arrayToList($agents);
216
            if (null !== $interval) {
217
                $payLoad['interval'] = $interval;
218
            }
219
        }
220
221
        $this->urlBuilder->build();
222
223
        $this->handle($payLoad, $method);
224
    }
225
226
227
    /**
228
     * Gets the current Queue State
229
     *
230
     * @see https://developers.liveperson.com/data-operational-realtime-current-queue-state.html
231
     *
232
     * @param array $skillIds an optional array of skills to filter the queue on.
233
     *
234
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
235
     * @throws \CwsOps\LivePerson\Rest\URLNotBuiltException
236
     */
237
    public function currentQueueState(array $skillIds = [])
238
    {
239
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
240
            ->setService($this->getService())
241
            ->setAccount($this->config->getAccountId())
242
            ->setAction('queuestate');
243
244
        if (0 !== count($skillIds)) {
245
            $this->urlBuilder->hasQueryParam(true);
246
            $this->urlBuilder->addQueryParam('skillIds', $this->arrayToList($skillIds));
247
        }
248
249
        $this->urlBuilder->build()->getUrl();
250
251
        $this->handle();
252
    }
253
254
255
    /**
256
     * Should provide the Live person service, this service will query against
257
     *
258
     * @return string
259
     */
260
    protected function getDomain(): string
261
    {
262
        return 'leDataReporting';
263
    }
264
265
266
    /**
267
     * Should provide the Live Person service the service will query against.
268
     *
269
     * @return string
270
     */
271
    protected function getService(): string
272
    {
273
        return 'operations';
274
    }
275
276
    /**
277
     * Checks if the timeframe is valid.
278
     * i.e. the time is only valid between 0 and 1440 minutes (0 hrs to 24hrs).
279
     *
280
     * @param int $timeFrame the number of minutes.
281
     *
282
     * @return bool true or false if the timeframe is valid.
283
     */
284
    private function isTimeFrameValid(int $timeFrame): bool
285
    {
286
        if ($timeFrame >= 0 && $timeFrame <= 1440) {
287
            return true;
288
        }
289
        return false;
290
    }
291
292
    /**
293
     * Checks if the interval is valid.
294
     * The interval must be less than the time frame and must be dividable by the timeframe.
295
     *
296
     * @param int $timeFrame the timeframe value.
297
     * @param int $interval the interval to check.
298
     *
299
     * @return bool true or false if the interval is valid.
300
     */
301
    private function isIntervalValid($timeFrame, $interval): bool
302
    {
303
        if (null === $interval || $interval <= $timeFrame && $timeFrame % $interval === 0) {
304
            return true;
305
        }
306
        return false;
307
    }
308
}
309