Completed
Pull Request — master (#15)
by Massimiliano
05:59
created

Sms   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 30
lcom 2
cbo 2
dl 0
loc 341
ccs 70
cts 70
cp 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Fazland\SkebbyRestClient\DataStructure;
5
6
use Fazland\SkebbyRestClient\Constant\ValidityPeriods;
7
use Fazland\SkebbyRestClient\Exception\InvalidDeliveryStartException;
8
use Fazland\SkebbyRestClient\Exception\InvalidValidityPeriodException;
9
10
/**
11
 * Represents an SMS.
12
 *
13
 * @author Massimiliano Braglia <[email protected]>
14
 */
15
class Sms
16
{
17
    private ?string $sender = null;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '?', expecting T_FUNCTION or T_CONST
Loading history...
18
19
    /**
20
     * @var string[]
21
     */
22
    private array $recipients;
23
24
    /**
25
     * @var string[][]
26
     */
27
    private array $recipientVariables;
28
29
    private string $text;
30
31
    private ?string $userReference = null;
32
33
    private ?\DateTimeInterface $deliveryStart = null;
34
35
    private ?\DateInterval $validityPeriod = null;
36
37
    /**
38
     * Sms constructor.
39
     */
40
    public function __construct()
41
    {
42
        $this->recipients = [];
43
        $this->recipientVariables = [];
44
    }
45
46
    /**
47
     * Creates a new instance of SMS.
48
     */
49
    public static function create(): self
50
    {
51
        return new static();
52
    }
53
54 14
    /**
55
     * Gets the sender.
56 14
     */
57 14
    public function getSender(): ?string
58 14
    {
59
        return $this->sender;
60
    }
61
62
    /**
63
     * Sets the sender.
64
     */
65 9
    public function setSender(string $sender): self
66
    {
67 9
        $this->sender = $sender;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Gets the recipients.
74
     *
75 8
     * @return string[]
76
     */
77 8
    public function getRecipients(): array
78
    {
79
        return $this->recipients;
80
    }
81
82
    /**
83
     * Sets the recipients.
84
     *
85
     * @param string[] $recipients
86
     */
87 1
    public function setRecipients(array $recipients): self
88
    {
89 1
        $this->recipients = $recipients;
90
91 1
        return $this;
92
    }
93
94
    /**
95
     * Adds a single recipient.
96
     */
97
    public function addRecipient(string $recipient): self
98
    {
99 9
        $this->recipients[] = $recipient;
100
101 9
        return $this;
102
    }
103
104
    /**
105
     * Removes a single recipient.
106
     */
107
    public function removeRecipient(string $recipient): self
108
    {
109
        $itemPosition = array_search($recipient, $this->recipients, true);
110
111 8
        if (false !== $itemPosition) {
112
            unset($this->recipients[$itemPosition]);
113 8
        }
114
115 8
        unset($this->recipientVariables[$recipient]);
116
117
        return $this;
118
    }
119
120
    /**
121
     * Whether the current sms has or not recipients.
122
     */
123
    public function hasRecipients(): bool
124
    {
125 6
        return ! empty($this->recipients);
126
    }
127 6
128
    /**
129 6
     * Gets the recipient variables.
130
     *
131
     * @return string[][]
132
     */
133
    public function getRecipientVariables(): array
134
    {
135
        return $this->recipientVariables;
136
    }
137
138
    /**
139 1
     * Sets the recipient variables for the recipient specified.
140
     *
141 1
     * @param string[] $recipientVariables
142
     */
143 1
    public function setRecipientVariables(string $recipient, array $recipientVariables): self
144 1
    {
145
        $this->recipientVariables[$recipient] = $recipientVariables;
146
147 1
        return $this;
148
    }
149 1
150
    /**
151
     * Adds a single recipient variable for the specified recipient.
152
     */
153
    public function addRecipientVariable(
154
        string $recipient,
155
        string $recipientVariable,
156
        string $recipientVariableValue
157 9
    ): self {
158
        if (! isset($this->recipientVariables[$recipient])) {
159 9
            $this->recipientVariables[$recipient] = [];
160
        }
161
162
        $this->recipientVariables[$recipient][$recipientVariable] = $recipientVariableValue;
163
164
        return $this;
165
    }
166
167 10
    /**
168
     * Removes the recipient variable for the recipient specified.
169 10
     */
170
    public function removeRecipientVariable(string $recipient, string $recipientVariable): self
171
    {
172
        unset($this->recipientVariables[$recipient][$recipientVariable]);
173
174
        return $this;
175
    }
176
177
    /**
178
     * Whether the current sms has or not recipient variables.
179
     */
180 1
    public function hasRecipientVariables(): bool
181
    {
182 1
        return ! empty($this->recipientVariables);
183
    }
184 1
185
    /**
186
     * Clears the recipient variables.
187
     */
188
    public function clearRecipientVariables(): self
189
    {
190
        $this->recipientVariables = [];
191
192
        return $this;
193
    }
194
195
    /**
196 5
     * Gets the text.
197
     */
198
    public function getText(): string
199
    {
200
        return $this->text;
201 5
    }
202 5
203
    /**
204
     * Sets the text.
205 5
     */
206
    public function setText(string $text): self
207 5
    {
208
        $this->text = $text;
209
210
        return $this;
211
    }
212
213
    /**
214
     * Gets the user reference.
215
     */
216
    public function getUserReference(): ?string
217
    {
218 1
        return $this->userReference;
219
    }
220 1
221
    /**
222 1
     * Sets the user reference.
223
     */
224
    public function setUserReference(string $userReference): self
225
    {
226
        $this->userReference = $userReference;
227
228
        return $this;
229
    }
230 8
231
    /**
232 8
     * Gets the delivery start.
233
     */
234
    public function getDeliveryStart(): ?\DateTimeInterface
235
    {
236
        return $this->deliveryStart;
237
    }
238
239
    /**
240 8
     * @throws InvalidDeliveryStartException
241
     */
242 8
    public function setDeliveryStart(?\DateTimeInterface $deliveryStart = null): self
243
    {
244 8
        if (null !== $deliveryStart && $deliveryStart < date_create_from_format('U', (string) time())) {
245
            throw new InvalidDeliveryStartException();
246
        }
247
248
        $this->deliveryStart = $deliveryStart;
249
250
        return $this;
251
    }
252 8
253
    /**
254 8
     * Gets the validity period.
255
     */
256
    public function getValidityPeriod(): ?\DateInterval
257
    {
258
        return $this->validityPeriod;
259
    }
260
261
    /**
262
     * Sets the validity period.
263
     *
264 9
     * @throws InvalidValidityPeriodException
265
     */
266 9
    public function setValidityPeriod(?\DateInterval $validityPeriod = null): self
267
    {
268 9
        if (null !== $validityPeriod &&
269
            ($validityPeriod->i < ValidityPeriods::MIN || $validityPeriod->i > ValidityPeriods::MAX)
270
        ) {
271
            throw new InvalidValidityPeriodException();
272
        }
273
274
        $this->validityPeriod = $validityPeriod;
275
276 8
        return $this;
277
    }
278
}
279