Completed
Pull Request — master (#21)
by Alexander
01:13
created

XMLRequest::exec()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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('reportNumber',
84
            [
85
                'start'  => $dateFrom,
86
                'stop'   => $dateTo,
87
                'number' => $number,
88
            ]);
89
    }
90
91
    /**
92
     * @return array|null
93
     */
94
    public function getBalance()
95
    {
96
        return $this->exec('balance');
97
    }
98
99
    public function exec($action, $params = [], $method = 'POST')
100
    {
101
        return parent::exec($action, $params, $method);
102
    }
103
104
105
    /**
106
     * @param string $action
107
     * @return string
108
     */
109
    protected function makeEndPoint($action)
110
    {
111
        return "https://lcab.smsintel.ru/API/XML/{$action}.php";
112
    }
113
114
    /**
115
     * @param array $requestBody
116
     * @return string|array
117
     */
118
    protected function formatRequestBody(array $requestBody)
119
    {
120
        return (new XMLFormatter($requestBody))->toXml();
121
    }
122
123
    /**
124
     * Parses XML from response and returns it as an array
125
     *
126
     * @param string $response
127
     * @return array
128
     */
129
    protected function parseResponse($response)
130
    {
131
        $xml = simplexml_load_string($response);
132
        return (array)$xml->children();
133
    }
134
}