Passed
Pull Request — master (#127)
by Alexandr
02:03
created

BroadcastRequest::buildHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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