|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Api; |
|
6
|
|
|
|
|
7
|
|
|
use Kerox\Messenger\InsightsInterface; |
|
8
|
|
|
use Kerox\Messenger\Request\InsightsRequest; |
|
9
|
|
|
use Kerox\Messenger\Response\InsightsResponse; |
|
10
|
|
|
|
|
11
|
|
|
class Insights extends AbstractApi implements InsightsInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param array $metrics |
|
15
|
|
|
* @param null|int $since |
|
16
|
|
|
* @param null|int $until |
|
17
|
|
|
* |
|
18
|
|
|
* @return \Kerox\Messenger\Response\InsightsResponse |
|
19
|
|
|
*/ |
|
20
|
|
|
public function get(array $metrics = [], ?int $since = null, ?int $until = null): InsightsResponse |
|
21
|
|
|
{ |
|
22
|
3 |
|
$metrics = $this->isValidMetrics($metrics); |
|
23
|
|
|
|
|
24
|
3 |
|
$request = new InsightsRequest($this->pageToken, $metrics, $since, $until); |
|
25
|
3 |
|
$response = $this->client->get('me/insights', $request->build()); |
|
26
|
|
|
|
|
27
|
|
|
return new InsightsResponse($response); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $metrics |
|
32
|
|
|
* |
|
33
|
1 |
|
* @throws \InvalidArgumentException |
|
34
|
|
|
* |
|
35
|
1 |
|
* @return array |
|
36
|
1 |
|
*/ |
|
37
|
|
|
private function isValidMetrics(array $metrics): array |
|
38
|
|
|
{ |
|
39
|
1 |
|
$allowedMetrics = $this->getAllowedMetrics(); |
|
40
|
|
|
|
|
41
|
|
|
$metrics = empty($metrics) ? $allowedMetrics : $metrics; |
|
42
|
|
|
if ($metrics !== $allowedMetrics) { |
|
43
|
|
|
array_map(function ($metric) use ($allowedMetrics): void { |
|
44
|
|
|
if (!\in_array($metric, $allowedMetrics, true)) { |
|
45
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
46
|
|
|
'%s is not a valid value. $metrics must only contain %s', |
|
47
|
1 |
|
$metric, |
|
48
|
1 |
|
implode(', ', $allowedMetrics) |
|
49
|
|
|
)); |
|
50
|
1 |
|
} |
|
51
|
|
|
}, $metrics); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $metrics; |
|
55
|
|
|
} |
|
56
|
1 |
|
|
|
57
|
|
|
/** |
|
58
|
1 |
|
* @return array |
|
59
|
1 |
|
*/ |
|
60
|
|
|
private function getAllowedMetrics(): array |
|
61
|
1 |
|
{ |
|
62
|
|
|
return [ |
|
63
|
|
|
self::ACTIVE_THREAD_UNIQUE, |
|
64
|
|
|
self::BLOCKED_CONVERSATIONS_UNIQUE, |
|
65
|
|
|
self::REPORTED_CONVERSATIONS_UNIQUE, |
|
66
|
|
|
self::REPORTED_CONVERSATIONS_BY_REPORT_TYPE_UNIQUE, |
|
67
|
|
|
self::FEEDBACK_BY_ACTION_UNIQUE, |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|