Passed
Push — master ( e59495...facc33 )
by Isaac
02:07
created

OutboundSMSRequest::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 4
dl 0
loc 13
ccs 7
cts 8
cp 0.875
crap 3.0175
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Mediumart\Orange\SMS\Http\Requests;
4
5
use Exception;
6
use Mediumart\Orange\SMS\Http\SMSClientRequest;
7
8
class OutboundSMSRequest extends SMSClientRequest
9
{
10
    /**
11
     * Request body
12
     *
13
     * @var array
14
     */
15
    protected $body;
16
17
    /**
18
     * Recipient number
19
     *
20
     * @var string
21
     */
22
    protected $sender;
23
24
    /**
25
     * OutboundSMSObjectRequest constructor.
26
     * @param $message
27
     * @param $recipientNumber
28
     * @param $senderNumber
29
     * @param $senderName
30
     * @throws \Exception
31
     */
32 1
    public function __construct($message, $recipientNumber, $senderNumber, $senderName = null)
33
    {
34 1
        $this->throwsExceptionIfEmpty($recipientNumber, $senderNumber);
35
36 1
        $this->body = ['outboundSMSMessageRequest' => [
37 1
               'address' => 'tel:'.$this->normalizePhoneNumber($recipientNumber),
38 1
               'senderAddress' => $this->sender = 'tel:'.$this->normalizePhoneNumber($senderNumber),
39 1
               'outboundSMSTextMessage' => [ 'message' => $message ?: '']
40
           ]
41
        ];
42
43 1
        if ($senderName) {
44
            $this->body['outboundSMSMessageRequest']['senderName'] = urlencode($senderName);
45
        }
46 1
    }
47
48
    /**
49
     * @inherit
50
     *
51
     * @return string
52
     */
53 1
    public function method()
54
    {
55 1
        return 'POST';
56
    }
57
58
    /**
59
     * @inherit
60
     *
61
     * @return string
62
     * @throws \Exception
63
     */
64 1
    public function uri()
65
    {
66 1
        return static::BASE_URI."/smsmessaging/v1/outbound/".urlencode($this->sender)."/requests";
67
    }
68
69
    /**
70
     * @inherit
71
     *
72
     * @return array
73
     */
74 1
    public function options()
75
    {
76
        return [
77 1
            'headers' => ["Content-Type" => "Application/json"],
78 1
            'body' => json_encode($this->body)
79
        ];
80
    }
81
82
    /**
83
     * @param $recipientNumber
84
     * @param $senderNumber
85
     * @throws \Exception
86
     */
87 1
    private function throwsExceptionIfEmpty($recipientNumber, $senderNumber)
88
    {
89 1
        if (empty($senderNumber)) {
90
            throw new Exception('Missing Sender number');
91
        }
92
93 1
        if (empty($recipientNumber)) {
94
            throw new Exception('Missing Recipient number');
95
        }
96 1
    }
97
}
98