Completed
Pull Request — master (#85)
by Romain
03:54
created

Nlp::getAllowedConfigKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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
                if (($key === NlpInterface::CONFIG_KEY_NLP_ENABLED || $key === NlpInterface::CONFIG_KEY_VERBOSE) && !\is_bool($value)) {
67
                    throw new \InvalidArgumentException($key . ' must be a boolean');
68
                }
69
70
                if (($key === NlpInterface::CONFIG_KEY_CUSTOM_TOKEN || $key === NlpInterface::CONFIG_KEY_MODEL) && !\is_string($value)) {
71
                    throw new \InvalidArgumentException($key . ' must be a string');
72
                }
73
74
                if ($key === NlpInterface::CONFIG_KEY_N_BEST && (!\is_int($value) || $value < 1 || $value > 8)) {
75
                    throw new \InvalidArgumentException($key . ' must be an integer between 1 and 8');
76
                }
77
            }
78
        }
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    private function getAllowedConfigKeys(): array
85
    {
86
        return [
87
            NlpInterface::CONFIG_KEY_NLP_ENABLED,
88
            NlpInterface::CONFIG_KEY_MODEL,
89
            NlpInterface::CONFIG_KEY_CUSTOM_TOKEN,
90
            NlpInterface::CONFIG_KEY_VERBOSE,
91
            NlpInterface::CONFIG_KEY_N_BEST,
92
        ];
93
    }
94
}
95