SendRequest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 90
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 4
A buildHeaders() 0 6 1
A buildBody() 0 14 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
10
class SendRequest extends AbstractRequest
11
{
12
    public const REQUEST_TYPE_MESSAGE = 'message';
13
    public const REQUEST_TYPE_ACTION = 'action';
14
15
    /**
16
     * @var array|null
17
     */
18
    protected $recipient;
19
20
    /**
21
     * @var string|\Kerox\Messenger\Model\Message|null
22
     */
23
    protected $message;
24
25
    /**
26
     * @var string|null
27
     */
28
    protected $senderAction;
29
30
    /**
31
     * @var string|null
32
     */
33
    protected $notificationType;
34
35
    /**
36
     * @var string|null
37
     */
38
    protected $tag;
39
40
    /**
41
     * @var string|null
42
     */
43
    protected $personaId;
44
45
    /**
46
     * @var string
47
     */
48
    protected $messagingType;
49
50
    /**
51
     * Request constructor.
52
     *
53
     * @param string|\Kerox\Messenger\Model\Message $content
54
     * @param string|array|null                     $recipient
55
     */
56 6
    public function __construct(
57
        string $pageToken,
58
        $content,
59
        $recipient = null,
60
        array $options = [],
61
        string $requestType = self::REQUEST_TYPE_MESSAGE
62
    ) {
63 6
        parent::__construct($pageToken);
64
65 6
        if ($content instanceof Message || $requestType === self::REQUEST_TYPE_MESSAGE) {
66 5
            $this->message = $content;
67
        } else {
68 1
            $this->senderAction = $content;
69
        }
70
71 6
        $this->recipient = \is_string($recipient) ? ['id' => $recipient] : $recipient;
72 6
        $this->messagingType = $options[SendInterface::OPTION_MESSAGING_TYPE] ?? null;
73 6
        $this->notificationType = $options[SendInterface::OPTION_NOTIFICATION_TYPE] ?? null;
74 6
        $this->tag = $options[SendInterface::OPTION_TAG] ?? null;
75 6
        $this->personaId = $options[SendInterface::OPTION_PERSONA_ID] ?? null;
76 6
    }
77
78 6
    protected function buildHeaders(): array
79
    {
80
        return [
81 6
            'Content-Type' => 'application/json',
82
        ];
83
    }
84
85 6
    protected function buildBody(): array
86
    {
87
        $body = [
88 6
            'messaging_type' => $this->messagingType,
89 6
            'recipient' => $this->recipient,
90 6
            'message' => $this->message,
91 6
            'sender_action' => $this->senderAction,
92 6
            'notification_type' => $this->notificationType,
93 6
            'tag' => $this->tag,
94 6
            'persona_id' => $this->personaId,
95
        ];
96
97 6
        return array_filter($body);
98
    }
99
}
100