1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Api; |
6
|
|
|
|
7
|
|
|
use Kerox\Messenger\Exception\InvalidKeyException; |
8
|
|
|
use Kerox\Messenger\Exception\InvalidTypeException; |
9
|
|
|
use Kerox\Messenger\NlpInterface; |
10
|
|
|
use Kerox\Messenger\Request\NlpRequest; |
11
|
|
|
use Kerox\Messenger\Response\NlpResponse; |
12
|
|
|
|
13
|
|
|
class Nlp extends AbstractApi implements NlpInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
17
|
|
|
*/ |
18
|
5 |
|
public function config(array $configs = []): NlpResponse |
19
|
|
|
{ |
20
|
5 |
|
$this->isValidConfigs($configs); |
21
|
|
|
|
22
|
1 |
|
$request = new NlpRequest($this->pageToken, $configs); |
23
|
1 |
|
$response = $this->client->post('me/nlp_configs', $request->build()); |
24
|
|
|
|
25
|
1 |
|
return new NlpResponse($response); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
30
|
|
|
*/ |
31
|
5 |
|
private function isValidConfigs(array $configs): void |
32
|
|
|
{ |
33
|
5 |
|
$allowedConfigKeys = $this->getAllowedConfigKeys(); |
34
|
5 |
|
if (!empty($configs)) { |
35
|
5 |
|
foreach ($configs as $key => $value) { |
36
|
5 |
|
if (!\in_array($key, $allowedConfigKeys, true)) { |
37
|
1 |
|
throw new InvalidKeyException(sprintf('%s is not a valid key. configs must only contain "%s".', $key, implode(', ', $allowedConfigKeys))); |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
$this->isBool($key, $value); |
41
|
3 |
|
$this->isString($key, $value); |
42
|
3 |
|
$this->isValidNBest($key, $value); |
43
|
|
|
} |
44
|
|
|
} |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param int|string $key |
49
|
|
|
* @param mixed $value |
50
|
|
|
* |
51
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
52
|
|
|
*/ |
53
|
4 |
View Code Duplication |
private function isBool($key, $value): void |
|
|
|
|
54
|
|
|
{ |
55
|
4 |
|
if (!\is_bool($value) && |
56
|
4 |
|
\in_array($key, [self::CONFIG_KEY_NLP_ENABLED, self::CONFIG_KEY_VERBOSE], true) |
57
|
|
|
) { |
58
|
2 |
|
throw new InvalidTypeException(sprintf('%s must be a boolean.', $key)); |
59
|
|
|
} |
60
|
3 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param int|string $key |
64
|
|
|
* @param mixed $value |
65
|
|
|
* |
66
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
67
|
|
|
*/ |
68
|
3 |
View Code Duplication |
private function isString($key, $value): void |
|
|
|
|
69
|
|
|
{ |
70
|
3 |
|
if (!\is_string($value) && |
71
|
3 |
|
\in_array($key, [self::CONFIG_KEY_CUSTOM_TOKEN, self::CONFIG_KEY_MODEL], true) |
72
|
|
|
) { |
73
|
|
|
throw new InvalidTypeException(sprintf('%s must be a string.', $key)); |
74
|
|
|
} |
75
|
3 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param int|string $key |
79
|
|
|
* @param mixed $value |
80
|
|
|
* |
81
|
|
|
* @throws \Kerox\Messenger\Exception\MessengerException |
82
|
|
|
*/ |
83
|
3 |
|
private function isValidNBest($key, $value): void |
84
|
|
|
{ |
85
|
3 |
|
if ($key === self::CONFIG_KEY_N_BEST && (!\is_int($value) || $value < 1 || $value > 8)) { |
86
|
1 |
|
throw new InvalidTypeException(sprintf('%s must be an integer between 1 and 8.', $key)); |
87
|
|
|
} |
88
|
3 |
|
} |
89
|
|
|
|
90
|
5 |
View Code Duplication |
private function getAllowedConfigKeys(): array |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
5 |
|
self::CONFIG_KEY_NLP_ENABLED, |
94
|
5 |
|
self::CONFIG_KEY_MODEL, |
95
|
5 |
|
self::CONFIG_KEY_CUSTOM_TOKEN, |
96
|
5 |
|
self::CONFIG_KEY_VERBOSE, |
97
|
5 |
|
self::CONFIG_KEY_N_BEST, |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.