Completed
Push — master ( 4c1222...393226 )
by Romain
10s
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 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
     *
16
     * @throws \InvalidArgumentException
17
     *
18
     * @return \Kerox\Messenger\Response\InsightsResponse
19
     */
20
    public function get(array $metrics = []): InsightsResponse
21
    {
22 3
        $allowedMetrics = $this->getAllowedMetrics();
23
        if (!empty($allowedMetrics)) {
24 3
            foreach ($metrics as $metric) {
25 3
                if (!\in_array($metric, $allowedMetrics, true)) {
26
                    throw new \InvalidArgumentException($metric . ' is not a valid value. $metrics must only contain ' . implode(', ', $allowedMetrics));
27
                }
28
            }
29
        } else {
30
            $metrics = $allowedMetrics;
31
        }
32
33 1
        $request = new InsightsRequest($this->pageToken, $metrics);
34
        $response = $this->client->get('me/insights', $request->build());
35 1
36 1
        return new InsightsResponse($response);
37
    }
38
39 1
    /**
40
     * @return array
41
     */
42
    private function getAllowedMetrics(): array
43
    {
44
        return [
45 1
            InsightsInterface::ACTIVE_THREAD_UNIQUE,
46
            InsightsInterface::BLOCKED_CONVERSATIONS_UNIQUE,
47 1
            InsightsInterface::REPORTED_CONVERSATIONS_UNIQUE,
48 1
            InsightsInterface::REPORTED_CONVERSATIONS_BY_REPORT_TYPE_UNIQUE,
49
            InsightsInterface::FEEDBACK_BY_ACTION_UNIQUE,
50 1
        ];
51
    }
52
}
53