Completed
Push — master ( c391fd...ad5ea5 )
by Kirill
02:54
created

GameService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 58
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findGame() 0 17 3
A getCurrentUser() 0 17 2
A getActiveGames() 0 5 1
A getInactiveGames() 0 5 1
1
<?php
2
3
namespace Chrl\AppBundle\Service;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Chrl\AppBundle\Entity\Game;
7
use Chrl\AppBundle\Entity\User;
8
use Chrl\AppBundle\BuktopuhaBotApi;
9
10
/**
11
 * Game service
12
 *
13
 * @category Symfony
14
 * @package  Chrl_Buktopuha
15
 * @author   Kirill Kholodilin <[email protected]>
16
 * @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
17
 * @link     https://take2.ru/
18
 */
19
20
class GameService
21
{
22
    public $em;
23
24
    public function __construct(BuktopuhaBotApi $telegramBotApi, $config, EntityManagerInterface $entityManager)
0 ignored issues
show
Unused Code introduced by
The parameter $telegramBotApi 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...
Unused Code introduced by
The parameter $config 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...
25
    {
26
        $this->em = $entityManager;
27
    }
28
29
30
    public function findGame(array $message)
31
    {
32
        $game = $this->em->getRepository('AppBundle:Game')->findOneBy(['chatId'=>$message['chat']['id']]);
33
        if (!$game) {
34
            // create game
35
            $game = new Game();
36
            $game->status = 0;
37
            $game->chatId = $message['chat']['id'];
38
            $game->title = isset($message['chat']['title'])
39
                ? $message['chat']['title']
40
                : 'No title';
41
42
            $this->em->persist($game);
43
            $this->em->flush();
44
        }
45
        return $game;
46
    }
47
48
49
    public function getCurrentUser(array $message)
50
    {
51
        $user = $this->em->getRepository('AppBundle:User')->findOneBy(['tgId'=>$message['from']['id']]);
52
        if (!$user) {
53
            $user = new User();
54
            $user->setName($message['from']['first_name'].' '.$message['from']['last_name']);
55
            $user->setAlias($message['from']['username']);
56
            $user->setTgId($message['from']['id']);
57
            $user->setGame($this->findGame($message));
58
            $user->setChatId($message['chat']['id']);
59
            $user->setPoints(0);
60
61
            $this->em->persist($user);
62
            $this->em->flush();
63
        }
64
        return $user;
65
    }
66
67
    public function getActiveGames()
68
    {
69
        $games = $this->em->getRepository('AppBundle:Game')->findBy(['status'=>1]);
70
        return $games;
71
    }
72
    public function getInactiveGames()
73
    {
74
        $games = $this->em->getRepository('AppBundle:Game')->findBy(['status'=>0]);
75
        return $games;
76
    }
77
}
78