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

InsightsRequest::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 5
cts 5
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 InsightsRequest extends AbstractRequest implements QueryRequestInterface
12
{
13
    use UtilityTrait;
14
15
    /**
16
     * @var array
17
     */
18
    protected $metrics;
19
20
    /**
21
     * @var null|int
22
     */
23
    protected $since;
24
25
    /**
26
     * @var null|int
27
     */
28
    protected $until;
29
30
    /**
31
     * InsightsRequest constructor.
32
     *
33
     * @param string   $path
34
     * @param array    $metrics
35
     * @param null|int $since
36
     * @param null|int $until
37
     */
38 3
    public function __construct(string $path, array $metrics, ?int $since = null, ?int $until = null)
39
    {
40 3
        parent::__construct($path);
41
42 3
        $this->metrics = $metrics;
43 3
        $this->since = $since;
44 3
        $this->until = $until;
45 3
    }
46
47
    /**
48
     * @param string $method
49
     *
50
     * @return RequestInterface
51
     */
52 3
    public function build(string $method = 'post'): RequestInterface
53
    {
54 3
        $uri = Uri::fromParts([
55 3
            'path' => $this->origin->getUri()->getPath(),
56 3
            'query' => $this->buildQuery(),
57
        ]);
58
59 3
        return $this->origin->withUri($uri);
60
    }
61
62
    /**
63
     * @return string
64
     */
65 3
    public function buildQuery(): string
66
    {
67
        $query = [
68 3
            'metric' => implode(',', $this->metrics),
69 3
            'since' => $this->since,
70 3
            'until' => $this->until,
71
        ];
72
73 3
        return http_build_query($this->arrayFilter($query));
74
    }
75
}
76