Passed
Push — master ( 7d6da9...87fa1f )
by Zacchaeus
01:25 queued 10s
created

Sms::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
ccs 4
cts 4
cp 1
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 9
    public static function getInstance()
29
    {
30 9
        if (! self::$httpClient) {
31 1
            self::$httpClient = new Client();
32
        }
33
34 9
        return self::$httpClient;
35
    }
36
37
    /** Define SMS recipient(s)
38
     * @param $numbers string|array
39
     * @return $this
40
     */
41 7
    public function to($numbers)
42
    {
43 7
        $numbers = is_array($numbers) ? $numbers : func_get_args();
44
45 7
        if (count($numbers)) {
46 7
            $this->setRecipients($numbers);
47
        }
48
49 7
        return $this;
50
    }
51
52 7
    private function setRecipients($numbers)
53
    {
54 7
        foreach ($numbers as $number) {
55 7
            $this->recipients[] = $this->numberFormat($number);
56
        }
57
58 7
        return $this;
59
    }
60
61 3
    public function getRecipients() : array
62
    {
63 3
        return $this->recipients;
64
    }
65
66 4
    public function text($text = null)
67
    {
68 4
        if ($text) {
69 4
            $this->setText($text);
70
        }
71
72 4
        return $this;
73
    }
74
75 7
    private function numberFormat($number)
76
    {
77 7
        $number = (string) $number;
78 7
        $number = trim($number);
79 7
        $number = preg_replace("/\s|\+|-/", '', $number);
80
81 7
        return $number;
82
    }
83
84 4
    public function setText($text)
85
    {
86 4
        $this->text = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', trim($text));
87
88 4
        return $this;
89
    }
90
91 1
    public function getText()
92
    {
93 1
        return $this->text;
94
    }
95
96 4
    public function from($from)
97
    {
98 4
        $this->sender = $from;
99
100 4
        return $this;
101
    }
102
103 1
    public function getSender()
104
    {
105 1
        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 1
    public function hasRecipients()
124
    {
125 1
        return property_exists($this, 'recipients') && ! empty($this->recipients);
126
    }
127
}
128