Completed
Push — master ( edddde...b38748 )
by Hilmi Erdem
01:28
created

JetSmsHttpResponse   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 129
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isSuccessful() 0 4 1
A statusCode() 0 4 1
A status() 0 6 2
A message() 0 4 1
A groupId() 0 6 1
A messageReportIdentifiers() 0 8 2
A readResponseBodyString() 0 19 3
1
<?php
2
3
namespace Erdemkeren\JetSms\Http\Responses;
4
5
use BadMethodCallException;
6
7
/**
8
 * Class JetSMSHttpApiResponse.
9
 */
10
final class JetSmsHttpResponse implements JetSmsResponseInterface
11
{
12
    /**
13
     * The read response of SMS message request..
14
     *
15
     * @var array
16
     */
17
    private $responseAttributes = [];
18
19
    /**
20
     * The JetSMS error codes.
21
     *
22
     * @var array
23
     */
24
    private static $statuses = [
25
        '0'   => 'Success',
26
        '-1'  => 'The specified SMS outbox name is invalid',
27
        '-5'  => 'The SMS service credentials are incorrect',
28
        '-6'  => 'The specified data is malformed',
29
        '-7'  => 'The send date of the SMS message has already expired',
30
        '-8'  => 'The SMS gsm number is invalid',
31
        '-9'  => 'The SMS message body is missing',
32
        '-15' => 'The SMS service is having some trouble at the moment',
33
        '-99' => 'The SMS service encountered an unexpected error',
34
    ];
35
36
    /**
37
     * Create a message response.
38
     *
39
     * @param  string $responseBody
40
     */
41
    public function __construct($responseBody)
42
    {
43
        $this->responseAttributes = $this->readResponseBodyString($responseBody);
44
    }
45
46
    /**
47
     * Determine if the operation was successful or not.
48
     *
49
     * @return bool
50
     */
51
    public function isSuccessful()
52
    {
53
        return '0' === $this->statusCode();
54
    }
55
56
    /**
57
     * Get the status code.
58
     *
59
     * @return string
60
     */
61
    public function statusCode()
62
    {
63
        return (string) $this->responseAttributes['statusCode'];
64
    }
65
66
    /**
67
     * Get the string representation of the status.
68
     *
69
     * @return string
70
     */
71
    public function status()
72
    {
73
        return array_key_exists($this->statusCode(), self::$statuses)
74
            ? self::$statuses[$this->statusCode()]
75
            : 'Unknown';
76
    }
77
78
    /**
79
     * Get the message of the response.
80
     *
81
     * @return null|string
82
     */
83
    public function message()
84
    {
85
        return null;
86
    }
87
88
    /**
89
     * Get the group identifier from the response.
90
     */
91
    public function groupId()
92
    {
93
        throw new BadMethodCallException(
94
            "JetSms Http API responses do not group bulk message identifiers. Use messageReportIdentifiers instead."
95
        );
96
    }
97
98
    /**
99
     * Get the message report identifiers for the messages sent.
100
     * Message report id returns -1 if invalid Msisdns, -2 if invalid message text.
101
     *
102
     * @return array
103
     */
104
    public function messageReportIdentifiers()
105
    {
106
        if (array_key_exists('messageids', $this->responseAttributes)) {
107
            return explode('|', $this->responseAttributes['messageids']);
108
        }
109
110
        return [];
111
    }
112
113
    /**
114
     * Read the message response body string.
115
     *
116
     * @param $responseBodyString
117
     * @return array
118
     */
119
    private function readResponseBodyString($responseBodyString)
120
    {
121
        $responseLines = array_filter(array_map(function ($value) {
122
            return trim($value);
123
        }, explode("\n", $responseBodyString)));
124
125
        $result = [];
126
        foreach ($responseLines as $responseLine) {
127
            $responseParts = explode('=', $responseLine);
128
            $result[strtolower($responseParts[0])] = $responseParts[1];
129
        }
130
131
        $status = (int) $result['status'];
132
        unset($result['status']);
133
        $result['success'] = ($status >= 0) ? true : false;
134
        $result['statusCode'] = $status;
135
136
        return $result;
137
    }
138
}
139