Completed
Push — master ( e2547a...b91078 )
by Tyler
04:34
created

Sms   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 64.1%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 105
ccs 25
cts 39
cp 0.641
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A getDriver() 0 8 2
A send() 0 8 2
A sendMany() 0 8 2
A sendArray() 0 8 2
A getSearchParams() 0 8 2
A searchAndBuyNumber() 0 4 1
A searchNumber() 0 4 1
A buyNumber() 0 4 1
A sellNumber() 0 4 1
1
<?php
2
3
namespace LeadThread\Sms;
4
5
use Config;
6
use LeadThread\Sms\Exceptions\InvalidPhoneNumberException;
7
use LeadThread\Sms\Factories\DriverFactory;
8
use LeadThread\Sms\Factories\SearchFactory;
9
use LeadThread\Sms\Interfaces\PhoneSearchParams;
10
use LeadThread\Sms\Interfaces\SendsSms;
11
use LeadThread\Sms\Search\Search;
12
13
class Sms
14
{
15
    protected $driver;
16
    protected $config;
17
18 33
    public function __construct(SendsSms $driver = null)
19
    {
20 33
        $this->config = (class_exists("Config") ? Config::get('sms') : []);
21 33
        $this->driver = $this->getDriver($driver);
22 33
    }
23
24
    /**
25
     * Returns a SMS driver instance
26
     * @param  mixed $driver An existing SMS driver instance to use
27
     * @return \LeadThread\Sms\Interfaces\SendsSms
28
     */
29 33
    protected function getDriver($driver = null)
30
    {
31 33
        if (!$driver instanceof SendsSms) {
32 6
            $factory = new DriverFactory;
33 6
            $driver = $factory->get($this->config['driver']);
0 ignored issues
show
Bug Compatibility introduced by
The expression $factory->get($this->config['driver']); of type LeadThread\Sms\Drivers\SendsSms adds the type LeadThread\Sms\Drivers\SendsSms to the return on line 35 which is incompatible with the return type documented by LeadThread\Sms\Sms::getDriver of type LeadThread\Sms\Interfaces\SendsSms.
Loading history...
34 6
        }
35 33
        return $driver;
36
    }
37
38
    /**
39
     * Sends an SMS message
40
     * @param  string $msg  The message to send
41
     * @param  mixed  $to   The number to send to
42
     * @param  number $from The number to send from
43
     * @return mixed        The response of the message
44
     */
45 27
    public function send($msg, $to, $from = null)
46
    {
47 27
        if (is_array($to)) {
48
            return $this->sendMany($msg, $to, $from);
49
        } else {
50 27
            return $this->driver->send($msg, $to, $from);
0 ignored issues
show
Bug introduced by
The call to send() misses a required argument $callback.

This check looks for function calls that miss required arguments.

Loading history...
51
        }
52
    }
53
54
    /**
55
     * Sends the same message from the same number to many phone numbers
56
     * @param  string $msg  The message to send
57
     * @param  array  $tos  An array of numbers to send to
58
     * @param  number $from The number to send from
59
     * @return array        An array of responses per number
60
     */
61 9
    public function sendMany($msg, array $tos, $from = null)
62
    {
63 9
        $resp = [];
64 9
        foreach ($tos as $to) {
65 9
            $resp[] = $this->send($msg, $to, $from);
66 9
        }
67 9
        return $resp;
68
    }
69
70
    /**
71
     * An array of SMS items to send
72
     * @param  array $data Must contain msg, to, and from keys per item
73
     * @return array       An array of responses per item
74
     */
75 9
    public function sendArray(array $data)
76
    {
77 9
        $resp = [];
78 9
        foreach ($data as $item) {
79 9
            $resp[] = $this->send($item['msg'], $item['to'], $item['from']);
80 9
        }
81 9
        return $resp;
82
    }
83
84
    protected function getSearchParams($search)
85
    {
86
        if (!$search instanceof PhoneSearchParams) {
87
            $f = new SearchFactory();
88
            return $f->get($this->config["driver"], $search);
89
        }
90
        return $search;
91
    }
92
93
    /**
94
     * Searches for a number and then purchases the first one it finds
95
     * @param  array $search Array of search options
96
     * @return \LeadThread\Sms\Responses\Response
97
     */
98
    public function searchAndBuyNumber($search)
99
    {
100
        return $this->driver->searchAndBuyNumber($this->getSearchParams($search));
101
    }
102
103
    public function searchNumber($search)
104
    {
105
        return $this->driver->searchNumber($this->getSearchParams($search));
106
    }
107
108
    public function buyNumber($number)
109
    {
110
        return $this->driver->buyNumber($number);
111
    }
112
113
    public function sellNumber($number)
114
    {
115
        return $this->driver->sellNumber($number);
116
    }
117
}
118