Message   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%
Metric Value
wmc 19
lcom 1
cbo 0
dl 0
loc 213
ccs 57
cts 57
cp 1
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getPhones() 0 3 1
A setPhones() 0 4 1
A getText() 0 3 1
A getTransactionId() 0 3 1
A setTransactionId() 0 4 1
A getPrefix() 0 3 1
A setPrefix() 0 4 1
A getCharset() 0 3 1
A setCharset() 0 4 1
A getUserDataHeader() 0 3 1
A setUserDataHeader() 0 4 1
A addPhone() 0 5 1
A __construct() 0 6 1
B export() 0 22 6
1
<?php
2
/*
3
 * Copyright 2015 Topface, LLC <[email protected]>
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace TopfaceLibrary\SmsOnline\Bulk;
19
20
/**
21
 * Message class
22
 * @author alxmsl
23
 */
24
final class Message {
25
    /**
26
     * Message statuses
27
     */
28
    const STATUS_DELIVERED     = 0, // the message was delivered to the subscriber
29
          STATUS_BUFFERED      = 1, // the message is queued at SMSC
30
          STATUS_ABSENT        = 2, // the subscriber is out of coverage. Message is queued
31
          STATUS_PREPARING     = 3, // the message is being prepared for delivery
32
          STATUS_UNKNOWN       = 4, // no reply from mobile network operator
33
          STATUS_NOT_DELIVERED = -1, // message was not delivered
34
          STATUS_EXPIRED       = -2, // message expired and deleted from the SMSC
35
          STATUS_REJECTED      = -3; // delivery rejected by the mobile network operator
36
37
    /**
38
     * Maximum message delay, seconds
39
     */
40
    const MAX_DELAY = 10080;
41
42
    /**
43
     * Message type: text or binary
44
     */
45
    const TYPE_TEXT   = 0,
46
          TYPE_BINARY = 1;
47
48
    /**
49
     * Delivery report constants
50
     */
51
    const NO_DELIVERY_REPORT = 0,
52
          DELIVERY_REPORT    = 1;
53
54
    /**
55
     * @var int message type
56
     */
57
    private $type = self::TYPE_TEXT;
58
59
    /**
60
     * @var int delivery message delay
61
     */
62
    private $delay = 0;
63
64
    /**
65
     * @var bool need delivery message report or not
66
     */
67
    private $needDeliveryReport = false;
68
69
    /**
70
     * @var int[] phone numbers for message
71
     */
72
    private $phones = [];
73
74
    /**
75
     * @return int[] phone numbers for message
76
     */
77 4
    public function getPhones() {
78 4
        return $this->phones;
79
    }
80
81
    /**
82
     * @param int[] $phones phone numbers for message
83
     * @return $this self instance
84
     */
85 1
    public function setPhones(array $phones) {
86 1
        $this->phones = $phones;
87 1
        return $this;
88
    }
89
90
    /**
91
     * Add phone for delivering method
92
     * @param int $phone phone number
93
     * @return $this self instance
94
     */
95 3
    public function addPhone($phone) {
96 3
        $this->phones[] = (int) $phone;
97 3
        $this->phones = array_unique($this->phones);
98 3
        return $this;
99
    }
100
101
    /**
102
     * @var string message text
103
     */
104
    private $text = '';
105
106
    /**
107
     * @return string message text
108
     */
109 4
    public function getText() {
110 4
        return $this->text;
111
    }
112
113
    /**
114
     * @var string transaction identifier
115
     */
116
    private $transactionId = '';
117
118
    /**
119
     * @return string transaction identifier
120
     */
121 3
    public function getTransactionId() {
122 3
        return $this->transactionId;
123
    }
124
125
    /**
126
     * @param string $transactionId transaction identifier
127
     * @return $this self instance
128
     */
129 2
    public function setTransactionId($transactionId) {
130 2
        $this->transactionId = (string) $transactionId;
131 2
        return $this;
132
    }
133
134
    /**
135
     * @var null|string session mode prefix
136
     */
137
    private $prefix = null;
138
139
    /**
140
     * @return string|null session mode prefix
141
     */
142 3
    public function getPrefix() {
143 3
        return $this->prefix;
144
    }
145
146
    /**
147
     * @param string $prefix session mode prefix
148
     * @return $this self instance
149
     */
150 2
    public function setPrefix($prefix) {
151 2
        $this->prefix = (string) $prefix;
152 2
        return $this;
153
    }
154
155
    /**
156
     * @var string message charset
157
     */
158
    private $charset = 'UTF-8';
159
160
    /**
161
     * @return string message charset
162
     */
163 3
    public function getCharset() {
164 3
        return $this->charset;
165
    }
166
167
    /**
168
     * @param string $charset message charset
169
     * @return $this self instance
170
     */
171 2
    public function setCharset($charset) {
172 2
        $this->charset = (string) $charset;
173 2
        return $this;
174
    }
175
176
    /**
177
     * @var null|string message UDH
178
     */
179
    private $userDataHeader = null;
180
181
    /**
182
     * @return null|string message UDH
183
     */
184 3
    public function getUserDataHeader() {
185 3
        return $this->userDataHeader;
186
    }
187
188
    /**
189
     * @param string $userDataHeader UDH for message
190
     * @return $this self instance
191
     */
192 2
    public function setUserDataHeader($userDataHeader) {
193 2
        $this->userDataHeader = (string) $userDataHeader;
194 2
        return $this;
195
    }
196
197
    /**
198
     * @param string $text message text
199
     * @param bool $needDeliveryReport need delivery report or not
200
     * @param int $delay timeout for message delivering, seconds
201
     * @param int $type message type
202
     */
203 4
    public function __construct($text, $needDeliveryReport = false, $delay = 0, $type = self::TYPE_TEXT) {
204 4
        $this->delay              = min($delay, self::MAX_DELAY);
205 4
        $this->needDeliveryReport = (bool) $needDeliveryReport;
206 4
        $this->text               = (string) $text;
207 4
        $this->type               = $type;
208 4
    }
209
210
    /**
211
     * Export message state for sending method
212
     * @return string[] message state
213
     */
214 2
    public function export() {
215
        $data = [
216 2
            'charset=' . rawurlencode($this->getCharset()),
217 2
            'delay=' . $this->delay,
218 2
            'dlr=' . ($this->needDeliveryReport ? self::DELIVERY_REPORT : self::NO_DELIVERY_REPORT),
219 2
            'hex=' . $this->type,
220 2
            'txt=' . $this->getText(),
221 2
        ];
222 2
        foreach ($this->getPhones() as $phone) {
223 1
            $data[] = 'phone=' . rawurlencode($phone);
224 2
        }
225 2
        if (!empty($this->getTransactionId())) {
226 1
            $data[] = 'p_transaction_id=' . rawurlencode($this->getTransactionId());
227 1
        }
228 2
        if (!is_null($this->getPrefix())) {
229 1
            $data[] = 'pref=' . rawurlencode($this->getPrefix());
230 1
        }
231 2
        if (!is_null($this->getUserDataHeader())) {
232 1
            $data[] = 'udh=' . $this->getUserDataHeader();
233 1
        }
234 2
        return $data;
235
    }
236
}
237