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

BroadcastRequest::buildBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Kerox\Messenger\Model\Message;
9
use Kerox\Messenger\SendInterface;
10
use Psr\Http\Message\RequestInterface;
11
use function GuzzleHttp\Psr7\stream_for;
12
13
class BroadcastRequest extends AbstractRequest implements BodyRequestInterface
14
{
15
    use UtilityTrait;
16
17
    public const REQUEST_TYPE_MESSAGE = 'message';
18
    public const REQUEST_TYPE_ACTION = 'action';
19
20
    /**
21
     * @var null|Message
22
     */
23
    protected $message;
24
25
    /**
26
     * @var null|string
27
     */
28
    protected $messageCreativeId;
29
30
    /**
31
     * @var null|string
32
     */
33
    protected $notificationType;
34
35
    /**
36
     * @var null|string
37
     */
38
    protected $tag;
39
40
    /**
41
     * @var string|null
42
     */
43
    protected $messagingType;
44
45
    /**
46
     * Request constructor.
47
     *
48
     * @param string       $path
49
     * @param Message|null $message
50
     * @param string|null  $messageCreativeId
51
     * @param array        $options
52
     */
53 4
    public function __construct(
54
        string $path,
55
        ?Message $message = null,
56
        ?string $messageCreativeId = null,
57
        array $options = []
58
    ) {
59 4
        parent::__construct($path);
60
61 4
        $this->message = $message;
62 4
        $this->messageCreativeId = $messageCreativeId;
63 4
        $this->messagingType = $options[SendInterface::OPTION_MESSAGING_TYPE] ?? null;
64 4
        $this->notificationType = $options[SendInterface::OPTION_NOTIFICATION_TYPE] ?? null;
65 4
        $this->tag = $options[SendInterface::OPTION_TAG] ?? null;
66 4
    }
67
68
    /**
69
     * @param string $method
70
     *
71
     * @return RequestInterface
72
     */
73 4
    public function build(string $method = 'post'): RequestInterface
74
    {
75 4
        return $this->origin
76 4
            ->withMethod($method)
77 4
            ->withBody(stream_for($this->buildBody()));
78
    }
79
80
    /**
81
     * @return string
82
     */
83 4
    public function buildBody(): string
84
    {
85
        $body = [
86
            'messages' => [
87 4
                $this->message,
88
            ],
89 4
            'message_creative_id' => $this->messageCreativeId,
90 4
            'messaging_type' => $this->messagingType,
91 4
            'notification_type' => $this->notificationType,
92 4
            'tag' => $this->tag,
93
        ];
94
95 4
        return \json_encode($this->arrayFilter($body));
96
    }
97
}
98