Completed
Push — master ( f49a8d...3fb39c )
by Romain
10s
created

Insights   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 41
rs 10
c 0
b 0
f 0
ccs 11
cts 11
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B get() 0 19 5
A getAllowedMetrics() 0 8 1
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
        $allowedMetrics = $this->getAllowedMetrics();
23
        $metrics = empty($metrics) ? $allowedMetrics : $metrics;
24 3
25 3
        if ($metrics !== $allowedMetrics) {
26
            foreach ($metrics as $metric) {
27
                if (!\in_array($metric, $allowedMetrics, true)) {
28
                    throw new \InvalidArgumentException(
29
                        $metric . ' is not a valid value. $metrics must only contain ' . implode(', ', $allowedMetrics)
30
                    );
31
                }
32
            }
33 1
        }
34
35 1
        $request = new InsightsRequest($this->pageToken, $metrics, $since, $until);
36 1
        $response = $this->client->get('me/insights', $request->build());
37
38
        return new InsightsResponse($response);
39 1
    }
40
41
    /**
42
     * @return array
43
     */
44
    private function getAllowedMetrics(): array
45 1
    {
46
        return [
47 1
            self::ACTIVE_THREAD_UNIQUE,
48 1
            self::BLOCKED_CONVERSATIONS_UNIQUE,
49
            self::REPORTED_CONVERSATIONS_UNIQUE,
50 1
            self::REPORTED_CONVERSATIONS_BY_REPORT_TYPE_UNIQUE,
51
            self::FEEDBACK_BY_ACTION_UNIQUE,
52
        ];
53
    }
54
}
55