Completed
Push — master ( 021ec3...a65a54 )
by Hilmi Erdem
04:23
created

JetSMSMessage::toArray()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Author: Hilmi Erdem KEREN
4
 * Date: 17/11/2016.
5
 */
6
namespace NotificationChannels\JetSMS;
7
8
use Carbon\Carbon;
9
10
/**
11
 * Class JetSMSMessage.
12
 */
13
final class JetSMSMessage implements JetSMSMessageInterface
14
{
15
    /**
16
     * The message content.
17
     *
18
     * @var string
19
     */
20
    private $content;
21
22
    /**
23
     * The number to be notified.
24
     *
25
     * @var string
26
     */
27
    private $number;
28
29
    /**
30
     * The message sender.
31
     *
32
     * @var string
33
     */
34
    private $originator;
35
36
    /**
37
     * The date message will be sent.
38
     *
39
     * @var Carbon|null
40
     */
41
    private $sendDate = null;
42
43
    /**
44
     * JetSMSMessage constructor.
45
     *
46
     * @param string             $content
47
     * @param string             $number
48
     * @param string|null        $originator
49
     * @param Carbon|string|null $sendDate
50
     */
51 10
    public function __construct($content, $number, $originator = null, $sendDate = null)
52
    {
53 10
        $this->content = $content;
54 10
        $this->number = $number;
55
56 10
        if (! is_null($originator)) {
57 2
            $this->originator = $originator;
58
        }
59
60 10
        if (! is_null($sendDate)) {
61 1
            $this->sendDate = $sendDate instanceof Carbon ? $sendDate : Carbon::createFromFormat($this->dateFormat(), $sendDate);
62
        }
63 10
    }
64
65
    /**
66
     * Get the message properties as array.
67
     *
68
     * @return array
69
     */
70 5
    public function toArray()
71
    {
72 5
        return array_filter([
73 5
            'Msisdns'        => $this->number(),
74 5
            'Messages'       => $this->content(),
75 5
            'SendDate'       => $this->sendDate() ? $this->sendDate()->format($this->dateFormat()) : null,
76 5
            'TransmissionID' => $this->originator(),
77
        ]);
78
    }
79
80
    /**
81
     * Convert the sms message to sms parameters.
82
     *
83
     * @return array
84
     */
85 8
    public function toRequestParams()
86
    {
87 8
        return $this->toArray();
88
    }
89
90
    /**
91
     * Get the short message.
92
     *
93
     * @return string
94
     */
95 5
    public function content()
96
    {
97 5
        return $this->content;
98
    }
99
100
    /**
101
     * Get the to number.
102
     *
103
     * @return string
104
     */
105 5
    public function number()
106
    {
107 5
        return $this->number;
108
    }
109
110
    /**
111
     * Get the outbox name of the message.
112
     *
113
     * @return string
114
     */
115 5
    public function originator()
116
    {
117 5
        return $this->originator;
118
    }
119
120
    /**
121
     * Get the send date of the short message.
122
     *
123
     * @return Carbon|null
124
     */
125
    public function sendDate()
126
    {
127
        return $this->sendDate;
128
    }
129
130
    /**
131
     * Property getter.
132
     *
133
     * @param  string $name
134
     * @return mixed
135
     */
136
    public function __get($name)
137
    {
138
        return $this->$name();
139
    }
140
141
    /**
142
     * Get the api date format.
143
     *
144
     * @return string
145
     */
146
    private function dateFormat()
147
    {
148
        return 'Y-m-d H:i:s';
149
    }
150
}
151