SmsOtpCommonTrait   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Test Coverage

Coverage 83.56%

Importance

Changes 0
Metric Value
dl 0
loc 218
ccs 61
cts 73
cp 0.8356
rs 10
c 0
b 0
f 0
wmc 29

15 Methods

Rating   Name   Duplication   Size   Complexity  
A isVaildAfterMinutes() 0 5 1
A isVaildDateTime() 0 10 4
A addXml() 0 9 2
A checkArray() 0 5 2
A checkResponse() 0 6 2
A isValidNumber() 0 4 1
A isString() 0 4 1
A addDataArray() 0 13 3
A checkMessageLength() 0 8 2
A isNumeric() 0 4 1
A buildData() 0 14 3
A addArray() 0 4 2
A isInterger() 0 4 1
A validLength() 0 13 3
A isKeyExists() 0 3 1
1
<?php
2
namespace Sender\Traits;
3
4
use Sender\Validation;
5
use Sender\MobileNumber;
6
use Sender\Otp\OtpBuildClass;
7
use Sender\Sms\SmsDefineClass;
8
use Sender\Sms\SmsBuildClass;
9
use Sender\Otp\OtpDefineClass;
10
use Sender\ExceptionClass\ParameterException;
11
12
/**
13
 * This trait for SMS OTP FUNCTIONS
14
 *
15
 * @package    Msg91 SMS&OTP package
16
 * @author     VenkatS <[email protected]>
17
 * @link       https://github.com/tesark/msg91-php
18
 * @license    MIT
19
 */
20
21
trait SmsOtpCommonTrait
22
{
23
    /**
24
     * This function add data to array
25
     * @param string $key
26
     * @param int|string $value
27
     * @param array $array
28
     *
29
     * @return array
30
     */
31 92
    public function addArray($key, $value, $array)
32
    {
33 92
        $result = !is_null($value) ? $value : null;
0 ignored issues
show
introduced by
The condition ! is_null($value) can never be false.
Loading history...
34 92
        return $array += [$key => $result];
35
    }
36
    /**
37
     * This function for test sender length
38
     * @param string $key
39
     * @param string $value
40
     * @param array $data
41
     * @param string $api
42
     * @param int $category
43
     * @param array $xmlDoc
44
     *
45
     */
46 65
    public function validLength($key, $value, $data, $api, $category = null, $xmlDoc = null)
47
    {
48 65
        if (strlen($value) == 6) {
49 60
            if ($api === 'otp') {
50 12
                $data = $this->addArray($key, $value, $data);
51
            } else {
52 60
                $data = $this->buildData($category, $key, $value, $data, $xmlDoc);
53
            }
54
        } else {
55 5
            $message = "String length must be 6 characters";
56 5
            throw ParameterException::invalidInput($key, "string", $value, $message);
57
        }
58 60
        return $data;
59
    }
60
    /**
61
     * Check string value
62
     * @param string $value
63
     * @return bool
64
     */
65 96
    public function isString($value)
66
    {
67 96
        $result = Validation::isString($value);
68 96
        return $result;
69
    }
70
    /**
71
     * Check integer value
72
     * @param value
73
     * @return bool
74
     */
75 100
    public function isInterger($value)
76
    {
77 100
        $result = Validation::isInteger($value);
78 100
        return $result;
79
    }
80
    /**
81
     * Check numeric value
82
     * @param value
83
     * @return bool
84
     */
85 12
    public function isNumeric($value)
86
    {
87 12
        $result = Validation::isNumeric($value);
88 12
        return $result;
89
    }
90
    /*
91
     * Check isvalid mobile number 
92
     */
93 10
    public function isValidNumber($value)
94
    {
95 10
        $result = MobileNumber::isValidNumber($value);
96 10
        return $result;
97
    }
98
    /**
99
     * Check key present in array or not
100
     * @param string $key
101
     * @param array  $array
102
     *
103
     * @return bool
104
     */
105 100
    public function isKeyExists($key, $array)
106
    {
107 100
        return array_key_exists($key, $array);
108
    }
109
    /**
110
     * This function for check message length allowed only 160 char, unicode allowed 70 char
111
     * @param array $buildSmsData
112
     * @param int $limit
113
     *
114
     * @throws ParameterException missing parameters or return empty
115
     * @return array $buildSmsData
116
     */
117 66
    public function checkMessageLength($key, $buildSmsData, $limit, $value, $category, $xmlDoc)
118
    {
119 66
        if (strlen($value) <= $limit) {
120 62
            $buildSmsData = $this->buildData($category, $key, $value, $buildSmsData, $xmlDoc, true, "TEXT");
121 62
            return $buildSmsData;
122
        } else {
123 4
            $message = "allowed below ".$limit." cheracters,but given length:_".strlen($value);
124 4
            throw ParameterException::invalidInput("message", "string", $value, $message);
125
        }
126
    }
127
    /**
128
     * This function for buildData normal SMS as well bulk SMS
129
     * @param int $category
130
     * @param string $key
131
     * @param int|string $value
132
     * @param array $buildSmsData
133
     * @param bool $isElement
134
     * @param string $attr
135
     *
136
     */
137 80
    public function buildData($category, $key, $value, $buildSmsData, $xmlDoc = null, $isElement = null, $attr = null)
138
    {
139 80
        if ($category === 1) {
140 80
            $buildSmsData = $this->addArray($key, $value, $buildSmsData);
141
        } else {
142
            if ($isElement) {
143
                $childAttr = $xmlDoc->createAttribute($attr);
144
                $childText = $xmlDoc->createTextNode($value);
145
                $buildSmsData->appendChild($childAttr)->appendChild($childText);
146
            } else {
147
                $buildSmsData = $this->addXml($buildSmsData, $xmlDoc, $key, $value);
148
            }
149
        }
150 80
        return $buildSmsData;
151
    }
152
    /**
153
     * This function add data to xml string
154
     *
155
     */
156
    public function addXml($buildSmsData, $xmlDoc, $key, $value)
157
    {
158
        if ($key === 'schtime') {
159
            $key = 'scheduledatetime';
160
        }
161 12
        $key = strtoupper(trim($key));
162
        //create a country element
163 12
        $buildSmsData->appendChild($xmlDoc->createElement($key, $value));
164 8
        return $buildSmsData;
165
    }
166 7
    /**
167
     * This function added int value in array
168 12
     * @param string $key
169 7
     * @param int|string $value
170
     * @param array $data
171 8
     * @param string $type
172
     *
173 7
     * @return array
174
     */
175
    public function addDataArray($key, $value, $data, $type)
176
    {
177
        if ($type === 'int') {
178
            $test = $this->isInterger($value);
179 20
        } else {
180
            $test = $this->isString($value);
181 20
        }
182
        if ($test) {
183 20
            $data = $this->addArray($key, $value, $data);
184
        } else {
185 20
            throw ParameterException::invalidArrtibuteType($key, $type, $value);
186
        }
187
        return $data;
188 20
    }
189
    /**
190
     * Check vaild Date Time
191
     * @return bool
192
     */
193
    public function isVaildDateTime($value)
194
    {
195
        if (Validation::isValidDateFirstFormat($value)) {
196
            return true;
197 80
        } elseif (Validation::isValidDateSecondFormat($value)) {
198
            return true;
199 80
        } elseif (Validation::isValidTimeStamp($value)) {
200 72
            return true;
201
        } else {
202 8
            return false;
203 8
        }
204
    }
205
    /**
206
     * Check afterminutes limits
207
     * @param string $afterMinutes
208
     *
209
     * @return bool
210
     */
211
    public function isVaildAfterMinutes($afterMinutes)
212 4
    {
213
        $value  = array('options' => array('min_range' => 10, 'max_range' => 20000));
214 4
        $result = filter_var($afterMinutes, FILTER_VALIDATE_INT, $value);
215 4
        return (bool) $result;
216 4
    }
217
    /**
218
     * This function Check value 0 or 1
219
     *
220
     * @return int
221
     */
222
    public function checkArray($value)
223 6
    {
224
        $responseFormat = array(0, 1);
225 6
        $value = in_array($value, $responseFormat) ? $value : null;
226 6
        return $value;
227 6
    }
228
    /**
229
     * This function check expect value present in array
230
     * @param string $value
231
     *
232
     */
233
    public function checkResponse($value)
234
    {
235
        $responseFormat = array('xml', 'json');
236
        $responseVal = strtolower($value);
237
        $value = in_array($responseVal, $responseFormat) ? $responseVal : null;
238
        return $value;
239
    }
240
}
241