Completed
Pull Request — master (#85)
by Romain
06:09
created

Nlp::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

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 4
nc 1
nop 1
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->isValidKeyValue($key, $value);
67
            }
68
        }
69
    }
70
71
    /**
72
     * @param string $key
73
     * @param mixed  $value
74
     *
75
     * @throws \InvalidArgumentException
76
     */
77
    private function isValidKeyValue(string $key, $value): void
78
    {
79
        if (($key === NlpInterface::CONFIG_KEY_NLP_ENABLED || $key === NlpInterface::CONFIG_KEY_VERBOSE) && !\is_bool($value)) {
80
            throw new \InvalidArgumentException($key . ' must be a boolean');
81
        }
82
83
        if (($key === NlpInterface::CONFIG_KEY_CUSTOM_TOKEN || $key === NlpInterface::CONFIG_KEY_MODEL) && !\is_string($value)) {
84
            throw new \InvalidArgumentException($key . ' must be a string');
85
        }
86
87
        if ($key === NlpInterface::CONFIG_KEY_N_BEST && (!\is_int($value) || $value < 1 || $value > 8)) {
88
            throw new \InvalidArgumentException($key . ' must be an integer between 1 and 8');
89
        }
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    private function getAllowedConfigKeys(): array
96
    {
97
        return [
98
            NlpInterface::CONFIG_KEY_NLP_ENABLED,
99
            NlpInterface::CONFIG_KEY_MODEL,
100
            NlpInterface::CONFIG_KEY_CUSTOM_TOKEN,
101
            NlpInterface::CONFIG_KEY_VERBOSE,
102
            NlpInterface::CONFIG_KEY_N_BEST,
103
        ];
104
    }
105
}
106