Passed
Branch real-time-api (954386)
by James
06:12
created

OperationalRTService::isTimeFrameValid()   A

Complexity

Conditions 3
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 3
nc 2
nop 1
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 = [], int $interval = null)
34
    {
35
        if (!$this->isTimeFrameValid($timeFrame)) {
36
            throw new \InvalidArgumentException(sprintf('The $timeframe must be between 0 and 1440, you passed %d', $timeFrame));
37
        }
38
        if (!$this->isIntervalValid($interval)) {
0 ignored issues
show
Bug introduced by
It seems like $interval can also be of type null; however, parameter $timeFrame of CwsOps\LivePerson\Servic...vice::isIntervalValid() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        if (!$this->isIntervalValid(/** @scrutinizer ignore-type */ $interval)) {
Loading history...
39
            throw new \InvalidArgumentException(sprintf('The $interval you passed was not valid or not dividable by the $timeframe (%d), you passed %d', $timeFrame, $interval));
40
        }
41
42
43
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
44
            ->setService($this->getService())
45
            ->setAccount($this->config->getAccountId())
46
            ->setAction('queuehealth')
47
            ->hasQueryParam(true)
48
            ->addQueryParam('timeframe', $timeFrame);
49
50
        if ([] === $skillIds) {
51
            // Implode the array into a comma separated string and add to the url.
52
            $this->urlBuilder->addQueryParam('skillIds', $this->arrayToList($skillIds));
53
        }
54
55
        if (null !== $interval) {
56
            $this->urlBuilder->addQueryParam('interval', $interval);
57
        }
58
59
        $this->urlBuilder->build()->getUrl();
60
61
        $this->handle($payload = [], Request::METHOD_GET);
62
    }
63
64
    /**
65
     * Retrieves engagement activity-related metrics at the account, skill, or agent level
66
     * @see https://developers.liveperson.com/data-operational-realtime-engagement-activity.html
67
     *
68
     * @param int $timeFrame The time range in minutes max value is 1440 minutes (24 hours)
69
     * @param array $agents an array of agents to get data for, this can be left empty to return data for all agents
70
     * @param array $skillIds an array of skills to filter by, this can be left empty to get data for all skills.
71
     * @param int|null $interval the interval to filter at.
72
     *
73
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
74
     * @throws \CwsOps\LivePerson\Rest\URLNotBuiltException
75
     */
76
    public function engagementActivity(int $timeFrame = 60, array $agents = [], array $skillIds = [], int $interval = null)
77
    {
78
        if (!$this->isTimeFrameValid($timeFrame)) {
79
            $message = sprintf('The $timeframe must be between 0 and 1440, you passed %d', $timeFrame);
80
            throw new \InvalidArgumentException($message);
81
        }
82
        if (!$this->isIntervalValid($interval)) {
0 ignored issues
show
Bug introduced by
It seems like $interval can also be of type null; however, parameter $timeFrame of CwsOps\LivePerson\Servic...vice::isIntervalValid() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        if (!$this->isIntervalValid(/** @scrutinizer ignore-type */ $interval)) {
Loading history...
83
            $message = sprintf('The $interval you passed was not valid or not dividable by the $timeframe (%d), you passed %d', $timeFrame, $interval);
84
            throw new \InvalidArgumentException($message);
85
        }
86
87
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
88
            ->setService($this->getService())
89
            ->setAccount($this->config->getAccountId())
90
            ->setAction('engactivity')
91
            ->hasQueryParam(true)
92
            ->addQueryParam('timeframe', $timeFrame);
93
94
        if (0 !== count($agents)) {
95
            $this->urlBuilder->addQueryParam('agentIds', $this->arrayToList($agents));
96
        }
97
        if (0 !== count($skillIds)) {
98
            $this->urlBuilder->addQueryParam('skillIds', $this->arrayToList($skillIds));
99
        }
100
        if (null !== $interval) {
101
            $this->urlBuilder->addQueryParam('interval', $interval);
102
        }
103
104
        $payload = [];
105
106
        $this->urlBuilder->build()->getUrl();
107
108
        $this->handle($payload);
109
    }
110
111
112
    /**
113
     * Retrieves the distribution of visitors’ wait time in the queue, before an agent replies to their chat.
114
     * @see https://developers.liveperson.com/data-operational-realtime-sla-histogram.html
115
     *
116
     * @param int $timeFrame the timeframe in minutes to filter the data from.
117
     * @param array $skillIds an optional array of skills to filter the data by.
118
     * @param array $groupIds an optional array of groups filter agents by.
119
     * @param array $histogram an array of histogram values to provide. All values must be multiples of 5.
120
     *
121
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
122
     * @throws \CwsOps\LivePerson\Rest\URLNotBuiltException
123
     */
124
    public function slaHistogram($timeFrame = 60, array $skillIds = [], array $groupIds = [], array $histogram = [])
125
    {
126
        if (!$this->isIntervalValid($timeFrame)) {
127
            throw new \InvalidArgumentException(sprintf('The $timeframe must be between 0 and 1440, you passed %d', $timeFrame));
128
        }
129
130
        // Check if the histogram values are multiples of 5.
131
        if (count($histogram) > 0) {
132
            foreach ($histogram as $value) {
133
                if ($value % 5 != 0) {
134
                    // One or more of the values is not a multiple of 5.
135
                    $message = sprintf('One or more of your histogram values is not a multiple of 5. You passed %s', $this->arrayToList($histogram));
136
137
                    $this->log($message, LogLevel::ERROR, $histogram);
138
139
                    throw new \InvalidArgumentException($message);
140
                }
141
            }
142
        }
143
144
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
145
            ->setService($this->getService())
146
            ->setAccount($this->config->getAccountId())
147
            ->setAction('sla')
148
            ->hasQueryParam(true)
149
            ->addQueryParam('timeframe', $timeFrame);
150
151
        if (0 !== count($skillIds)) {
152
            $this->urlBuilder->addQueryParam('skillIds', $this->arrayToList($skillIds));
153
        }
154
        if (0 !== count($groupIds)) {
155
            $this->urlBuilder->addQueryParam('groupIds', $this->arrayToList($groupIds));
156
        }
157
        if (0 !== count($histogram)) {
158
            $this->urlBuilder->addQueryParam('histogram', $this->arrayToList($histogram));
159
        }
160
161
        $this->urlBuilder->build()->getUrl();
162
163
        $this->handle();
164
    }
165
166
    /**
167
     * Retrieves Agent State Distribution data
168
     * @see https://developers.liveperson.com/data-operational-realtime-agent-activity.html
169
     *
170
     * @param int $timeFrame The time range in minutes max value is 1440 minutes (24 hours)
171
     * @param array $agents an array of agents to get data for, this can be left empty to return data for all agents
172
     * @param int|null $interval $interval the interval to filter at.
173
     *
174
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
175
     * @throws \CwsOps\LivePerson\Rest\URLNotBuiltException
176
     */
177
    public function agentActivity(int $timeFrame = 60, array $agents = [], int $interval = null)
178
    {
179
        if (!$this->isTimeFrameValid($timeFrame)) {
180
            throw new \InvalidArgumentException(sprintf('The $timeframe must be between 0 and 1440, you passed %d', $timeFrame));
181
        }
182
        if (!$this->isIntervalValid($interval)) {
0 ignored issues
show
Bug introduced by
It seems like $interval can also be of type null; however, parameter $timeFrame of CwsOps\LivePerson\Servic...vice::isIntervalValid() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

182
        if (!$this->isIntervalValid(/** @scrutinizer ignore-type */ $interval)) {
Loading history...
183
            throw new \InvalidArgumentException(sprintf('The $interval you passed was not valid or not dividable by the $timeframe (%d), you passed %d', $timeFrame, $interval));
184
        }
185
186
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
187
            ->setService('operations')
188
            ->setAccount($this->config->getAccountId())
189
            ->setAction('agentactivity');
190
191
        // the API requests that if there is a long list of AgentsId, these should be passed in a POST request.
192
        // So  we check here if the agent list is more than 3 and if so we will instead send a post request.
193
        $payLoad = [];
194
195
        if (count($agents) < 3) {
196
            $method = Request::METHOD_GET;
197
            $this->urlBuilder
198
                ->hasQueryParam(true)
199
                ->addQueryParam('timeframe', $timeFrame);
200
201
            if (0 !== count($agents)) {
202
                $this->urlBuilder->addQueryParam('agentIds', rtrim(implode(self::GLUE_CHAR, $agents), self::GLUE_CHAR));
203
            } else {
204
                $this->urlBuilder->addQueryParam('agentsIds', 'all');
205
            }
206
207
            if (null !== $interval) {
208
                $this->urlBuilder->addQueryParam('interval', $interval);
209
            }
210
        } else {
211
            $method = Request::METHOD_POST;
212
            // agents is more than 3, so were going to send a POST request.
213
            $payLoad['timeframe'] = $timeFrame;
214
            $payLoad['agentsIds'] = $this->arrayToList($agents);
215
            if (null !== $interval) {
216
                $payLoad['interval'] = $interval;
217
            }
218
        }
219
220
        $this->urlBuilder->build()->getUrl();
221
222
        $this->handle($payLoad, $method);
223
    }
224
225
226
    /**
227
     * Gets the current Queue State
228
     *
229
     * @see https://developers.liveperson.com/data-operational-realtime-current-queue-state.html
230
     *
231
     * @param array $skillIds an optional array of skills to filter the queue on.
232
     *
233
     * @throws \CwsOps\LivePerson\Rest\BuilderLockedException
234
     * @throws \CwsOps\LivePerson\Rest\URLNotBuiltException
235
     */
236
    public function currentQueueState(array $skillIds = [])
237
    {
238
        $this->urlBuilder = $this->request->buildUrl($this->getDomain())
239
            ->setService($this->getService())
240
            ->setAccount($this->config->getAccountId())
241
            ->setAction('queuestate');
242
243
        if (0 !== count($skillIds)) {
244
            $this->urlBuilder->hasQueryParam(true);
245
            $this->urlBuilder->addQueryParam('skillIds', $this->arrayToList($skillIds));
246
        }
247
248
        $this->urlBuilder->build()->getUrl();
249
250
        $this->handle();
251
    }
252
253
254
    /**
255
     * Should provide the Live person service, this service will query against
256
     *
257
     * @return string
258
     */
259
    protected function getDomain(): string
260
    {
261
        return 'leDataReporting';
262
    }
263
264
265
    /**
266
     * Should provide the Live Person service the service will query against.
267
     *
268
     * @return string
269
     */
270
    protected function getService(): string
271
    {
272
        return 'operations';
273
    }
274
275
    /**
276
     * Checks if the timeframe is valid.
277
     * i.e. the time is only valid between 0 and 1440 minutes (0 hrs to 24hrs).
278
     *
279
     * @param int $timeFrame the number of minutes.
280
     *
281
     * @return bool true or false if the timeframe is valid.
282
     */
283
    private function isTimeFrameValid(int $timeFrame): bool
284
    {
285
        if ($timeFrame >= 0 && $timeFrame <= 1440) {
286
            return true;
287
        }
288
        return false;
289
    }
290
291
    /**
292
     * Checks if the interval is valid.
293
     * The interval must be less than the time frame and must be dividable by the timeframe.
294
     *
295
     * @param int $timeFrame the timeframe value.
296
     * @param int|null $interval the interval to check.
297
     *
298
     * @return bool true or false if the interval is valid.
299
     */
300
    private function isIntervalValid(int $timeFrame, int $interval = null): bool
301
    {
302
        if (null === $interval || $timeFrame < $interval && $interval % $timeFrame === 0) {
303
            return true;
304
        }
305
        return false;
306
    }
307
}
308