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

NlpRequest::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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