Bandwidth::send()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 4
crap 2
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
            "receiptRequested" => "all",
41
        )));
42
    }
43
44
    public function searchNumber(PhoneSearchParams $search)
45
    {
46
        $response = (new PhoneNumbers())->listLocal($search->toArray());
47
        return new BandwidthResponse($response);
48
    }
49
50
    public function buyNumber($phone)
51
    {
52
        $response = (new PhoneNumbers())->allocate([
53
            "number" => $phone,
54
            "applicationId" => $this->getApplicationId(),
55
        ]);
56
        return new BandwidthResponse($response);
57
    }
58
59
    public function sellNumber($phone)
60
    {
61
        throw new \Exception("Error Processing Request", 1);
62
    }
63
64
    protected function getFallbackUrl()
65
    {
66
        return array_key_exists("fallback_url", $this->config) ? $this->config["fallback_url"] : null;
67
    }
68
69
    protected function getApplicationId()
70
    {
71
        return array_key_exists("application_id", $this->config) ? $this->config["application_id"] : null;
72
    }
73
}
74