Completed
Pull Request — master (#140)
by
unknown
02:19
created

Nlp   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 17
eloc 27
c 1
b 0
f 1
dl 0
loc 85
ccs 34
cts 35
cp 0.9714
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedConfigKeys() 0 8 1
A isString() 0 6 3
A isValidConfigs() 0 12 4
A isValidNBest() 0 4 5
A isBool() 0 6 3
A config() 0 8 1
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
    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
    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
    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