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