NetgsmOtpMessage::createXmlPost()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 20
rs 9.7333
cc 2
nc 2
nop 0
1
<?php
2
3
namespace TarfinLabs\Netgsm\Sms;
4
5
use TarfinLabs\Netgsm\NetgsmErrors;
6
7
class NetgsmOtpMessage extends AbstractNetgsmMessage
8
{
9
    protected $url = 'sms/send/otp';
10
11
    protected $errorCodes = [
12
        '20'  => NetgsmErrors::MESSAGE_TOO_LONG,
13
        '30'  => NetgsmErrors::CREDENTIALS_INCORRECT,
14
        '40'  => NetgsmErrors::SENDER_INCORRECT,
15
        '50'  => NetgsmErrors::RECEIVER_INCORRECT,
16
        '60'  => NetgsmErrors::OTP_ACCOUNT_NOT_DEFINED,
17
        '70'  => NetgsmErrors::PARAMETERS_INCORRECT,
18
        '80'  => NetgsmErrors::QUERY_LIMIT_EXCEED,
19
        '100' => NetgsmErrors::SYSTEM_ERROR,
20
    ];
21
22
    protected $fields = [
23
        'msgheader',
24
        'msg',
25
        'no',
26
    ];
27
28
    /**
29
     * creates the xml request body for sms sending via xml post method.
30
     *
31
     * @return string
32
     */
33
    protected function createXmlPost(): string
34
    {
35
        $xml = '<?xml version="1.0" encoding="UTF-8"?>';
36
        $xml .= '<mainbody>';
37
        $xml .= '<header>';
38
        $xml .= '<usercode>'.$this->credentials['user_code'].'</usercode>';
39
        $xml .= '<password>'.$this->credentials['secret'].'</password>';
40
        $xml .= '<msgheader>'.$this->getHeader().'</msgheader>';
41
        $xml .= '</header>';
42
        $xml .= '<body>';
43
        $xml .= '<msg>';
44
        $xml .= '<![CDATA['.$this->message.']]>';
45
        $xml .= '</msg>';
46
        foreach ($this->recipients as $recipient) {
47
            $xml .= '<no>'.$recipient.'</no>';
48
        }
49
        $xml .= '</body>';
50
        $xml .= '</mainbody>';
51
52
        return $xml;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getUrl(): string
59
    {
60
        return $this->url;
61
    }
62
63
    /**
64
     * maps the given parameters according to the required parameters of the otp message endpoint.
65
     *
66
     * @return array
67
     */
68
    protected function mappers(): array
69
    {
70
        return [
71
            'msgheader' => $this->header ?? $this->defaults['header'],
72
            'msg'       => $this->message,
73
            'no'        => is_array($this->recipients) ? $this->recipients[0] : $this->recipients,
0 ignored issues
show
introduced by
The condition is_array($this->recipients) is always true.
Loading history...
74
        ];
75
    }
76
}
77