Passed
Push — master ( 6c011a...8a7749 )
by Romain
36s
created

Send::attachment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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