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

CodeRequest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 17
dl 0
loc 62
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A build() 0 5 1
A buildBody() 0 11 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