Passed
Push — master ( e6338b...565ec3 )
by Alexey
03:45
created

MessageSender::sendMessageToChat()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 23
cp 0
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 2
nop 6
crap 6
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram;
4
5
use GuzzleHttp\Exception\ClientException;
6
use Skobkin\Bundle\PointToolsBundle\Entity\Telegram\Account;
7
use unreal4u\TelegramAPI\Telegram\Methods\SendMessage;
8
use unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardMarkup;
9
use unreal4u\TelegramAPI\TgLog;
10
11
/**
12
 * Service which sends simple messages to Telegram users
13
 */
14
class MessageSender
15
{
16
    const PARSE_MODE_NOPARSE = '';
17
    const PARSE_MODE_MARKDOWN = 'Markdown';
18
    const PARSE_MODE_HTML5 = 'HTML';
19
20
    /**
21
     * @var TgLog
22
     */
23
    private $client;
24
25
    /**
26
     * @param TgLog $client
27
     */
28
    public function __construct(TgLog $client)
29
    {
30
        $this->client = $client;
31
    }
32
33
    public function sendMessageToUser(
34
        Account $account,
35
        string $text,
36
        string $parseMode = self::PARSE_MODE_NOPARSE,
37
        ReplyKeyboardMarkup $keyboardMarkup = null,
38
        bool $disableWebPreview = false,
39
        bool $disableNotifications = false
40
    ): bool
41
    {
42
        return $this->sendMessageToChat($account->getChatId(), $text, $parseMode, $keyboardMarkup, $disableWebPreview, $disableNotifications);
43
    }
44
45
     public function sendMessageToChat(
46
        int $chatId,
47
        string $text,
48
        string $parseMode = self::PARSE_MODE_NOPARSE,
49
        ReplyKeyboardMarkup $keyboardMarkup = null,
50
        bool $disableWebPreview = false,
51
        bool $disableNotifications = false
52
    ): bool
53
    {
54
        $sendMessage = new SendMessage();
55
        $sendMessage->chat_id = (string) $chatId;
56
        $sendMessage->text = $text;
57
        $sendMessage->parse_mode = $parseMode;
58
        $sendMessage->disable_web_page_preview = $disableWebPreview;
59
        $sendMessage->disable_notification = $disableNotifications;
60
        $sendMessage->reply_markup = $keyboardMarkup;
61
62
        try {
63
            $this->client->performApiRequest($sendMessage);
64
65
            return true;
66
        } catch (ClientException $e) {
67
            return false;
68
        }
69
    }
70
}