Completed
Push — master ( 4474f6...7e8ad4 )
by Sergey
04:22 queued 02:10
created

JSONRequest::makeEndPoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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