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

SendRequest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 31
dl 0
loc 99
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 4
A buildBody() 0 13 1
A build() 0 5 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 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
     * @param string                                $requestType
60
     */
61 6
    public function __construct(
62
        string $pageToken,
63
        $content,
64
        ?string $recipient = null,
65
        array $options = [],
66
        string $requestType = self::REQUEST_TYPE_MESSAGE
67
    ) {
68 6
        parent::__construct($pageToken);
69
70 6
        if ($content instanceof Message || $requestType === self::REQUEST_TYPE_MESSAGE) {
71 5
            $this->message = $content;
72
        } else {
73 1
            $this->senderAction = $content;
74
        }
75
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 6
        $this->personaId = $options[SendInterface::OPTION_PERSONA_ID] ?? null;
81 6
    }
82
83
    /**
84
     * @param string|null $method
85
     *
86
     * @return RequestInterface
87
     */
88 6
    public function build(?string $method = null): RequestInterface
89
    {
90 6
        return $this->origin
91 6
            ->withMethod('post')
92 6
            ->withBody(stream_for($this->buildBody()));
93
    }
94
95
    /**
96
     * @return string
97
     */
98 6
    public function buildBody(): string
99
    {
100
        $body = [
101 6
            'messaging_type' => $this->messagingType,
102 6
            'recipient' => $this->recipient,
103 6
            'message' => $this->message,
104 6
            'sender_action' => $this->senderAction,
105 6
            'notification_type' => $this->notificationType,
106 6
            'tag' => $this->tag,
107 6
            'persona_id' => $this->personaId,
108
        ];
109
110 6
        return json_encode(\array_filter($body));
111
    }
112
}
113