Passed
Push — master ( 3eb6ea...b5f9b2 )
by Anton
01:44
created

SlackChannel   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 15
dl 0
loc 37
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 11 1
A init() 0 9 2
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
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
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');
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
        $webhookUrl = $recipient->routeNotificationFor('slack');
49
        $text = "*{$message->subject}*\n{$message->body}";
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
50
        return $this->httpClient->createRequest()
51
            ->setMethod('post')
52
            ->setUrl($webhookUrl)
53
            ->setData(ArrayHelper::merge(['text' => $text], $message->arguments))
54
            ->send();
55
    }
56
57
}