Completed
Pull Request — master (#107)
by Romain
02:31
created

Send::isValidOptions()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

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