Completed
Pull Request — master (#127)
by Alexandr
04:00
created

NlpRequest::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 3
cts 3
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 1
     * @param array  $configs
25
     */
26 1
    public function __construct(string $path, array $configs)
27
    {
28 1
        parent::__construct($path);
29 1
30
        $this->configs = $configs;
31 1
    }
32
33 1
    /**
34
     * @param string|null $method
35 1
     *
36
     * @return RequestInterface
37 1
     */
38
    public function build(?string $method = null): RequestInterface
39
    {
40
        $uri = Uri::fromParts([
41
            'query' => $this->buildQuery(),
42 1
        ]);
43
44 1
        return $this->origin
45
            ->withMethod('post')
46 1
            ->withUri($uri);
47 1
    }
48 1
49 1
    /**
50 1
     * @return string
51
     */
52
    public function buildQuery(): string
53 1
    {
54
        $query = [
55
            'nlp_enabled' => $this->configs['nlp_enabled'] ?? null,
56
            'model' => $this->configs['model'] ?? null,
57
            'custom_token' => $this->configs['custom_token'] ?? null,
58
            'verbose' => $this->configs['verbose'] ?? null,
59
            'n_best' => $this->configs['n_best'] ?? null,
60
        ];
61
62
        return http_build_query($this->arrayFilter($query));
63
    }
64
}
65