Passed
Pull Request — master (#94)
by Romain
01:30
created

Insights::get()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 5
eloc 10
nc 6
nop 3
crap 5
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