Passed
Pull Request — master (#23)
by Anton
08:44 queued 13s
created

TelegramChannel::createUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace tuyakhov\notifications\channels;
4
5
use tuyakhov\notifications\NotifiableInterface;
6
use tuyakhov\notifications\NotificationInterface;
7
use tuyakhov\notifications\messages\TelegramMessage;
8
use yii\base\Component;
9
use yii\di\Instance;
10
use yii\helpers\ArrayHelper;
11
use yii\httpclient\Client;
12
13
class TelegramChannel extends Component implements ChannelInterface
14
{
15
    /**
16
     * @var BaseActiveRecord|string
0 ignored issues
show
Bug introduced by
The type tuyakhov\notifications\channels\BaseActiveRecord was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
     */
18
    public $model = 'app\additional\notification\Notification';
19
20
    /**
21
     * @var Client|array|string
22
     */
23
    public $httpClient;
24
25
    /**
26
     * @var string
27
     */
28
    public $api_url = "https://api.telegram.org/bot";
29
30
    /**
31
     * @var string
32
     */
33
    public $bot_id;
34
35
    /**
36
     * @var string
37
     */
38
    public $bot_token;
39
40
    /**
41
     * @var string
42
     */
43
    public $parse_mode = null;
44
45
    const PARSE_MODE_HTML = "HTML";
46
47
    const PARSE_MODE_MARKDOWN = "Markdown";
48
49
    /**
50
     * @var bool
51
     * If you need to change silent_mode, you can use this code before calling telegram channel
52
     *
53
     * \Yii::$container->set('\app\additional\notification\TelegramChannel', [
54
     *                         'silent_mode' => true,
55
     * ]);
56
     */
57
    public $silent_mode = false;
58
59
    /**
60
     * @throws \yii\base\InvalidConfigException
61
     */
62
    public function init()
63
    {
64
        parent::init();
65
66
        if(!isset($this->bot_id) || !isset($this->bot_token)){
67
            throw new InvalidConfigException("Bot id or bot token is undefined");
0 ignored issues
show
Bug introduced by
The type tuyakhov\notifications\c...\InvalidConfigException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
68
        }
69
70
        if (!isset($this->httpClient)) {
71
            $this->httpClient = [
72
                '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

72
                '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...
73
            ];
74
        }
75
        $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

75
        $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...
76
    }
77
78
79
    /**
80
     * @param NotifiableInterface $recipient
81
     * @param NotificationInterface $notification
82
     * @return mixed
83
     * @throws \Exception
84
     */
85
    public function send(NotifiableInterface $recipient, NotificationInterface $notification)
86
    {
87
        /** @var TelegramMessage $message */
88
        $message = $notification->exportFor('telegram');
89
        $text = "*{$message->subject}*\n{$message->body}";
90
        $chat_id = $recipient->routeNotificationFor('telegram');
91
        if(!$chat_id){
92
            throw new \Exception("User doesn't have telegram_id");
93
        }
94
95
        $data = [
96
            "chat_id" => $chat_id,
97
            "text" => $text,
98
            'disable_notification' => $this->silent_mode
99
        ];
100
        if($this->parse_mode != null){
101
            $data["parse_mode"] = $this->parse_mode;
102
        }
103
104
        $resultUrl = $this->createUrl();
105
        return $this->httpClient->createRequest()
106
            ->setMethod('post')
107
            ->setUrl($resultUrl)
108
            ->setData($data)
109
            ->send();
110
    }
111
112
    private function createUrl()
113
    {
114
        return $this->api_url . $this->bot_id . ":" . $this->bot_token . "/sendmessage";
115
    }
116
}