Completed
Push — master ( 02e5d6...52f4c9 )
by Sergey
02:45
created

JSONRequest::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 8
nc 2
nop 4
1
<?php
2
3
namespace seregazhuk\SmsIntel\Requests;
4
5
class JSONRequest extends Request
6
{
7
    /**
8
     * @return array
9
     */
10
    public static function getAllowedMethods()
11
    {
12
        return [
13
            'getSource',
14
            'getContacts',
15
            'requestSource',
16
            'send',
17
            'getPhoneInfo',
18
        ];
19
    }
20
21
    /**
22
     * @param string $source
23
     * @return array|null
24
     */
25
    public function getSource($source)
26
    {
27
        return $this->exec('requestSource', ['source' => $source]);
28
    }
29
30
    /**
31
     * @param string|array $to
32
     * @param string $from
33
     * @param string $message
34
     * @param array $params
35
     * @return array|null
36
     */
37
    public function send($to, $from, $message, $params = [ ])
38
    {
39
        $to = is_array($to) ? $to : [ $to ];
40
41
        $requestParams = array_merge(
42
            [
43
                'to'     => $to,
44
                'txt'   => $message,
45
                'source' => $from,
46
            ],
47
            $params
48
        );
49
50
        return $this->exec('sendSms', $requestParams);
51
    }
52
53
    /**
54
     * @param string $name
55
     * @return array|null
56
     */
57
    public function requestSource($name)
58
    {
59
        return $this->exec('requestSource', ['source' => $name]);
60
    }
61
62
    /**
63
     * @param null|string $groupId
64
     * @param null|string $phone
65
     * @return array|null
66
     */
67
    public function getContacts($groupId = null, $phone = null)
68
    {
69
        return $this->exec(
70
            'getContacts',
71
            ['idGroup' => $groupId, 'phone' => $phone]
72
        );
73
    }
74
    
75
    /**
76
     * @param string $phone
77
     * @return array|null
78
     */
79
    public function getPhoneInfo($phone)
80
    {
81
        return $this->exec('getPhoneInfo', ['phone' => $phone]);
82
    }
83
84
    /**
85
     * @param array $contactInfo
86
     * @return array|null
87
     */
88
    public function addContact(array $contactInfo)
89
    {
90
        return $this->exec('addContact', $contactInfo);
91
    }
92
93
    /**
94
     * @param string|null $groupId
95
     * @param string|null $groupName
96
     * @return array|null
97
     */
98
    public function getGroups($groupId = null,  $groupName = null)
99
    {
100
        return $this->exec(
101
            'getGroups',
102
            ['id' => $groupId, 'name' => $groupName]
103
        );
104
    }
105
106
    /**
107
     * @param string $name
108
     * @return array|null
109
     */
110
    public function createGroup($name)
111
    {
112
        return $this->editGroup($name, null);
113
    }
114
115
    /**
116
     * @param string $id
117
     * @param string $name
118
     * @return array|null
119
     */
120
    public function editGroup($name, $id)
121
    {
122
        return $this->exec('saveGroup', ['id' => $id, 'name' => $name]);
123
    }
124
125
    /**
126
     * @return array|null
127
     */
128
    public function getAccountInfo()
129
    {
130
        return $this->exec('info');
131
    }
132
133
    /**
134
     * @param string $phone
135
     * @param string|null $groupId
136
     * @return array|null
137
     */
138
    public function removeContact($phone, $groupId = null)
139
    {
140
        return $this->exec('removeContact', ['phone' => $phone, 'groupId' => $groupId]);
141
    }
142
143
    /**
144
     * @param string $action
145
     * @return string
146
     */
147
    public function makeEndPoint($action)
148
    {
149
        return 'https://lcab.smsintel.ru/lcabApi/' . $action . '.php';
150
    }
151
152
    /**
153
     * @param array $requestBody
154
     * @return mixed
155
     */
156
    protected function formatRequestBody(array $requestBody)
157
    {
158
        return $requestBody;
159
    }
160
161
    /**
162
     * @param string $response
163
     * @return array
164
     */
165
    protected function parseResponse($response)
166
    {
167
        return json_decode($response, true);
168
    }
169
}