MessageSender   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 100
ccs 0
cts 63
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A sendMassTemplatedMessage() 0 16 2
A sendTemplatedMessage() 0 14 1
A sendMessage() 0 11 1
A sendMessageToChat() 0 25 2
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\Abstracts\KeyboardMethods;
8
use unreal4u\TelegramAPI\Telegram\Methods\SendMessage;
9
use unreal4u\TelegramAPI\TgLog;
10
11
/**
12
 * Service which sends simple messages to Telegram users
13
 */
14
class MessageSender
15
{
16
    public const PARSE_PLAIN = '';
17
    public const PARSE_MARKDOWN = 'Markdown';
18
    public const PARSE_HTML5 = 'HTML';
19
20
    /** @var TgLog */
21
    private $client;
22
23
    /** @var \Twig_Environment */
24
    private $twig;
25
26
    /**
27
     * @param TgLog $client
28
     */
29
    public function __construct(TgLog $client, \Twig_Environment $twig)
30
    {
31
        $this->client = $client;
32
        $this->twig = $twig;
33
    }
34
35
    /**
36
     * @param Account[] $accounts
37
     * @param string $template
38
     * @param array $templateData
39
     * @param KeyboardMethods|null $keyboardMarkup
40
     * @param bool $disableWebPreview
41
     * @param bool $disableNotifications
42
     * @param string $parseMode
43
     */
44
    public function sendMassTemplatedMessage(
45
        array $accounts,
46
        string $template,
47
        array $templateData = [],
48
        KeyboardMethods $keyboardMarkup = null,
49
        bool $disableWebPreview = true,
50
        bool $disableNotifications = false,
51
        string $parseMode = self::PARSE_MARKDOWN
52
    ): void
53
    {
54
        $text = $this->twig->render($template, $templateData);
55
56
        foreach ($accounts as $account) {
57
            $this->sendMessage($account, $text, $parseMode, $keyboardMarkup, $disableWebPreview, $disableNotifications);
58
        }
59
    }
60
61
    public function sendTemplatedMessage(
62
        Account $account,
63
        string $template,
64
        array $templateData = [],
65
        KeyboardMethods $keyboardMarkup = null,
66
        bool $disableWebPreview = true,
67
        bool $disableNotifications = false,
68
        string $parseMode = self::PARSE_MARKDOWN
69
    ): bool
70
    {
71
        $text = $this->twig->render($template, $templateData);
72
73
        return $this->sendMessage($account, $text, $parseMode, $keyboardMarkup, $disableWebPreview, $disableNotifications);
74
    }
75
76
    public function sendMessage(
77
        Account $account,
78
        string $text,
79
        string $parseMode = self::PARSE_PLAIN,
80
        KeyboardMethods $keyboardMarkup = null,
81
        bool $disableWebPreview = false,
82
        bool $disableNotifications = false
83
    ): bool
84
    {
85
        return $this->sendMessageToChat($account->getChatId(), $text, $parseMode, $keyboardMarkup, $disableWebPreview, $disableNotifications);
86
    }
87
88
    public function sendMessageToChat(
89
        int $chatId,
90
        string $text,
91
        string $parseMode = self::PARSE_PLAIN,
92
        KeyboardMethods $keyboardMarkup = null,
93
        bool $disableWebPreview = false,
94
        bool $disableNotifications = false
95
    ): bool
96
    {
97
        $sendMessage = new SendMessage();
98
        $sendMessage->chat_id = (string)$chatId;
99
        $sendMessage->text = $text;
100
        $sendMessage->parse_mode = $parseMode;
101
        $sendMessage->disable_web_page_preview = $disableWebPreview;
102
        $sendMessage->disable_notification = $disableNotifications;
103
        $sendMessage->reply_markup = $keyboardMarkup;
104
105
        try {
106
            $this->client->performApiRequest($sendMessage);
107
108
            return true;
109
        } catch (ClientException $e) {
110
            return false;
111
        }
112
    }
113
}