Completed
Pull Request — master (#127)
by Alexandr
04:00
created

SendRequest::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 SendRequest extends AbstractRequest implements BodyRequestInterface
13
{
14
    public const REQUEST_TYPE_MESSAGE = 'message';
15
    public const REQUEST_TYPE_ACTION = 'action';
16
17
    /**
18
     * @var null|array
19
     */
20
    protected $recipient;
21
22
    /**
23
     * @var null|string|\Kerox\Messenger\Model\Message
24
     */
25
    protected $message;
26
27
    /**
28
     * @var null|string
29
     */
30
    protected $senderAction;
31
32
    /**
33
     * @var null|string
34
     */
35
    protected $notificationType;
36
37
    /**
38
     * @var null|string
39
     */
40
    protected $tag;
41
42
    /**
43
     * @var null|string
44
     */
45
    protected $personaId;
46
47
    /**
48
     * @var string
49
     */
50
    protected $messagingType;
51
52
    /**
53
     * Request constructor.
54
     *
55
     * @param string                                $pageToken
56
     * @param string|\Kerox\Messenger\Model\Message $content
57
     * @param string|null                           $recipient
58
     * @param array                                 $options
59 6
     * @param string                                $requestType
60
     */
61
    public function __construct(
62
        string $pageToken,
63
        $content,
64
        ?string $recipient = null,
65
        array $options = [],
66 6
        string $requestType = self::REQUEST_TYPE_MESSAGE
67
    ) {
68 6
        parent::__construct($pageToken);
69 5
70
        if ($content instanceof Message || $requestType === self::REQUEST_TYPE_MESSAGE) {
71 1
            $this->message = $content;
72
        } else {
73
            $this->senderAction = $content;
74 6
        }
75 6
76 6
        $this->recipient = \is_string($recipient) ? ['id' => $recipient] : $recipient;
77 6
        $this->messagingType = $options[SendInterface::OPTION_MESSAGING_TYPE] ?? null;
78 6
        $this->notificationType = $options[SendInterface::OPTION_NOTIFICATION_TYPE] ?? null;
79 6
        $this->tag = $options[SendInterface::OPTION_TAG] ?? null;
80
        $this->personaId = $options[SendInterface::OPTION_PERSONA_ID] ?? null;
81
    }
82
83
    /**
84 6
     * @param string|null $method
85
     *
86
     * @return RequestInterface
87 6
     */
88
    public function build(?string $method = null): RequestInterface
89
    {
90
        return $this->origin
91
            ->withMethod('post')
92
            ->withBody(stream_for($this->buildBody()));
93
    }
94 6
95
    /**
96
     * @return string
97 6
     */
98 6
    public function buildBody(): string
99 6
    {
100 6
        $body = [
101 6
            'messaging_type' => $this->messagingType,
102 6
            'recipient' => $this->recipient,
103 6
            'message' => $this->message,
104
            'sender_action' => $this->senderAction,
105
            'notification_type' => $this->notificationType,
106 6
            'tag' => $this->tag,
107
            'persona_id' => $this->personaId,
108
        ];
109
110
        return json_encode(\array_filter($body));
111
    }
112
}
113