Issues (15)

src/channels/SlackChannel.php (2 issues)

1
<?php
2
/**
3
 * @copyright Anton Tuyakhov <[email protected]>
4
 */
5
6
namespace tuyakhov\notifications\channels;
7
8
9
use tuyakhov\notifications\messages\SlackMessage;
10
use tuyakhov\notifications\NotifiableInterface;
11
use tuyakhov\notifications\NotificationInterface;
12
use yii\base\Component;
13
use yii\di\Instance;
14
use yii\helpers\ArrayHelper;
15
use yii\httpclient\Client;
16
use yii\httpclient\Response;
17
18
class SlackChannel extends Component implements ChannelInterface
19
{
20
    /**
21
     * @var Client|array|string
22
     */
23
    public $httpClient;
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public function init()
29
    {
30
        parent::init();
31
        if (!isset($this->httpClient)) {
32
            $this->httpClient = [
33
                '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

33
                '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...
34
            ];
35
        }
36
        $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

36
        $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...
37
    }
38
39
    /**
40
     * @param NotifiableInterface $recipient
41
     * @param NotificationInterface $notification
42
     * @return Response
43
     */
44
    public function send(NotifiableInterface $recipient, NotificationInterface $notification)
45
    {
46
        /** @var SlackMessage $message */
47
        $message = $notification->exportFor('slack');
48
        $webhookUrl = $recipient->routeNotificationFor('slack');
49
        $text = "*{$message->subject}*\n{$message->body}";
50
        return $this->httpClient->createRequest()
51
            ->setMethod('post')
52
            ->setUrl($webhookUrl)
53
            ->setData(ArrayHelper::merge(['text' => $text], $message->arguments))
54
            ->send();
55
    }
56
57
}