Issues (14)

src/channels/TwilioChannel.php (2 issues)

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
/**
15
 * Sending an SMS or MMS using Twilio REST API.
16
 * 
17
 * ```php
18
 * [
19
 *      'components' => [
20
 *          'notifier' => [
21
 *              'class' => '\tuyakhov\notifications\Notifier',
22
 *              'channels' => [
23
 *                  'sms' => [
24
 *                      'class' => '\tuyakhov\notifications\channels\TwilioChannel,
25
 *                      'accountSid' => '...',
26
 *                      'authToken' => '...',
27
 *                      'from' => '+1234567890'
28
 *                  ]
29
 *              ],
30
 *          ],
31
 *      ],
32
 * ]
33
 * ```
34
 */
35
class TwilioChannel extends Component implements ChannelInterface
36
{
37
    /**
38
     * @var string
39
     */
40
    public $baseUrl = 'https://api.twilio.com/2010-04-01';
41
42
    /**
43
     * A Twilio account SID
44
     * @var string
45
     */
46
    public $accountSid;
47
48
    /**
49
     * A Twilio account auth token
50
     * @var string
51
     */
52
    public $authToken;
53
54
    /**
55
     * A Twilio phone number (in E.164 format) or alphanumeric sender ID enabled for the type of message you wish to send.
56
     * @var string
57
     */
58
    public $from;
59
60
    /**
61
     * @var Client|array|string
62
     */
63
    public $httpClient;
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function init()
69
    {
70
        parent::init();
71
        if (!isset($this->httpClient)) {
72
            $this->httpClient = [
73
                'class' => Client::className(),
0 ignored issues
show
Deprecated Code introduced by Anton Tuyakhov
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

73
                'class' => /** @scrutinizer ignore-deprecated */ Client::className(),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
74
                'baseUrl' => $this->baseUrl
75
            ];
76
        }
77
        $this->httpClient = Instance::ensure($this->httpClient, Client::className());
0 ignored issues
show
Deprecated Code introduced by Anton Tuyakhov
The function yii\base\BaseObject::className() has been deprecated: since 2.0.14. On PHP >=5.5, use `::class` instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
        $this->httpClient = Instance::ensure($this->httpClient, /** @scrutinizer ignore-deprecated */ Client::className());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
78
    }
79
80
    /**
81
     * The Messages list resource URI
82
     * @return string
83
     */
84
    public function getUri()
85
    {
86
        return sprintf('%s/%s/%s', 'Accounts', $this->accountSid, 'Messages.json');
87
    }
88
89
    /**
90
     * @inheritdoc
91
     */
92
    public function send(NotifiableInterface $recipient, NotificationInterface $notification)
93
    {
94
        /** @var SmsMessage $message */
95
        $message = $notification->exportFor('sms');
96
        $data = [
97
            'From' => isset($message->from) ? $message->from : $this->from,
98
            'To' => $recipient->routeNotificationFor('sms'),
99
            'Body' => $message->body
100
        ];
101
        if (isset($message->mediaUrl)) {
102
            $data['MedialUrl'] = $message->mediaUrl;
103
        }
104
        return $this->httpClient
105
            ->createRequest()
106
            ->setMethod('post')
107
            ->setUrl($this->getUri())
108
            ->addHeaders(['Authorization' => 'Basic ' . base64_encode("{$this->accountSid}:{$this->authToken}")])
109
            ->setData($data)
110
            ->send();
111
    }
112
113
}