Completed
Push — master ( 8e0ded...b5dffc )
by Romain
10s
created

Send::attachment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 1
cts 1
cp 1
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Api;
6
7
use GuzzleHttp\ClientInterface;
8
use Kerox\Messenger\Helper\ValidatorTrait;
9
use Kerox\Messenger\Model\Message;
10
use Kerox\Messenger\Model\Message\Attachment;
11
use Kerox\Messenger\Request\SendRequest;
12
use Kerox\Messenger\Response\SendResponse;
13
14
class Send extends AbstractApi
15
{
16
    use ValidatorTrait;
17
18
    public const SENDER_ACTION_TYPING_ON = 'typing_on';
19
    public const SENDER_ACTION_TYPING_OFF = 'typing_off';
20
    public const SENDER_ACTION_MARK_SEEN = 'mark_seen';
21
22
    public const NOTIFICATION_TYPE_REGULAR = 'REGULAR';
23
    public const NOTIFICATION_TYPE_SILENT_PUSH = 'SILENT_PUSH';
24
    public const NOTIFICATION_TYPE_NO_PUSH = 'NO_PUSH';
25
26
    public const TAG_SHIPPING_UPDATE = 'SHIPPING_UPDATE';
27
    public const TAG_RESERVATION_UPDATE = 'RESERVATION_UPDATE';
28
    public const TAG_ISSUE_RESOLUTION = 'ISSUE_RESOLUTION';
29
    public const TAG_APPOINTMENT_UPDATE = 'APPOINTMENT_UPDATE';
30
    public const TAG_GAME_EVENT = 'GAME_EVENT';
31
    public const TAG_TRANSPORTATION_UPDATE = 'TRANSPORTATION_UPDATE';
32
    public const TAG_FEATURE_FUNCTIONALITY_UPDATE = 'FEATURE_FUNCTIONALITY_UPDATE';
33
    public const TAG_TICKET_UPDATE = 'TICKET_UPDATE';
34
    public const TAG_ACCOUNT_UPDATE = 'ACCOUNT_UPDATE';
35
    public const TAG_PAYMENT_UPDATE = 'PAYMENT_UPDATE';
36
    public const TAG_PERSONAL_FINANCE_UPDATE = 'PERSONAL_FINANCE_UPDATE';
37
38
    /**
39
     * @var null|\Kerox\Messenger\Api\Send
40
     */
41
    private static $_instance;
42
43
    /**
44 10
     * @param string                      $pageToken
45
     * @param \GuzzleHttp\ClientInterface $client
46 10
     *
47 10
     * @return \Kerox\Messenger\Api\Send
48
     */
49
    public static function getInstance(string $pageToken, ClientInterface $client): self
50
    {
51
        if (self::$_instance === null) {
52
            self::$_instance = new self($pageToken, $client);
53
        }
54
55 1
        return self::$_instance;
56
    }
57 1
58 1
    /**
59
     * @param string                                $recipient
60
     * @param string|\Kerox\Messenger\Model\Message $message
61 1
     * @param string                                $notificationType
62
     * @param string|null                           $tag
63
     *
64
     * @throws \Exception
65
     *
66
     * @return \Kerox\Messenger\Response\SendResponse
67
     */
68
    public function message(
69
        string $recipient,
70
        $message,
71
        string $notificationType = self::NOTIFICATION_TYPE_REGULAR,
72 6
        ?string $tag = null
73
    ): SendResponse {
74 6
        $message = $this->isValidMessage($message);
75 5
        $this->isValidNotificationType($notificationType, $this->getAllowedNotificationType());
76
77 4
        if ($tag !== null) {
78 1
            $this->isValidTag($tag, $this->getAllowedTag());
79
        }
80
81 3
        $request = new SendRequest($this->pageToken, $message, $recipient, $notificationType, $tag);
82 3
        $response = $this->client->post('me/messages', $request->build());
83
84 3
        return new SendResponse($response);
85
    }
86
87
    /**
88
     * @param string $recipient
89
     * @param string $action
90
     * @param string $notificationType
91
     *
92
     * @throws \InvalidArgumentException
93
     *
94 2
     * @return \Kerox\Messenger\Response\SendResponse
95
     */
96 2
    public function action(
97 1
        string $recipient,
98
        string $action,
99 1
        string $notificationType = self::NOTIFICATION_TYPE_REGULAR
100 1
    ): SendResponse {
101
        $this->isValidAction($action);
102 1
        $this->isValidNotificationType($notificationType, $this->getAllowedNotificationType());
103
104
        $request = new SendRequest($this->pageToken, $action, $recipient, $notificationType, null, SendRequest::REQUEST_TYPE_ACTION);
105
        $response = $this->client->post('me/messages', $request->build());
106
107
        return new SendResponse($response);
108
    }
109
110 1
    /**
111
     * @param \Kerox\Messenger\Model\Message\Attachment $attachment
112 1
     *
113
     * @throws \Exception
114 1
     *
115 1
     * @return \Kerox\Messenger\Response\SendResponse
116
     */
117 1
    public function attachment(Attachment $attachment): SendResponse
118
    {
119
        $message = $this->isValidMessage($attachment);
120
121
        $request = new SendRequest($this->pageToken, $message);
122
        $response = $this->client->post('me/message_attachments', $request->build());
123
124
        return new SendResponse($response);
125
    }
126
127 7
    /**
128
     * @return array
129 7
     */
130 1
    private function getAllowedNotificationType(): array
131
    {
132
        return [
133 6
            self::NOTIFICATION_TYPE_REGULAR,
134 5
            self::NOTIFICATION_TYPE_SILENT_PUSH,
135
            self::NOTIFICATION_TYPE_NO_PUSH,
136
        ];
137 1
    }
138
139
    /**
140
     * @param string $action
141
     *
142
     * @throws \InvalidArgumentException
143
     */
144
    private function isValidAction(string $action): void
145 6
    {
146
        $allowedSenderAction = $this->getAllowedSenderAction();
147 6
        if (!\in_array($action, $allowedSenderAction, true)) {
148 6
            throw new \InvalidArgumentException('$action must be either ' . implode(', ', $allowedSenderAction));
149 1
        }
150
    }
151 5
152
    /**
153
     * @return array
154
     */
155
    private function getAllowedSenderAction(): array
156 6
    {
157
        return [
158
            self::SENDER_ACTION_TYPING_ON,
159 6
            self::SENDER_ACTION_TYPING_OFF,
160 6
            self::SENDER_ACTION_MARK_SEEN,
161 6
        ];
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    private function getAllowedTag(): array
168
    {
169
        return [
170 2
            self::TAG_ISSUE_RESOLUTION,
171
            self::TAG_RESERVATION_UPDATE,
172 2
            self::TAG_SHIPPING_UPDATE,
173 2
            self::TAG_APPOINTMENT_UPDATE,
174 1
            self::TAG_GAME_EVENT,
175
            self::TAG_TRANSPORTATION_UPDATE,
176 1
            self::TAG_FEATURE_FUNCTIONALITY_UPDATE,
177
            self::TAG_TICKET_UPDATE,
178
            self::TAG_ACCOUNT_UPDATE,
179
            self::TAG_PAYMENT_UPDATE,
180
            self::TAG_PERSONAL_FINANCE_UPDATE,
181 2
        ];
182
    }
183
}
184