Passed
Pull Request — master (#91)
by Romain
02:54
created

Insights::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 1
cts 1
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Api;
6
7
use GuzzleHttp\ClientInterface;
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
     * @param array $metrics
16
     *
17
     * @throws \InvalidArgumentException
18
     *
19
     * @return \Kerox\Messenger\Response\InsightsResponse
20
     */
21
    public function get(array $metrics = []): InsightsResponse
22 3
    {
23
        $allowedMetrics = $this->getAllowedMetrics();
24 3
        if (!empty($allowedMetrics)) {
25 3
            foreach ($metrics as $metric) {
26
                if (!\in_array($metric, $allowedMetrics, true)) {
27
                    throw new \InvalidArgumentException($metric . ' is not a valid value. $metrics must only contain ' . implode(', ', $allowedMetrics));
28
                }
29
            }
30
        } else {
31
            $metrics = $allowedMetrics;
32
        }
33 1
34
        $request = new InsightsRequest($this->pageToken, $metrics);
35 1
        $response = $this->client->get('me/insights', $request->build());
36 1
37
        return new InsightsResponse($response);
38
    }
39 1
40
    /**
41
     * @return array
42
     */
43
    private function getAllowedMetrics(): array
44
    {
45 1
        return [
46
            InsightsInterface::ACTIVE_THREAD_UNIQUE,
47 1
            InsightsInterface::BLOCKED_CONVERSATIONS_UNIQUE,
48 1
            InsightsInterface::REPORTED_CONVERSATIONS_UNIQUE,
49
            InsightsInterface::REPORTED_CONVERSATIONS_BY_REPORT_TYPE_UNIQUE,
50 1
            InsightsInterface::FEEDBACK_BY_ACTION_UNIQUE,
51
        ];
52
    }
53
}
54