Completed
Pull Request — master (#20)
by Alexander
10:01
created

XMLRequest::checkCoupon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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