Passed
Pull Request — master (#109)
by Romain
03:30
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 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 9
    public function message(string $recipient, $message, array $options = []): SendResponse
40
    {
41 9
        $message = $this->isValidMessage($message);
42 8
        $options = $this->isValidOptions($options, $message);
43
44 3
        $request = new SendRequest($this->pageToken, $message, $recipient, $options);
45 3
        $response = $this->client->post('me/messages', $request->build());
46
47 3
        return new SendResponse($response);
48
    }
49
50
    /**
51
     * @param string $recipient
52
     * @param string $action
53
     * @param array  $options
54
     *
55
     * @return \Kerox\Messenger\Response\SendResponse
56
     */
57 2
    public function action(string $recipient, string $action, array $options = []): SendResponse
58
    {
59 2
        $this->isValidSenderAction($action);
60 1
        $options = $this->isValidOptions($options, $action);
61
62 1
        $request = new SendRequest($this->pageToken, $action, $recipient, $options, SendRequest::REQUEST_TYPE_ACTION);
63 1
        $response = $this->client->post('me/messages', $request->build());
64
65 1
        return new SendResponse($response);
66
    }
67
68
    /**
69
     * @param \Kerox\Messenger\Model\Message\Attachment $attachment
70
     *
71
     * @throws \Exception
72
     *
73
     * @return \Kerox\Messenger\Response\SendResponse
74
     */
75 1
    public function attachment(Attachment $attachment): SendResponse
76
    {
77 1
        $message = $this->isValidMessage($attachment);
78
79 1
        $request = new SendRequest($this->pageToken, $message);
80 1
        $response = $this->client->post('me/message_attachments', $request->build());
81
82 1
        return new SendResponse($response);
83
    }
84
85
    /**
86
     * @param array $options
87
     * @param       $message
88
     *
89
     * @throws \InvalidArgumentException
90
     *
91
     * @return array
92
     */
93 9
    private function isValidOptions(array $options, $message): array
94
    {
95 9
        $allowedOptionsKeys = $this->getAllowedOptionsKeys();
96 9
        array_map(function ($key) use ($allowedOptionsKeys): void {
97 6
            if (!\in_array($key, $allowedOptionsKeys, true)) {
98 1
                throw new InvalidArgumentException(sprintf(
99 1
                    'Only "%s" are allowed keys for options.',
100 1
                    implode(', ', $allowedOptionsKeys)
101
                ));
102
            }
103 9
        }, array_keys($options));
104
105 8
        if (isset($options['messaging_type'])) {
106 2
            $this->isValidMessagingType($options['messaging_type']);
107
        }
108
109 7
        if (isset($options['notification_type'])) {
110 4
            $this->isValidNotificationType($options['notification_type']);
111
        }
112
113 6
        if (isset($options['tag'])) {
114 3
            $this->isValidTag($options['tag'], $message);
115
        }
116
117 4
        return $options;
118
    }
119
120
    /**
121
     * @return array
122
     */
123 9
    private function getAllowedOptionsKeys(): array
124
    {
125
        return [
126 9
            'messaging_type',
127
            'notification_type',
128
            'tag',
129
        ];
130
    }
131
}
132