Send::isValidOptions()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 17

Duplication

Lines 3
Ratio 17.65 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 0
Metric Value
dl 3
loc 17
ccs 12
cts 12
cp 1
rs 9.0777
c 0
b 0
f 0
cc 6
nc 6
nop 2
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Kerox\Messenger\Api;
6
7
use Kerox\Messenger\Exception\InvalidOptionException;
8
use Kerox\Messenger\Exception\InvalidRecipientException;
9
use Kerox\Messenger\Exception\InvalidTypeException;
10
use Kerox\Messenger\Helper\ValidatorTrait;
11
use Kerox\Messenger\Model\Message\AbstractAttachment;
12
use Kerox\Messenger\Request\SendRequest;
13
use Kerox\Messenger\Response\SendResponse;
14
use Kerox\Messenger\SendInterface;
15
16
class Send extends AbstractApi implements SendInterface
17
{
18
    use ValidatorTrait;
19
20
    /**
21
     * @param string|array $recipient
22
     * @param mixed        $message
23
     *
24
     * @throws \Exception
25
     */
26 11 View Code Duplication
    public function message($recipient, $message, array $options = []): SendResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28 11
        $this->isValidRecipient($recipient);
29 10
        $this->isValidOptions($options, $message);
30 5
        $message = $this->isValidMessage($message);
31
32 4
        $request = new SendRequest($this->pageToken, $message, $recipient, $options);
33 4
        $response = $this->client->post('me/messages', $request->build());
34
35 4
        return new SendResponse($response);
36
    }
37
38
    /**
39
     * @param string|array $recipient
40
     *
41
     * @throws \Kerox\Messenger\Exception\MessengerException
42
     */
43 2 View Code Duplication
    public function action($recipient, string $action, array $options = []): SendResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    {
45 2
        $this->isValidRecipient($recipient);
46 2
        $this->isValidSenderAction($action);
47 1
        $this->isValidOptions($options);
48
49 1
        $request = new SendRequest($this->pageToken, $action, $recipient, $options, SendRequest::REQUEST_TYPE_ACTION);
50 1
        $response = $this->client->post('me/messages', $request->build());
51
52 1
        return new SendResponse($response);
53
    }
54
55
    /**
56
     * @throws \Exception
57
     */
58 1 View Code Duplication
    public function attachment(AbstractAttachment $attachment): SendResponse
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60 1
        $message = $this->isValidMessage($attachment);
61
62 1
        $request = new SendRequest($this->pageToken, $message);
63 1
        $response = $this->client->post('me/message_attachments', $request->build());
64
65 1
        return new SendResponse($response);
66
    }
67
68
    /**
69
     * @param mixed $recipient
70
     *
71
     * @throws \Kerox\Messenger\Exception\InvalidRecipientException
72
     */
73 13
    private function isValidRecipient($recipient): void
74
    {
75 13
        if (!\is_string($recipient) && !\is_array($recipient)) {
76 1
            throw new InvalidRecipientException('"recipient" must be either a string or an array.');
77
        }
78 12
    }
79
80
    /**
81
     * @param mixed $message
82
     *
83
     * @throws \Kerox\Messenger\Exception\MessengerException
84
     */
85 11
    private function isValidOptions(array $options, $message = null): void
86
    {
87 11
        $allowedOptionsKeys = $this->getAllowedOptionsKeys();
88 11
        foreach ($options as $key => $value) {
89 7 View Code Duplication
            if (!\in_array($key, $allowedOptionsKeys, true)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90 1
                throw new InvalidOptionException(sprintf('Only "%s" are allowed keys for options.', implode(', ', $allowedOptionsKeys)));
91
            }
92
93 7
            if ($key === self::OPTION_MESSAGING_TYPE) {
94 2
                $this->isValidMessagingType($value);
95 6
            } elseif ($key === self::OPTION_NOTIFICATION_TYPE) {
96 5
                $this->isValidNotificationType($value);
97 4
            } elseif ($key === self::OPTION_TAG) {
98 3
                $this->isValidTag($value, $message);
99
            }
100
        }
101 6
    }
102
103
    /**
104
     * @throws \Kerox\Messenger\Exception\MessengerException
105
     */
106 2
    private function isValidMessagingType(string $messagingType): void
107
    {
108 2
        $allowedMessagingType = $this->getAllowedMessagingType();
109 2
        if (!\in_array($messagingType, $allowedMessagingType, true)) {
110 1
            throw new InvalidTypeException(sprintf('"messagingType" must be either "%s".', implode(', ', $allowedMessagingType)));
111
        }
112 1
    }
113
114 11
    private function getAllowedOptionsKeys(): array
115
    {
116
        return [
117 11
            self::OPTION_MESSAGING_TYPE,
118 11
            self::OPTION_NOTIFICATION_TYPE,
119 11
            self::OPTION_TAG,
120 11
            self::OPTION_PERSONA_ID,
121
        ];
122
    }
123
124 2
    public function getAllowedMessagingType(): array
125
    {
126
        return [
127 2
            self::MESSAGING_TYPE_RESPONSE,
128 2
            self::MESSAGING_TYPE_MESSAGE_TAG,
129 2
            self::MESSAGING_TYPE_NON_PROMOTIONAL_SUBSCRIPTION,
130 2
            self::MESSAGING_TYPE_UPDATE,
131
        ];
132
    }
133
}
134