Completed
Branch develop (8166a6)
by Romain
01:52
created

Send   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 120
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A message() 0 10 1
A action() 0 10 1
A isValidMessage() 0 12 4
A isValidNotificationType() 0 7 2
A getAllowedNotificationType() 0 8 1
A isValidAction() 0 7 2
A getAllowedSenderAction() 0 8 1
1
<?php
2
namespace Kerox\Messenger\Api;
3
4
use GuzzleHttp\Client;
5
use Kerox\Messenger\Model\Message\Attachment;
6
use Kerox\Messenger\Model\Message\Message;
7
use Kerox\Messenger\Request\SendRequest;
8
use Kerox\Messenger\Response\SendResponse;
9
10
class Send extends AbstractApi
11
{
12
13
    const SENDER_ACTION_TYPING_ON = 'typing_on';
14
    const SENDER_ACTION_TYPING_OFF = 'typing_off';
15
    const SENDER_ACTION_MARK_SEEN = 'mark_seen';
16
17
    const NOTIFICATION_TYPE_REGULAR = 'REGULAR';
18
    const NOTIFICATION_TYPE_SILENT_PUSH = 'SILENT_PUSH';
19
    const NOTIFICATION_TYPE_NO_PUSH = 'NO_PUSH';
20
21
    /**
22
     * Send constructor.
23
     *
24
     * @param string $pageToken
25
     * @param \GuzzleHttp\Client $client
26
     */
27
    public function __construct(string $pageToken, Client $client)
28
    {
29
        parent::__construct($pageToken, $client);
30
    }
31
32
    /**
33
     * @param string $recipient
34
     * @param $message
35
     * @param string $notificationType
36
     * @return \Kerox\Messenger\Response\SendResponse
37
     */
38
    public function message(string $recipient, $message, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse
39
    {
40
        $message = $this->isValidMessage($message);
41
        $this->isValidNotificationType($notificationType);
42
43
        $request = new SendRequest($this->pageToken, $recipient, $message, null, $notificationType);
44
        $response = $this->client->post('/me/messages', $request->build());
45
46
        return new SendResponse($response);
47
    }
48
49
    /**
50
     * @param string $recipient
51
     * @param string $action
52
     * @param string $notificationType
53
     * @return \Kerox\Messenger\Response\SendResponse
54
     */
55
    public function action(string $recipient, string $action, string $notificationType = self::NOTIFICATION_TYPE_REGULAR): SendResponse
56
    {
57
        $this->isValidAction($action);
58
        $this->isValidNotificationType($notificationType);
59
60
        $request = new SendRequest($this->pageToken, $recipient, null, $action, $notificationType);
61
        $response = $this->client->post('/me/messages', $request->build());
62
63
        return new SendResponse($response);
64
    }
65
66
    /**
67
     * @param $message
68
     * @return Message
69
     * @throws \InvalidArgumentException
70
     */
71
    private function isValidMessage($message): Message
72
    {
73
        if ($message instanceof Message) {
0 ignored issues
show
Bug introduced by
The class Kerox\Messenger\Model\Message\Message does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
74
            return $message;
75
        }
76
77
        if (is_string($message) || $message instanceof Attachment) {
78
            return new Message($message);
79
        }
80
81
        throw new \InvalidArgumentException('$message must be a string or an instance of Message or Attachment');
82
    }
83
84
    /**
85
     * @param string $notificationType
86
     */
87
    private function isValidNotificationType(string $notificationType)
88
    {
89
        $allowedNotificationType = $this->getAllowedNotificationType();
90
        if (!in_array($notificationType, $allowedNotificationType)) {
91
            throw new \InvalidArgumentException('$notificationType must be either ' . implode(', ', $allowedNotificationType));
92
        }
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    private function getAllowedNotificationType(): array
99
    {
100
        return [
101
            self::NOTIFICATION_TYPE_REGULAR,
102
            self::NOTIFICATION_TYPE_NO_PUSH,
103
            self::NOTIFICATION_TYPE_SILENT_PUSH,
104
        ];
105
    }
106
107
    /**
108
     * @param string $action
109
     */
110
    private function isValidAction(string $action)
111
    {
112
        $allowedSenderAction = $this->getAllowedSenderAction();
113
        if (!in_array($action, $allowedSenderAction)) {
114
            throw new \InvalidArgumentException('$action must be either ' . implode(', ', $allowedSenderAction));
115
        }
116
    }
117
118
    /**
119
     * @return array
120
     */
121
    private function getAllowedSenderAction(): array
122
    {
123
        return [
124
            self::SENDER_ACTION_TYPING_ON,
125
            self::SENDER_ACTION_TYPING_OFF,
126
            self::SENDER_ACTION_MARK_SEEN,
127
        ];
128
    }
129
}
130