Completed
Push — master ( 4c1222...393226 )
by Romain
10s
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 Kerox\Messenger\NlpInterface;
8
use Kerox\Messenger\Request\NlpRequest;
9
use Kerox\Messenger\Response\NlpResponse;
10
11
class Nlp extends AbstractApi implements NlpInterface
12
{
13
    /**
14
     * @param array $configs
15
     *
16
     * @throws \InvalidArgumentException
17
     *
18
     * @return \Kerox\Messenger\Response\NlpResponse
19
     */
20
    public function config(array $configs = []): NlpResponse
21
    {
22
        $this->isValidConfigs($configs);
23
24
        $request = new NlpRequest($this->pageToken, $configs);
25
        $response = $this->client->post('me/nlp_configs', $request->build());
26
27
        return new NlpResponse($response);
28
    }
29
30
    /**
31
     * @param array $configs
32
     *
33
     * @throws \InvalidArgumentException
34
     */
35
    private function isValidConfigs(array $configs): void
36
    {
37
        $allowedConfigKeys = $this->getAllowedConfigKeys();
38
        if (!empty($configs)) {
39
            foreach ($configs as $key => $value) {
40
                if (!\in_array($key, $allowedConfigKeys, true)) {
41
                    throw new \InvalidArgumentException($key . ' is not a valid key. $configs must only contain ' . implode(', ', $allowedConfigKeys));
42
                }
43
44
                $this->isBool($key, $value);
45
                $this->isString($key, $value);
46
                $this->isValidNBest($key, $value);
47
            }
48
        }
49
    }
50
51
    /**
52
     * @param string $key
53
     * @param mixed  $value
54
     *
55
     * @throws \InvalidArgumentException
56
     */
57
    private function isBool(string $key, $value): void
58
    {
59
        if (!\is_bool($value) && \in_array($key, [NlpInterface::CONFIG_KEY_NLP_ENABLED, NlpInterface::CONFIG_KEY_VERBOSE], true)) {
60
            throw new \InvalidArgumentException($key . ' must be a boolean');
61
        }
62
    }
63
64
    /**
65
     * @param string $key
66
     * @param mixed  $value
67
     *
68
     * @throws \InvalidArgumentException
69
     */
70
    private function isString(string $key, $value): void
71
    {
72
        if (!\is_string($value) && \in_array($key, [NlpInterface::CONFIG_KEY_CUSTOM_TOKEN, NlpInterface::CONFIG_KEY_MODEL], true)) {
73
            throw new \InvalidArgumentException($key . ' must be a string');
74
        }
75
    }
76
77
    /**
78
     * @param string $key
79
     * @param mixed  $value
80
     *
81
     * @throws \InvalidArgumentException
82
     */
83
    private function isValidNBest(string $key, $value): void
84
    {
85
        if ($key === NlpInterface::CONFIG_KEY_N_BEST && (!\is_int($value) || $value < 1 || $value > 8)) {
86
            throw new \InvalidArgumentException($key . ' must be an integer between 1 and 8');
87
        }
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    private function getAllowedConfigKeys(): array
94
    {
95
        return [
96
            NlpInterface::CONFIG_KEY_NLP_ENABLED,
97
            NlpInterface::CONFIG_KEY_MODEL,
98
            NlpInterface::CONFIG_KEY_CUSTOM_TOKEN,
99
            NlpInterface::CONFIG_KEY_VERBOSE,
100
            NlpInterface::CONFIG_KEY_N_BEST,
101
        ];
102
    }
103
}
104