Test Failed
Pull Request — master (#7)
by Zacchaeus
16:16 queued 01:09
created

Sms::setText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Djunehor\Sms\Concrete;
4
5
use GuzzleHttp\Client;
6
7
abstract class Sms
8
{
9
    protected $text;
10
    protected $username;
11
    protected $password;
12
    protected $recipients = [];
13
    private static $httpClient;
14
    protected $sender;
15
    protected $response;
16
    protected $client;
17
    protected $request;
18
19
    /**
20
     * @var \Exception
21
     */
22
    public $httpError;
23
24
    /**We want HTTP CLient instantiated
25
     * only once in entire app lifecycle
26
     * @return Client
27
     */
28
    public static function getInstance()
29
    {
30
        if (! self::$httpClient) {
31
            self::$httpClient = new Client();
32
        }
33
34
        return self::$httpClient;
35
    }
36
37
    /** Define SMS recipient(s).
38
     * @param $numbers string|array
39
     * @return $this
40
     */
41
    public function to($numbers)
42
    {
43
        $numbers = is_array($numbers) ? $numbers : func_get_args();
44
45
        if (count($numbers)) {
46
            $this->setRecipients($numbers);
47
        }
48
49
        return $this;
50
    }
51
52
    private function setRecipients($numbers)
53
    {
54
        foreach ($numbers as $number) {
55
            $this->recipients[] = $this->numberFormat($number);
56
        }
57
58
        return $this;
59
    }
60
61
    public function getRecipients(): array
62
    {
63
        return $this->recipients;
64
    }
65
66
    public function text($text = null)
67
    {
68
        if ($text) {
69
            $this->setText($text);
70
        }
71
72
        return $this;
73
    }
74
75
    private function numberFormat($number)
76
    {
77
        $number = (string) $number;
78
        $number = trim($number);
79
        $number = preg_replace("/\s|\+|-/", '', $number);
80
81
        return $number;
82
    }
83
84
    public function setText($text)
85
    {
86
        $this->text = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', trim($text));
87
88
        return $this;
89
    }
90
91
    public function getText()
92
    {
93
        return $this->text;
94
    }
95
96
    public function from($from)
97
    {
98
        $this->sender = $from;
99
100
        return $this;
101
    }
102
103
    public function getSender()
104
    {
105
        return $this->sender;
106
    }
107
108
    public function getResponse()
109
    {
110
        return $this->response;
111
    }
112
113
    public function getException()
114
    {
115
        return $this->httpError;
116
    }
117
118
    /**
119
     * Determines if it has any recipients.
120
     *
121
     * @return bool [description]
122
     */
123
    public function hasRecipients()
124
    {
125
        return property_exists($this, 'recipients') && ! empty($this->recipients);
126
    }
127
}
128