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