Completed
Pull Request — master (#7)
by Hamoud
05:51
created

MobilyWsMessage::timeSend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
3
namespace NotificationChannels\MobilyWs;
4
5
use Carbon\Carbon;
6
use DateTime;
7
use DateTimeInterface;
8
use NotificationChannels\MobilyWs\Exceptions\CouldNotSendNotification;
9
10
class MobilyWsMessage
11
{
12
    /** @var string */
13
    public $text;
14
15
    /** @var Carbon */
16
    public $time;
17
18
    /**
19
     * Create new instance of mobilyWsMessage.
20
     *
21
     * @param string $text
22
     *
23
     * @return static
24
     */
25
    public static function create($text = '')
26
    {
27
        return new static($text);
28
    }
29
30
    /**
31
     * MobilyWsMessage constructor.
32
     *
33
     * @param string $text
34
     */
35
    public function __construct($text = '')
36
    {
37
        $this->text = $text;
38
    }
39
40
    /**
41
     * Set the Content of the SMS message.
42
     *
43
     * @param $text
44
     *
45
     * @return $this
46
     */
47
    public function text($text)
48
    {
49
        $this->text = $text;
50
51
        return $this;
52
    }
53
54
    /**
55
     * Set the message scheduled date and time.
56
     *
57
     * @param DateTime|Carbon|int $time
58
     *
59
     * @return $this
60
     *
61
     * @throws CouldNotSendNotification
62
     */
63
    public function time($time)
64
    {
65
        if ($time instanceof DateTimeInterface) {
66
            return $this->time($time->getTimestamp());
67
        }
68
69
        if (is_numeric($time)) {
70
            $this->time = Carbon::createFromTimestamp($time);
71
72
            return $this;
73
        }
74
75
        throw CouldNotSendNotification::withErrorMessage(
76
            sprintf('Time must be a timestamp or an object implementing DateTimeInterface. %s is given', gettype($time))
77
        );
78
    }
79
80
    /**
81
     * Get the message schedule date.
82
     *
83
     * @return string
84
     */
85
    public function dateSend()
86
    {
87
        if ($this->time) {
88
            return $this->time->format('m/d/Y');
89
        }
90
    }
91
92
    /**
93
     * Get the message schedule time.
94
     *
95
     * @return string
96
     */
97
    public function timeSend()
98
    {
99
        if ($this->time) {
100
            return $this->time->format('H:i:s');
101
        }
102
    }
103
}
104