Completed
Pull Request — master (#17)
by Rafal
03:41
created

UserBetting::allUpcommingGames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
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_reverse(\array_slice($pastGamesForm, -6))
54
            ]
55
        );
56
    }
57
58
    /**
59
     * @Route("/active-games/", name="active-games")
60
     * @return \Symfony\Component\HttpFoundation\Response
61
     */
62
    public function getActiveGames()
63
    {
64
        $activeGames = $this->userPastGames->getActiveGames($this->getUser());
65
66
        return $this->render(
67
            'gamebetting/betting/active_games.html.twig',
68
            [
69
                'activeGames' => $activeGames
70
            ]
71
        );
72
    }
73
74
    /**
75
     * @Route("/all-upcomming-games", name="all_upcomming_games")
76
     * @return \Symfony\Component\HttpFoundation\Response
77
     */
78
    public function allUpcommingGames()
79
    {
80
        $futureGamesFormBuilder = $this->getFutureGamesFormBuilder();
81
82
        return $this->render(
83
            'gamebetting/betting/all_games.html.twig',
84
            [
85
                'futureGamesForm' => $futureGamesFormBuilder
86
            ]
87
        );
88
    }
89
90
91
    /**
92
     * @Route("/all-past-games", name="all_past_games")
93
     * @return \Symfony\Component\HttpFoundation\Response
94
     */
95
    public function allPastGames()
96
    {
97
        $pastGamesForm = $this->userPastGames->get($this->getUser());
98
99
        return $this->render(
100
            'gamebetting/betting/past_games.html.twig',
101
            [
102
                'pastGamesForm' => $pastGamesForm,
103
                'username' => false
104
            ]
105
        );
106
    }
107
108
    /**
109
     * @Route("/all-past-games-by-user/{userId}", name="game_past_games_by_users")
110
     */
111
    public function allPastGameByUserId(int $userId)
112
    {
113
        $user = $this->getDoctrine()->getRepository(User::class)
114
            ->findOneBy(['id' => $userId]);
115
        $pastGamesForm = [];
116
        $username = '';
117
        if ($user instanceof User) {
118
            $pastGamesForm = $this->userPastGames->get($user);
119
            $username = $user->getUsername();
120
        }
121
122
        return $this->render(
123
            'gamebetting/betting/past_games.html.twig',
124
            [
125
                'pastGamesForm' => $pastGamesForm,
126
                'username' => $username
127
            ]
128
        );
129
130
    }
131
132
    public function getNextGames(int $numberOfGames)
133
    {
134
        $gameBetResult = \array_slice($this->userFutureGames->get($this->getUser()), 0, $numberOfGames);
135
136
        return $this->render(
137
            'dashboard/next_games.html.twig',
138
            [
139
                'gameBetResult' => $gameBetResult
140
            ]
141
        );
142
    }
143
144
    /**
145
     * @Route("/savebet", name="save_bet")
146
     * @Method({"POST"})
147
     */
148
    public function saveBet(Request $request)
149
    {
150
        $params = $request->get('user_betting');
151
        $userBetting = new UserBettingEntity();
152
        $form = $this->createForm(UserBettingType::class, $userBetting);
153
        $form->handleRequest($request);
154
        if (!$form->isSubmitted() && !$form->isValid()) {
155
            return $this->json(['status' => false]);
156
        }
157
        if (!isset($params['firstTeamResult']) || $params['firstTeamResult'] < 0) {
158
            return $this->json(['status' => false]);
159
        }
160
        if (!isset($params['secondTeamResult']) || $params['secondTeamResult'] < 0) {
161
            return $this->json(['status' => false]);
162
        }
163
164
        $entityManager = $this->getDoctrine()->getManager();
165
166
        $userBetting = $entityManager->getRepository(UserBettingEntity::class)
167
            ->findByGameIdAndUserId($params['gameId'], $this->getUser()->getId());
168
        if (!$userBetting instanceof UserBettingEntity) {
169
            $userBetting = new UserBettingEntity();
170
            $game = $entityManager->getRepository(Game::class)
171
                ->find($params['gameId']);
172
            $userBetting->setGame($game);
173
            $userBetting->setUser($this->getUser());
174
        }
175
176
177
        $userBetting->setFirstTeamResult((int)$params['firstTeamResult']);
178
        $userBetting->setSecondTeamResult((int)$params['secondTeamResult']);
179
180
        if ($userBetting->getGame()->getDate()->getTimestamp() < time()) {
181
            return $this->json(['status' => false]);
182
        }
183
184
        $entityManager->persist($userBetting);
185
        $entityManager->flush();
186
187
        return $this->json(['status' => true]);
188
    }
189
190
    /**
191
     * @return array
192
     */
193
    private function getFutureGamesFormBuilder(): array
194
    {
195
        $userFurureGames = $this->userFutureGames->get(
196
            $this->getUser()
197
        );
198
        $gamesFormBuilder = [];
199
        foreach ($userFurureGames as $gameId => $futureGameInfo) {
200
            $gamesFormBuilder[$gameId] = $this->createForm(
201
                UserBettingType::class, null, $futureGameInfo
202
            )->createView();
203
        }
204
        return $gamesFormBuilder;
205
    }
206
}