InlineQueryProcessor   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A process() 0 29 3
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
}