Completed
Push — master ( 4ed813...32abb8 )
by Sergey
04:41 queued 02:10
created

XMLRequest::checkCoupon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
namespace seregazhuk\SmsIntel\Requests;
4
5
use seregazhuk\SmsIntel\XMLFormatter;
6
7
class XMLRequest extends Request
8
{
9
    public static function getAllowedMethods() {
10
        return [
11
            'send',
12
            'cancel',
13
            'getReport',
14
            'getBalance',
15
            'checkCoupon',
16
            'getReportBySms',
17
            'getReportByNumber',
18
        ];
19
    }
20
21
    /**
22
     * @param string|array $to
23
     * @param string $from
24
     * @param string $message
25
     * @param array $params
26
     * @return array|null
27
     */
28
    public function send($to, $from, $message, $params = [ ])
29
    {
30
        $to = is_array($to) ? $to : [ $to ];
31
32
        $requestParams = array_merge(
33
            [
34
                'to'     => $to,
35
                'text'   => $message,
36
                'source' => $from,
37
            ], $params
38
        );
39
40
        return $this->exec('send', $requestParams);
41
    }
42
43
    /**
44
     * @param string $smsId
45
     * @return array|null
46
     */
47
    public function cancel($smsId)
48
    {
49
        return $this->exec('cancel', [ 'smsid' => $smsId ]);
50
    }
51
52
    /**
53
     * @param string $coupon
54
     * @param bool $markAsUsed
55
     * @param null|string $phone
56
     * @return array|null
57
     */
58
    public function checkCoupon($coupon, $markAsUsed = true, $phone = null)
59
    {
60
        return $this->exec(
61
            'checkcode',
62
            [
63
                'code'       => $coupon,
64
                'markAsUsed' => (int)$markAsUsed,
65
                'phone'      => $phone,
66
            ]
67
        );
68
    }
69
70
    /**
71
     * @param string $dateFrom
72
     * @param string $dateTo
73
     * @param null|string $source
74
     * @return array|null
75
     */
76
    public function getReport($dateFrom, $dateTo, $source = null)
77
    {
78
        return $this->exec(
79
            'report', [
80
                'start'  => $dateFrom,
81
                'stop'   => $dateTo,
82
                'source' => $source,
83
            ]
84
        );
85
    }
86
87
    /**
88
     * @param string $smsId
89
     * @return array|null
90
     */
91
    public function getReportBySms($smsId)
92
    {
93
        return $this->exec('report', [ 'smsid' => $smsId ]);
94
    }
95
96
    /**
97
     * @param string $dateFrom
98
     * @param string $dateTo
99
     * @param null|string $number
100
     * @return array|null
101
     */
102
    public function getReportByNumber($dateFrom, $dateTo, $number = null)
103
    {
104
        return $this->exec('reportNumber',
105
            [
106
                'start'  => $dateFrom,
107
                'stop'   => $dateTo,
108
                'number' => $number,
109
            ]);
110
    }
111
112
    /**
113
     * @return array|null
114
     */
115
    public function getBalance()
116
    {
117
        return $this->exec('balance');
118
    }
119
120
121
    /**
122
     * @param string $action
123
     * @return string
124
     */
125
    protected function makeEndPoint($action)
126
    {
127
        return self::BASE_URL . $action . '.php';
128
    }
129
130
    /**
131
     * @param array $requestBody
132
     * @return mixed
133
     */
134
    protected function formatRequestBody(array $requestBody)
135
    {
136
        return (new XMLFormatter($requestBody))->toXml();
137
    }
138
139
    /**
140
     * Parses XML from response and returns it as an array
141
     *
142
     * @param string $response
143
     * @return array
144
     */
145
    protected function parseResponse($response)
146
    {
147
        $xml = simplexml_load_string($response);
148
        return (array)$xml->children();
149
    }
150
}