NexmoCallback::cleanKeys()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace DoS\SMSBundle\SMS\Provider;
4
5
use DoS\SMSBundle\SMS\ProviderCallbackInterface;
6
use DoS\SMSBundle\SMS\ResultCallback;
7
8
class NexmoCallback implements ProviderCallbackInterface
9
{
10
    /**
11
     * @var ResultCallback[]
12
     */
13
    protected $results;
14
15
    /**
16
     * @param array $data
17
     *
18
     * @return array
19
     */
20
    protected function cleanKeys(array $data)
21
    {
22
        $return = array();
23
24
        foreach ($data as $key => $value) {
25
            $return[strtolower(preg_replace('/\W/', '', $key))] = $value;
26
        }
27
28
        return $return;
29
    }
30
31
    protected function processResponse(array $data)
32
    {
33
        $result = new ResultCallback($data);
34
        $result->setMessageId($data['messageid']);
35
        $result->setPrice($data['price'] * 100);
36
        $result->setSuccess(empty($data['errorcode']));
37
38
        return $result;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function process(array $response)
45
    {
46
        $response = $this->cleanKeys($response);
47
48
        if (array_key_exists('messages', $response)) {
49
            foreach ($response['messages'] as $message) {
50
                $this->results[] = $this->processResponse($message);
51
            }
52
        } else {
53
            $this->results[] = $this->processResponse($response);
54
        }
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getResults()
61
    {
62
        return $this->results;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getReponseCodes()
69
    {
70
        return array(
71
            array(
72
                'code' => 0,
73
                'message' => 'Success',
74
                'description' => 'The message was successfully accepted for delivery by Nexmo',
75
            ),
76
            array(
77
                'code' => 1,
78
                'message' => 'Throttled',
79
                'description' => 'You have exceeded the submission capacity allowed on this account, please back-off and retry',
80
            ),
81
            array(
82
                'code' => 2,
83
                'message' => 'Missing params',
84
                'description' => 'Your request is incomplete and missing some mandatory parameters',
85
            ),
86
            array(
87
                'code' => 3,
88
                'message' => 'Invalid params',
89
                'description' => 'The value of one or more parameters is invalid',
90
            ),
91
            array(
92
                'code' => 4,
93
                'message' => 'Invalid credentials',
94
                'description' => 'The api_key / api_secret you supplied is either invalid or disabled',
95
            ),
96
            array(
97
                'code' => 5,
98
                'message' => 'Internal error',
99
                'description' => 'An error has occurred in the Nexmo platform whilst processing this message',
100
            ),
101
            array(
102
                'code' => 6,
103
                'message' => 'Invalid message',
104
                'description' => 'The Nexmo platform was unable to process this message, for example, an un-recognized number prefix or the number is not whitelisted if your account is new',
105
            ),
106
            array(
107
                'code' => 7,
108
                'message' => 'Number barred',
109
                'description' => 'The number you are trying to submit to is blacklisted and may not receive messages',
110
            ),
111
            array(
112
                'code' => 8,
113
                'message' => 'Partner account barred',
114
                'description' => 'The api_key you supplied is for an account that has been barred from submitting messages',
115
            ),
116
            array(
117
                'code' => 9,
118
                'message' => 'Partner quota exceeded',
119
                'description' => 'Your pre-pay account does not have sufficient credit to process this message',
120
            ),
121
            array(
122
                'code' => 11,
123
                'message' => 'Account not enabled for REST',
124
                'description' => 'This account is not provisioned for REST submission, you should use SMPP instead',
125
            ),
126
            array(
127
                'code' => 12,
128
                'message' => 'Message too long',
129
                'description' => 'Applies to Binary submissions, where the length of the UDH and the message body combined exceed 140 octets',
130
            ),
131
            array(
132
                'code' => 13,
133
                'message' => 'Communication Failed',
134
                'description' => 'Message was not submitted because there was a communication failure',
135
            ),
136
            array(
137
                'code' => 14,
138
                'message' => 'Invalid Signature',
139
                'description' => 'Message was not submitted due to a verification failure in the submitted signature',
140
            ),
141
            array(
142
                'code' => 15,
143
                'message' => 'Invalid sender address',
144
                'description' => 'The sender address (from parameter) was not allowed for this message. Restrictions may apply depending on the destination see https://help.nexmo.com/hc/en-us/sections/200622473-Country-Specific-Features-and-Restrictions',
145
            ),
146
            array('code' => 16, 'message' => 'Invalid TTL', 'description' => 'The ttl parameter values is invalid'),
147
            array(
148
                'code' => 19,
149
                'message' => 'Facility not allowed',
150
                'description' => 'Your request makes use of a facility that is not enabled on your account',
151
            ),
152
            array(
153
                'code' => 20,
154
                'message' => 'Invalid Message class',
155
                'description' => 'The message class value supplied was out of range (0 - 3)',
156
            ),
157
        );
158
    }
159
}
160