Completed
Push — master ( 976e71...5eaed6 )
by Muhammad
02:53
created

GammuMessage::parseLongMessage()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
ccs 0
cts 12
cp 0
rs 8.8571
cc 2
eloc 19
nc 2
nop 1
crap 6
1
<?php
2
3
namespace NotificationChannels\Gammu;
4
5
class GammuMessage
6
{
7
    const VERSION = '0.0.1';
8
9
    /**
10
     * @var array Params payload.
11
     */
12
    public $payload = [];
13
14
    /**
15
     * @var array Multipart chunks.
16
     */
17
    public $multiparts = [];
18
19
    /**
20
     * @param string $content
21
     *
22
     * @return static
23
     */
24
    public static function create($content = '')
25
    {
26
        return new static($content);
27
    }
28
29
    /**
30
     * Create a new message instance.
31
     *
32
     * @param string $content
33
     */
34
    public function __construct($content = '')
35
    {
36
        $this->content($content);
37
        $this->payload['CreatorID'] = class_basename($this).'/'.self::VERSION;
38
        $this->payload['MultiPart'] = 'false';
39
    }
40
41
    /**
42
     * Destination phone number.
43
     *
44
     * @param $phoneNumber
45
     *
46
     * @return $this
47
     */
48
    public function to($phoneNumber)
49
    {
50
        $this->payload['DestinationNumber'] = $phoneNumber;
51
52
        return $this;
53
    }
54
55
    /**
56
     * SMS message.
57
     *
58
     * @param $content
59
     *
60
     * @return $this
61
     */
62
    public function content($content)
63
    {
64
        // Check if long SMS
65
        if (strlen($content) > 160) {
66
            $this->parseLongMessage($content);
67
        } else {
68
            $this->payload['TextDecoded'] = $content;
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * Sender Phone ID.
76
     *
77
     * @param $phoneId
78
     *
79
     * @return $this
80
     */
81
    public function sender($phoneId = null)
82
    {
83
        $this->payload['SenderID'] = $phoneId;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Determine if Sender Phone ID is not given.
90
     *
91
     * @return bool
92
     */
93
    public function senderNotGiven()
94
    {
95
        return ! isset($this->payload['SenderID']);
96
    }
97
98
    /**
99
     * Determine if Destination Phone Number is not given.
100
     *
101
     * @return bool
102
     */
103
    public function destinationNotGiven()
104
    {
105
        return ! isset($this->payload['DestinationNumber']);
106
    }
107
108
    /**
109
     * Returns params payload.
110
     *
111
     * @return array
112
     */
113
    public function toArray()
114
    {
115
        return $this->payload;
116
    }
117
118
    /**
119
     * Returns multipart chunks.
120
     *
121
     * @return array
122
     */
123
    public function getMultipartChunks()
124
    {
125
        return $this->multiparts;
126
    }
127
128
    /**
129
     * Generate UDH part for long SMS.
130
     *
131
     * @link https://en.wikipedia.org/wiki/Concatenated_SMS#Sending_a_concatenated_SMS_using_a_User_Data_Header
132
     *
133
     * @return string
134
     */
135
    protected function generateUDH($total = 2, $sequence = 2, $ref = 0)
136
    {
137
        // Length of User Data Header, in this case 05
138
        $octet_1 = '05';
139
140
        // Information Element Identifier, equal to 00 (Concatenated short messages, 8-bit reference number)
141
        $octet_2 = '00';
142
143
        // Length of the header, excluding the first two fields; equal to 03
144
        $octet_3 = '03';
145
146
        // CSMS reference number, must be same for all the SMS parts in the CSMS
147
        $octet_4 = str_pad(dechex($ref), 2, '0', STR_PAD_LEFT);
148
149
        // Total number of parts
150
        $octet_5 = str_pad(dechex($total), 2, '0', STR_PAD_LEFT);
151
152
        // Part sequence
153
        $octet_6 = str_pad(dechex($sequence), 2, '0', STR_PAD_LEFT);
154
155
        $udh = implode('', [
156
            $octet_1, $octet_2, $octet_3, $octet_4, $octet_5, $octet_6,
157
        ]);
158
159
        return strtoupper($udh);
160
    }
161
    
162
    protected function parseLongMessage($content)
163
    {
164
        // Parse message to chunks
165
        // @ref: http://www.nowsms.com/long-sms-text-messages-and-the-160-character-limit
166
        $messages = str_split($content, 153);
167
        $messages = collect($messages);
168
        $messages_count = $messages->count();
169
170
        // Get first message
171
        $firstChunk = $messages->shift();
172
173
        // Generate UDH
174
        $ref = mt_rand(0, 255);
175
        $i = 1;
176
        $firstUDH = $this->generateUDH($messages_count, $i, $ref);
177
        ++$i;
178
179
        $this->payload['TextDecoded'] = $firstChunk;
180
        $this->payload['UDH'] = $firstUDH;
181
        $this->payload['MultiPart'] = 'true';
182
183
        foreach ($messages as $chunk) {
184
            array_push($this->multiparts, [
185
                'UDH' => $this->generateUDH($messages_count, $i, $ref),
186
                'TextDecoded' => $chunk,
187
                'SequencePosition' => $i,
188
            ]);
189
            ++$i;
190
        }
191
        
192
        return $this;
193
    }
194
}
195