Completed
Push — master ( b91018...14d1d5 )
by Rafal
13s queued 10s
created

UserBetting::allPastGameByUserId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace App\GameBetting\Communication\Controller;
4
5
6
use App\GameBetting\Business\Form\UserBettingType;
7
use App\GameBetting\Business\Games\UserFutureGamesInterface;
8
use App\GameBetting\Business\Games\UserPastGamesInterface;
9
use App\GameBetting\Persistence\Entity\UserBetting as UserBettingEntity;
10
use App\GameCore\Persistence\Entity\Game;
11
use App\GameCore\Persistence\Repository\GameRepository;
12
use App\User\Persistence\Entity\User;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\Routing\Annotation\Route;
17
18
class UserBetting extends Controller
19
{
20
    /**
21
     * @var UserFutureGamesInterface
22
     */
23
    private $userFutureGames;
24
25
    /**
26
     * @var UserPastGamesInterface
27
     */
28
    private $userPastGames;
29
30
    /**
31
     * @param UserFutureGamesInterface $userFutureGames
32
     * @param UserPastGamesInterface $userPastGames
33
     */
34
    public function __construct(UserFutureGamesInterface $userFutureGames, UserPastGamesInterface $userPastGames)
35
    {
36
        $this->userFutureGames = $userFutureGames;
37
        $this->userPastGames = $userPastGames;
38
    }
39
40
    /**
41
     * @TODO template struktur überarbeiten und eigene actions machen
42
     * @Route("/gamebet/", name="game_bet_list")
43
     */
44
    public function list()
45
    {
46
        $futureGamesFormBuilder = $this->getFutureGamesFormBuilder();
47
        $pastGamesForm = $this->userPastGames->get($this->getUser());
48
49
        return $this->render(
50
            'gamebetting/betting/betting.html.twig',
51
            [
52
                'futureGamesForm' => $futureGamesFormBuilder,
53
                'pastGamesForm'   => \array_slice($pastGamesForm, -10)
54
            ]
55
        );
56
    }
57
58
    /**
59
     * @Route("/all-upcomming-games", name="all_upcomming_games")
60
     * @return \Symfony\Component\HttpFoundation\Response
61
     */
62
    public function allUpcommingGames()
63
    {
64
        $futureGamesFormBuilder = $this->getFutureGamesFormBuilder();
65
66
        return $this->render(
67
            'gamebetting/betting/all_games.html.twig',
68
            [
69
                'futureGamesForm' => $futureGamesFormBuilder
70
            ]
71
        );
72
    }
73
74
75
    /**
76
     * @Route("/all-past-games", name="all_past_games")
77
     * @return \Symfony\Component\HttpFoundation\Response
78
     */
79
    public function allPastGames()
80
    {
81
        $pastGamesForm = $this->userPastGames->get($this->getUser());
82
83
        return $this->render(
84
            'gamebetting/betting/past_games.html.twig',
85
            [
86
                'pastGamesForm'   => $pastGamesForm,
87
                'username' => false
88
            ]
89
        );
90
    }
91
92
    /**
93
     * @Route("/all-past-games-by-user/{userId}", name="game_past_games_by_users")
94
     */
95
    public function allPastGameByUserId(int $userId)
96
    {
97
        $user = $this->getDoctrine()->getRepository(User::class)
98
                            ->findOneBy(['id' => $userId]);
99
        $pastGamesForm = [];
100
        $username = '';
101
        if($user instanceof User) {
102
            $pastGamesForm = $this->userPastGames->get($user);
103
            $username = $user->getUsername();
104
        }
105
106
        return $this->render(
107
            'gamebetting/betting/past_games.html.twig',
108
            [
109
                'pastGamesForm'   => $pastGamesForm,
110
                'username'   => $username
111
            ]
112
        );
113
114
    }
115
116
    public function getNextGames(int $numberOfGames)
117
    {
118
        $gameBetResult = \array_slice($this->userFutureGames->get($this->getUser()),0,$numberOfGames);
119
120
        return $this->render(
121
            'dashboard/next_games.html.twig',
122
            [
123
                'gameBetResult' => $gameBetResult
124
            ]
125
        );
126
    }
127
128
    /**
129
     * @Route("/savebet", name="save_bet")
130
     * @Method({"POST"})
131
     */
132
    public function saveBet(Request $request)
133
    {
134
        $params = $request->get('user_betting');
135
        $userBetting = new UserBettingEntity();
136
        $form = $this->createForm(UserBettingType::class, $userBetting);
137
        $form->handleRequest($request);
138
        if (!$form->isSubmitted() && !$form->isValid()) {
139
            return $this->json(['status' => false]);
140
        }
141
        if(!isset($params['firstTeamResult'])  || $params['firstTeamResult'] < 0) {
142
            return $this->json(['status' => $params]);
143
        }
144
        if(!isset($params['secondTeamResult'])  || $params['secondTeamResult'] < 0) {
145
            return $this->json(['status' => false]);
146
        }
147
        
148
        $entityManager = $this->getDoctrine()->getManager();
149
150
        $userBetting = $entityManager->getRepository(UserBettingEntity::class)
151
                                     ->findByGameIdAndUserId($params['gameId'], $this->getUser()->getId())
152
        ;
153
        if (!$userBetting instanceof UserBettingEntity) {
154
            $userBetting = new UserBettingEntity();
155
            $game = $entityManager->getRepository(Game::class)
156
                                  ->find($params['gameId']);
157
            $userBetting->setGame($game);
158
            $userBetting->setUser($this->getUser());
159
        }
160
161
162
        $userBetting->setFirstTeamResult((int)$params['firstTeamResult']);
163
        $userBetting->setSecondTeamResult((int)$params['secondTeamResult']);
164
165
        if($userBetting->getGame()->getDate()->getTimestamp() < time() ) {
166
            return $this->json(['status' => false]);
167
        }
168
169
        $entityManager->persist($userBetting);
170
        $entityManager->flush();
171
172
        return $this->json(['status' => true]);
173
    }
174
175
    /**
176
     * @return array
177
     */
178
    private function getFutureGamesFormBuilder(): array
179
    {
180
        $userFurureGames = $this->userFutureGames->get(
181
            $this->getUser()
182
        );
183
        $gamesFormBuilder = [];
184
        foreach ($userFurureGames as $gameId => $futureGameInfo) {
185
            $gamesFormBuilder[$gameId] = $this->createForm(
186
                UserBettingType::class, null, $futureGameInfo
187
            )->createView();
188
        }
189
        return $gamesFormBuilder;
190
    }
191
}