Passed
Push — master ( 1145e4...3a5c01 )
by Alexey
03:43
created

InlineQueryProcessor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 56
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B process() 0 29 3
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Service\Telegram;
4
5
6
use Doctrine\ORM\EntityManagerInterface;
7
use Skobkin\Bundle\PointToolsBundle\Repository\UserRepository;
8
use unreal4u\TelegramAPI\Telegram\Methods\AnswerInlineQuery;
9
use unreal4u\TelegramAPI\Telegram\Types\Inline\Query;
10
use unreal4u\TelegramAPI\Telegram\Types\InputMessageContent\Text;
11
use unreal4u\TelegramAPI\TgLog;
12
13
class InlineQueryProcessor
14
{
15
    /**
16
     * @var EntityManagerInterface
17
     */
18
    private $em;
19
20
    /**
21
     * @var UserRepository
22
     */
23
    private $userRepo;
24
25
    /**
26
     * @var TgLog
27
     */
28
    private $client;
29
30
31
    public function __construct(EntityManagerInterface $em, TgLog $client)
32
    {
33
        $this->em = $em;
34
        $this->client = $client;
35
36
        $this->userRepo = $em->getRepository('SkobkinPointToolsBundle:User');
37
    }
38
39
    public function process(Query $inlineQuery)
40
    {
41
        if (mb_strlen($inlineQuery->query) < 2) {
42
            return;
43
        }
44
45
        $answerInlineQuery = new AnswerInlineQuery();
46
        $answerInlineQuery->inline_query_id = $inlineQuery->id;
47
48
        foreach ($this->em->getRepository('SkobkinPointToolsBundle:User')->findUsersLikeLogin($inlineQuery->query) as $user) {
49
            $article = new Query\Result\Article();
50
            $article->title = $user->getLogin();
51
52
            $contentText = new Text();
53
            $contentText->message_text = sprintf(
54
                "@%s:\nName: %s\nSubscribers: %d",
55
                $user->getLogin(),
56
                $user->getName(),
57
                $user->getSubscribers()->count()
58
            );
59
60
            $article->input_message_content = $contentText;
61
            $article->id = md5($user->getId());
62
63
            $answerInlineQuery->addResult($article);
64
        }
65
66
        $this->client->performApiRequest($answerInlineQuery);
67
    }
68
}