Passed
Pull Request — master (#93)
by Romain
02:36
created

Send::getAllowedTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
eloc 12
nc 1
nop 0
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
14
class Send extends AbstractApi implements SendInterface
15
{
16
    use ValidatorTrait;
17
18
    /**
19
     * @param string                      $pageToken
20
     * @param \GuzzleHttp\ClientInterface $client
21
     *
22
     * @return \Kerox\Messenger\Api\Send
23
     */
24
    public static function getInstance(string $pageToken, ClientInterface $client): self
25
    {
26
        return new self($pageToken, $client);
27
    }
28
29
    /**
30
     * @param string                                $recipient
31
     * @param string|\Kerox\Messenger\Model\Message $message
32
     * @param string                                $notificationType
33
     * @param string|null                           $tag
34
     *
35
     * @throws \Exception
36
     *
37
     * @return \Kerox\Messenger\Response\SendResponse
38
     */
39
    public function message(
40
        string $recipient,
41
        $message,
42
        string $notificationType = self::NOTIFICATION_TYPE_REGULAR,
43
        ?string $tag = null
44 10
    ): SendResponse {
45
        $message = $this->isValidMessage($message);
46 10
        $this->isValidNotificationType($notificationType);
47 10
48
        if ($tag !== null) {
49
            $this->isValidTag($tag);
50
        }
51
52
        $request = new SendRequest($this->pageToken, $message, $recipient, $notificationType, $tag);
53
        $response = $this->client->post('me/messages', $request->build());
54
55 1
        return new SendResponse($response);
56
    }
57 1
58 1
    /**
59
     * @param string $recipient
60
     * @param string $action
61 1
     * @param string $notificationType
62
     *
63
     * @throws \InvalidArgumentException
64
     *
65
     * @return \Kerox\Messenger\Response\SendResponse
66
     */
67
    public function action(
68
        string $recipient,
69
        string $action,
70
        string $notificationType = self::NOTIFICATION_TYPE_REGULAR
71
    ): SendResponse {
72 6
        $this->isValidSenderAction($action);
73
        $this->isValidNotificationType($notificationType);
74 6
75 5
        $request = new SendRequest($this->pageToken, $action, $recipient, $notificationType, null, SendRequest::REQUEST_TYPE_ACTION);
76
        $response = $this->client->post('me/messages', $request->build());
77 4
78 1
        return new SendResponse($response);
79
    }
80
81 3
    /**
82 3
     * @param \Kerox\Messenger\Model\Message\Attachment $attachment
83
     *
84 3
     * @throws \Exception
85
     *
86
     * @return \Kerox\Messenger\Response\SendResponse
87
     */
88
    public function attachment(Attachment $attachment): SendResponse
89
    {
90
        $message = $this->isValidMessage($attachment);
91
92
        $request = new SendRequest($this->pageToken, $message);
93
        $response = $this->client->post('me/message_attachments', $request->build());
94 2
95
        return new SendResponse($response);
96 2
    }
97
}
98