Passed
Pull Request — master (#123)
by Romain
02:12
created

Nlp   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 17
eloc 30
dl 0
loc 98
c 0
b 0
f 0
ccs 37
cts 38
cp 0.9737
rs 10

6 Methods

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