Completed
Push — master ( 4f4939...8e0ded )
by Romain
14s
created

Nlp::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Api;
6
7
use GuzzleHttp\ClientInterface;
8
use Kerox\Messenger\NlpInterface;
9
use Kerox\Messenger\Request\NlpRequest;
10
use Kerox\Messenger\Response\NlpResponse;
11
12
class Nlp extends AbstractApi implements NlpInterface
13
{
14
    /**
15
     * @var null|\Kerox\Messenger\Api\Nlp
16
     */
17
    private static $_instance;
18
19
    /**
20
     * @param string                      $pageToken
21
     * @param \GuzzleHttp\ClientInterface $client
22
     *
23
     * @return \Kerox\Messenger\Api\Nlp
24
     */
25
    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
34
    /**
35
     * @param array $configs
36
     *
37
     * @throws \InvalidArgumentException
38
     *
39
     * @return \Kerox\Messenger\Response\NlpResponse
40
     */
41
    public function config(array $configs = []): NlpResponse
42
    {
43
        $this->isValidConfigs($configs);
44
45
        $request = new NlpRequest($this->pageToken, $configs);
46
        $response = $this->client->post('me/nlp_configs', $request->build());
47
48
        return new NlpResponse($response);
49
    }
50
51
    /**
52
     * @param array $configs
53
     *
54
     * @throws \InvalidArgumentException
55
     */
56
    private function isValidConfigs(array $configs): void
57
    {
58
        $allowedConfigKeys = $this->getAllowedConfigKeys();
59
        if (!empty($configs)) {
60
            foreach ($configs as $key => $value) {
61
                if (!\in_array($key, $allowedConfigKeys, true)) {
62
                    throw new \InvalidArgumentException($key . ' is not a valid key. $configs must only contain ' . implode(', ',
63
                            $allowedConfigKeys));
64
                }
65
66
                $this->isBool($key, $value);
67
                $this->isString($key, $value);
68
                $this->isValidNBest($key, $value);
69
            }
70
        }
71
    }
72
73
    /**
74
     * @param string $key
75
     * @param mixed  $value
76
     *
77
     * @throws \InvalidArgumentException
78
     */
79
    private function isBool(string $key, $value): void
80
    {
81
        if (!\is_bool($value) && \in_array($key, [NlpInterface::CONFIG_KEY_NLP_ENABLED, NlpInterface::CONFIG_KEY_VERBOSE], true)) {
82
            throw new \InvalidArgumentException($key . ' must be a boolean');
83
        }
84
    }
85
86
    /**
87
     * @param string $key
88
     * @param mixed  $value
89
     *
90
     * @throws \InvalidArgumentException
91
     */
92
    private function isString(string $key, $value): void
93
    {
94
        if (!\is_string($value) && \in_array($key, [NlpInterface::CONFIG_KEY_CUSTOM_TOKEN, NlpInterface::CONFIG_KEY_MODEL], true)) {
95
            throw new \InvalidArgumentException($key . ' must be a string');
96
        }
97
    }
98
99
    /**
100
     * @param string $key
101
     * @param mixed  $value
102
     *
103
     * @throws \InvalidArgumentException
104
     */
105
    private function isValidNBest(string $key, $value): void
106
    {
107
        if ($key === NlpInterface::CONFIG_KEY_N_BEST && (!\is_int($value) || $value < 1 || $value > 8)) {
108
            throw new \InvalidArgumentException($key . ' must be an integer between 1 and 8');
109
        }
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    private function getAllowedConfigKeys(): array
116
    {
117
        return [
118
            NlpInterface::CONFIG_KEY_NLP_ENABLED,
119
            NlpInterface::CONFIG_KEY_MODEL,
120
            NlpInterface::CONFIG_KEY_CUSTOM_TOKEN,
121
            NlpInterface::CONFIG_KEY_VERBOSE,
122
            NlpInterface::CONFIG_KEY_N_BEST,
123
        ];
124
    }
125
}
126