Passed
Push — master ( a84c2d...e6338b )
by Alexey
04:17
created

MessageSender   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 53
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A sendMessageToUser() 0 11 1
A sendMessageToChat() 0 21 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\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
        $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,
0 ignored issues
show
Unused Code introduced by
The parameter $parseMode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
        ReplyKeyboardMarkup $keyboardMarkup = null,
0 ignored issues
show
Unused Code introduced by
The parameter $keyboardMarkup is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
        bool $disableWebPreview = false,
0 ignored issues
show
Unused Code introduced by
The parameter $disableWebPreview is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
        $disableNotifications = false
0 ignored issues
show
Unused Code introduced by
The parameter $disableNotifications is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    ): bool
53
    {
54
        $sendMessage = new SendMessage();
55
        $sendMessage->chat_id = (string) $chatId;
56
        $sendMessage->text = $text;
57
58
        try {
59
            $this->client->performApiRequest($sendMessage);
60
61
            return true;
62
        } catch (ClientException $e) {
63
            return false;
64
        }
65
    }
66
}