Passed
Push — master ( 19fefc...992cb1 )
by Anton
04:17
created

TwilioChannel::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Anton Tuyakhov <[email protected]>
4
 */
5
namespace tuyakhov\notifications\channels;
6
7
use tuyakhov\notifications\messages\SmsMessage;
8
use tuyakhov\notifications\NotifiableInterface;
9
use tuyakhov\notifications\NotificationInterface;
10
use yii\base\Component;
11
use yii\di\Instance;
12
use yii\httpclient\Client;
13
14
class TwilioChannel extends Component implements ChannelInterface
15
{
16
    public $baseUrl = 'https://api.twilio.com/2010-04-01';
17
    
18
    public $accountSid;
19
    
20
    public $authToken;
21
    
22
    public $from;
23
24
    /**
25
     * @var Client
26
     */
27
    public $httpClient;
28
29
    public function init()
30
    {
31
        parent::init();
32
        $this->httpClient = Instance::ensure($this->httpClient, 'yii\httpclient\Client');
33
    }
34
35
    public function getUri()
36
    {
37
        return sprintf('%s/%s/%s', 'Accounts', $this->accountSid, 'Messages.json');
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function send(NotifiableInterface $recipient, NotificationInterface $notification)
44
    {
45
        /** @var SmsMessage $message */
46
        $message = $notification->exportFor('sms');
47
        $data = [
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
48
            'From' => isset($message->from) ? $message->from : $this->from,
49
            'To' => $recipient->routeNotificationFor('sms'),
50
            'Body' => $message->body
51
        ];
52
        if (isset($message->mediaUrl)) {
53
            $data['MedialUrl'] = $message->mediaUrl;
54
        }
55
        $this->httpClient
56
            ->createRequest()
57
            ->setMethod('post')
58
            ->setUrl($this->getUri())
59
            ->addHeaders(['Authorization' => 'Basic ' . base64_encode("{$this->accountSid}:{$this->authToken}")])
60
            ->setData($data)
61
            ->send();
62
    }
63
64
}