Passed
Pull Request — master (#110)
by Romain
02:32
created

Send::getInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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\Attachment;
10
use Kerox\Messenger\Request\SendRequest;
11
use Kerox\Messenger\Response\SendResponse;
12
use Kerox\Messenger\SendInterface;
13
use Psr\Log\InvalidArgumentException;
14
15
class Send extends AbstractApi implements SendInterface
16
{
17
    use ValidatorTrait;
18
19
    /**
20
     * @param string                                $recipient
21
     * @param string|\Kerox\Messenger\Model\Message $message
22
     * @param array                                 $options
23
     *
24
     * @throws \Exception
25
     *
26
     * @return \Kerox\Messenger\Response\SendResponse
27
     */
28 9
    public function message(string $recipient, $message, array $options = []): SendResponse
29
    {
30 9
        $message = $this->isValidMessage($message);
31 8
        $options = $this->isValidOptions($options, $message);
32
33 3
        $request = new SendRequest($this->pageToken, $message, $recipient, $options);
34 3
        $response = $this->client->post('me/messages', $request->build());
35
36 3
        return new SendResponse($response);
37
    }
38
39
    /**
40
     * @param string $recipient
41
     * @param string $action
42
     * @param array  $options
43
     *
44
     * @return \Kerox\Messenger\Response\SendResponse
45
     */
46 2
    public function action(string $recipient, string $action, array $options = []): SendResponse
47
    {
48 2
        $this->isValidSenderAction($action);
49 1
        $options = $this->isValidOptions($options, $action);
50
51 1
        $request = new SendRequest($this->pageToken, $action, $recipient, $options, SendRequest::REQUEST_TYPE_ACTION);
52 1
        $response = $this->client->post('me/messages', $request->build());
53
54 1
        return new SendResponse($response);
55
    }
56
57
    /**
58
     * @param \Kerox\Messenger\Model\Message\Attachment $attachment
59
     *
60
     * @throws \Exception
61
     *
62
     * @return \Kerox\Messenger\Response\SendResponse
63
     */
64 1
    public function attachment(Attachment $attachment): SendResponse
65
    {
66 1
        $message = $this->isValidMessage($attachment);
67
68 1
        $request = new SendRequest($this->pageToken, $message);
69 1
        $response = $this->client->post('me/message_attachments', $request->build());
70
71 1
        return new SendResponse($response);
72
    }
73
74
    /**
75
     * @param array $options
76
     * @param       $message
77
     *
78
     * @throws \InvalidArgumentException
79
     *
80
     * @return array
81
     */
82 9
    private function isValidOptions(array $options, $message): array
83
    {
84 9
        $allowedOptionsKeys = $this->getAllowedOptionsKeys();
85 9
        array_map(function ($key) use ($allowedOptionsKeys): void {
86 6
            if (!\in_array($key, $allowedOptionsKeys, true)) {
87 1
                throw new InvalidArgumentException(sprintf(
88 1
                    'Only "%s" are allowed keys for options.',
89 1
                    implode(', ', $allowedOptionsKeys)
90
                ));
91
            }
92 9
        }, array_keys($options));
93
94 8
        if (isset($options['messaging_type'])) {
95 2
            $this->isValidMessagingType($options['messaging_type']);
96
        }
97
98 7
        if (isset($options['notification_type'])) {
99 4
            $this->isValidNotificationType($options['notification_type']);
100
        }
101
102 6
        if (isset($options['tag'])) {
103 3
            $this->isValidTag($options['tag'], $message);
104
        }
105
106 4
        return $options;
107
    }
108
109
    /**
110
     * @return array
111
     */
112 9
    private function getAllowedOptionsKeys(): array
113
    {
114
        return [
115 9
            'messaging_type',
116
            'notification_type',
117
            'tag',
118
        ];
119
    }
120
}
121