Completed
Push — master ( eb575e...c103e9 )
by Romain
13s
created

Insights::get()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 3
nop 1
crap 4
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
     * @var null|\Kerox\Messenger\Api\Insights
16
     */
17
    private static $_instance;
18
19
    /**
20
     * @param string                      $pageToken
21
     * @param \GuzzleHttp\ClientInterface $client
22 3
     *
23
     * @return \Kerox\Messenger\Api\Insights
24 3
     */
25 3
    public static function getInstance(string $pageToken, ClientInterface $client): self
26
    {
27
        if (self::$_instance === null) {
28
            self::$_instance = new self($pageToken, $client);
29
        }
30
31
        return self::$_instance;
32
    }
33 1
34
    /**
35 1
     * @param array $metrics
36 1
     *
37
     * @throws \InvalidArgumentException
38
     *
39 1
     * @return \Kerox\Messenger\Response\InsightsResponse
40
     */
41
    public function get(array $metrics = []): InsightsResponse
42
    {
43
        $allowedMetrics = $this->getAllowedMetrics();
44
        if (!empty($allowedMetrics)) {
45 1
            foreach ($metrics as $metric) {
46
                if (!\in_array($metric, $allowedMetrics, true)) {
47 1
                    throw new \InvalidArgumentException($metric . ' is not a valid value. $metrics must only contain ' . implode(', ', $allowedMetrics));
48 1
                }
49
            }
50 1
        } else {
51
            $metrics = $allowedMetrics;
52
        }
53
54
        $request = new InsightsRequest($this->pageToken, $metrics);
55
        $response = $this->client->get('me/insights', $request->build());
56 1
57
        return new InsightsResponse($response);
58 1
    }
59 1
60
    /**
61 1
     * @return array
62
     */
63
    private function getAllowedMetrics(): array
64
    {
65
        return [
66
            InsightsInterface::ACTIVE_THREAD_UNIQUE,
67
            InsightsInterface::BLOCKED_CONVERSATIONS_UNIQUE,
68
            InsightsInterface::REPORTED_CONVERSATIONS_UNIQUE,
69
            InsightsInterface::REPORTED_CONVERSATIONS_BY_REPORT_TYPE_UNIQUE,
70
            InsightsInterface::FEEDBACK_BY_ACTION_UNIQUE,
71
        ];
72
    }
73
}
74