Completed
Pull Request — master (#56)
by Romain
02:22 queued 14s
created

Send::getAllowedSenderAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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