Passed
Push — master ( 520379...c91c51 )
by Anton
07:49 queued 10s
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 Client|array|string
17
     */
18
    public $httpClient;
19
20
    /**
21
     * @var string
22
     */
23
    public $apiUrl = "https://api.telegram.org/";
24
25
    /**
26
     * @var string
27
     */
28
    public $bot_id;
29
30
    /**
31
     * @var string
32
     */
33
    public $botToken;
34
35
    /**
36
     * @var string
37
     */
38
    public $parseMode = null;
39
40
    const PARSE_MODE_HTML = "HTML";
41
42
    const PARSE_MODE_MARKDOWN = "Markdown";
43
44
    /**
45
     * @var bool
46
     * If you need to change silentMode, you can use this code before calling telegram channel
47
     *
48
     * \Yii::$container->set('\app\additional\notification\TelegramChannel', [
49
     *                         'silentMode' => true,
50
     * ]);
51
     */
52
    public $silentMode = false;
53
54
    /**
55
     * @throws \yii\base\InvalidConfigException
56
     */
57
    public function init()
58
    {
59
        parent::init();
60
61
        if(!isset($this->bot_id) || !isset($this->botToken)){
62
            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...
63
        }
64
65
        if (!isset($this->httpClient)) {
66
            $this->httpClient = [
67
                '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

67
                '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...
68
                'baseUrl' => $this->apiUrl
69
            ];
70
        }
71
        $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

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