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

CodeRequest::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
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 Kerox\Messenger\Helper\UtilityTrait;
8
use Psr\Http\Message\RequestInterface;
9
use function GuzzleHttp\Psr7\stream_for;
10
11
class CodeRequest extends AbstractRequest implements BodyRequestInterface
12
{
13
    use UtilityTrait;
14
15
    /**
16
     * @var int
17
     */
18
    protected $imageSize;
19
20
    /**
21
     * @var string
22
     */
23
    protected $codeType;
24
25
    /**
26
     * @var null|string
27
     */
28
    protected $ref;
29
30
    /**
31
     * CodeRequest constructor.
32
     *
33
     * @param string      $path
34
     * @param int         $imageSize
35
     * @param string      $codeType
36
     * @param null|string $ref
37
     */
38 3
    public function __construct(string $path, int $imageSize, string $codeType, ?string $ref = null)
39
    {
40 3
        parent::__construct($path);
41
42 3
        $this->imageSize = $imageSize;
43 3
        $this->codeType = $codeType;
44 3
        $this->ref = $ref;
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
        return $this->origin
55 3
            ->withMethod($method)
56 3
            ->withBody(stream_for($this->buildBody()));
57
    }
58
59
    /**
60
     * @return string
61
     */
62 3
    public function buildBody(): string
63
    {
64
        $body = [
65 3
            'type' => $this->codeType,
66 3
            'image_size' => $this->imageSize,
67
            'data' => [
68 3
                'ref' => $this->ref,
69
            ],
70
        ];
71
72 3
        return \json_encode($this->arrayFilter($body));
73
    }
74
}
75