Completed
Pull Request — master (#127)
by Alexandr
05:53
created

NlpRequest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 18
dl 0
loc 53
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A buildQuery() 0 11 1
A build() 0 10 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Request;
6
7
use GuzzleHttp\Psr7\Uri;
8
use Kerox\Messenger\Helper\UtilityTrait;
9
use Psr\Http\Message\RequestInterface;
10
11
class NlpRequest extends AbstractRequest implements QueryRequestInterface
12
{
13
    use UtilityTrait;
14
15
    /**
16
     * @var array
17
     */
18
    protected $configs;
19
20
    /**
21
     * CodeRequest constructor.
22
     *
23
     * @param string $path
24
     * @param array  $configs
25
     */
26 2
    public function __construct(string $path, array $configs)
27
    {
28 2
        parent::__construct($path);
29
30 2
        $this->configs = $configs;
31 2
    }
32
33
    /**
34
     * @param string|null $method
35
     *
36
     * @return RequestInterface
37
     */
38 2
    public function build(string $method = 'post'): RequestInterface
39
    {
40 2
        $uri = Uri::fromParts([
41 2
            'path' => $this->origin->getUri()->getPath(),
42 2
            'query' => $this->buildQuery(),
43
        ]);
44
45 2
        return $this->origin
46 2
            ->withMethod($method)
47 2
            ->withUri($uri);
48
    }
49
50
    /**
51
     * @return string
52
     */
53 2
    public function buildQuery(): string
54
    {
55
        $query = [
56 2
            'nlp_enabled' => $this->configs['nlp_enabled'] ?? null,
57 2
            'model' => $this->configs['model'] ?? null,
58 2
            'custom_token' => $this->configs['custom_token'] ?? null,
59 2
            'verbose' => $this->configs['verbose'] ?? null,
60 2
            'n_best' => $this->configs['n_best'] ?? null,
61
        ];
62
63 2
        return http_build_query($this->arrayFilter($query));
64
    }
65
}
66