Driver::to()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
nc 4
cc 3
nop 1
1
<?php
2
3
namespace Tzsk\Sms\Abstracts;
4
5
use Tzsk\Sms\Contracts\DriverInterface;
6
7
abstract class Driver implements DriverInterface
8
{
9
    /**
10
     * To Numbers array.
11
     *
12
     * @var array
13
     */
14
    protected $recipients = [];
15
16
    /**
17
     * Message body.
18
     *
19
     * @var string
20
     */
21
    protected $body = '';
22
23
    /**
24
     * Driver constructor.
25
     *
26
     * @param $settings
27
     */
28
    abstract public function __construct($settings);
29
30
    /**
31
     * String or Array of numbers.
32
     *
33
     * @param $numbers string|array
34
     * @return $this
35
     * @throws \Exception
36
     */
37
    public function to($numbers)
38
    {
39
        $recipients = is_array($numbers) ? $numbers : [$numbers];
40
        $recipients = array_map(function ($item) {
41
            return trim($item);
42
        }, array_merge($this->recipients, $recipients));
43
44
        $this->recipients = array_values(array_filter($recipients));
45
46
        if (count($this->recipients) < 1) {
47
            throw new \Exception('Message recipient could not be empty.');
48
        }
49
50
        return $this;
51
    }
52
53
    /**
54
     * Set text message body.
55
     *
56
     * @param $message string
57
     * @return $this
58
     * @throws \Exception
59
     */
60
    public function message($message)
61
    {
62
        if (! is_string($message)) {
63
            throw new \Exception('Message text should be a string.');
64
        }
65
        if (trim($message) == '') {
66
            throw new \Exception('Message text could not be empty.');
67
        }
68
        $this->body = $message;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Send the message
75
     *
76
     * @return object
77
     */
78
    abstract public function send();
79
}
80