InlineQueryProcessor::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 16
cp 0
rs 9.456
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram;
4
5
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
6
use unreal4u\TelegramAPI\Telegram\Methods\AnswerInlineQuery;
7
use unreal4u\TelegramAPI\Telegram\Types\Inline\Query;
8
use unreal4u\TelegramAPI\Telegram\Types\InputMessageContent\Text;
9
use unreal4u\TelegramAPI\TgLog;
10
11
class InlineQueryProcessor
12
{
13
    /** @var UserRepository */
14
    private $userRepo;
15
16
    /** @var TgLog */
17
    private $client;
18
19
20
    public function __construct(UserRepository $userRepository, TgLog $client)
21
    {
22
        $this->userRepo = $userRepository;
23
        $this->client = $client;
24
    }
25
26
    public function process(Query $inlineQuery): void
27
    {
28
        if (mb_strlen($inlineQuery->query) < 2) {
29
            return;
30
        }
31
32
        $answerInlineQuery = new AnswerInlineQuery();
33
        $answerInlineQuery->inline_query_id = $inlineQuery->id;
34
35
        foreach ($this->userRepo->findUsersLikeLogin($inlineQuery->query) as $user) {
36
            $article = new Query\Result\Article();
37
            $article->title = $user->getLogin();
38
39
            $contentText = new Text();
40
            $contentText->message_text = sprintf(
41
                "@%s:\nName: %s\nSubscribers: %d",
42
                $user->getLogin(),
43
                $user->getName(),
44
                $user->getSubscribers()->count()
45
            );
46
47
            $article->input_message_content = $contentText;
48
            $article->id = md5($user->getId());
49
50
            $answerInlineQuery->addResult($article);
51
        }
52
53
        $this->client->performApiRequest($answerInlineQuery);
54
    }
55
}