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

Bandwidth::getApplicationId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace LeadThread\Sms\Drivers;
4
5
use Catapult\Client as Service;
6
use Catapult\Credentials;
7
use Catapult\Message;
8
use Catapult\PhoneNumber;
9
use Catapult\PhoneNumbers;
10
use Catapult\TextMessage;
11
use Config;
12
use LeadThread\Sms\Drivers\Driver;
13
use LeadThread\Sms\Exceptions\InvalidPhoneNumberException;
14
use LeadThread\Sms\Interfaces\PhoneSearchParams;
15
use LeadThread\Sms\Responses\Bandwidth as BandwidthResponse;
16
use LeadThread\Sms\Search\Bandwidth as Search;
17
18
class Bandwidth extends Driver
19
{
20
    protected $handle;
21
22 12
    public function __construct($secret, $token, $user_id)
23
    {
24 12
        $this->config = (class_exists("Config") ? Config::get('sms.bandwidth') : []);
25 12
        $cred = new Credentials($user_id, $token, $secret);
26 12
        $this->handle = new Service($cred);
27
        
28
        // Turn off the logger
29 12
        \Catapult\Log::on(false);
30 12
    }
31
32
    public function send($msg, $to, $from, $callback = null)
33
    {
34
        return new BandwidthResponse(new Message(array(
35
            "from" => new PhoneNumber($from),
36
            "to" => new PhoneNumber($to),
37
            "text" => $msg,
38
            "callbackUrl" => $callback,
39
            "fallbackUrl" => $this->getFallbackUrl(),
40
        )));
41
    }
42
43
    public function searchNumber(PhoneSearchParams $search)
44
    {
45
        $response = (new PhoneNumbers())->listLocal($search->toArray());
46
        return new BandwidthResponse($response);
47
    }
48
49
    public function buyNumber($phone)
50
    {
51
        $response = (new PhoneNumbers())->allocate([
52
            "number" => $phone,
53
            "applicationId" => $this->getApplicationId(),
54
        ]);
55
        return new BandwidthResponse($response);
56
    }
57
58
    public function sellNumber($phone)
59
    {
60
        throw new \Exception("Error Processing Request", 1);
61
    }
62
63
    protected function getFallbackUrl()
64
    {
65
        return array_key_exists("fallback_url", $this->config) ? $this->config["fallback_url"] : null;
66
    }
67
68
    protected function getApplicationId()
69
    {
70
        return array_key_exists("application_id", $this->config) ? $this->config["application_id"] : null;
71
    }
72
}
73