Completed
Push — master ( 2f35f3...5b14ac )
by Rafal
10:52 queued 06:57
created

UserBetting::extraInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 12
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\DataProvider\ExtraFrontendResult;
10
use App\GameBetting\Persistence\Entity\UserBetting as UserBettingEntity;
11
use App\GameCore\Persistence\Entity\Game;
12
use App\GameCore\Persistence\Entity\Team;
13
use App\GameCore\Persistence\Repository\GameRepository;
14
use App\GameExtraBetting\Persistence\Entity\UserExtraBetting;
15
use App\User\Persistence\Entity\User;
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
17
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Routing\Annotation\Route;
20
21
class UserBetting extends Controller
22
{
23
    /**
24
     * @var UserFutureGamesInterface
25
     */
26
    private $userFutureGames;
27
28
    /**
29
     * @var UserPastGamesInterface
30
     */
31
    private $userPastGames;
32
33
    /**
34
     * @param UserFutureGamesInterface $userFutureGames
35
     * @param UserPastGamesInterface $userPastGames
36
     */
37
    public function __construct(UserFutureGamesInterface $userFutureGames, UserPastGamesInterface $userPastGames)
38
    {
39
        $this->userFutureGames = $userFutureGames;
40
        $this->userPastGames = $userPastGames;
41
    }
42
43
    /**
44
     * @TODO template struktur überarbeiten und eigene actions machen
45
     * @Route("/gamebet/", name="game_bet_list")
46
     */
47
    public function list()
48
    {
49
        $futureGamesFormBuilder = $this->getFutureGamesFormBuilder();
50
        $pastGamesForm = $this->userPastGames->get($this->getUser());
51
52
        return $this->render(
53
            'gamebetting/betting/betting.html.twig',
54
            [
55
                'futureGamesForm' => $futureGamesFormBuilder,
56
                'pastGamesForm' => \array_reverse(\array_slice($pastGamesForm, -6))
57
            ]
58
        );
59
    }
60
61
    /**
62
     * @Route("/active-games/", name="active-games")
63
     * @return \Symfony\Component\HttpFoundation\Response
64
     */
65
    public function getActiveGames()
66
    {
67
        $activeGames = $this->userPastGames->getActiveGames($this->getUser());
68
69
        return $this->render(
70
            'gamebetting/betting/active_games.html.twig',
71
            [
72
                'activeGames' => $activeGames
73
            ]
74
        );
75
    }
76
77
    /**
78
     * @Route("/all-upcomming-games", name="all_upcomming_games")
79
     * @return \Symfony\Component\HttpFoundation\Response
80
     */
81
    public function allUpcommingGames()
82
    {
83
        $futureGamesFormBuilder = $this->getFutureGamesFormBuilder();
84
85
        return $this->render(
86
            'gamebetting/betting/all_games.html.twig',
87
            [
88
                'futureGamesForm' => $futureGamesFormBuilder
89
            ]
90
        );
91
    }
92
93
    /**
94
     * @Route("/all-past-games", name="all_past_games")
95
     * @return \Symfony\Component\HttpFoundation\Response
96
     */
97
    public function allPastGames()
98
    {
99
        $user = $this->getUser();
100
        $pastGamesForm = $this->userPastGames->get($user);
101
102
        return $this->render(
103
            'gamebetting/betting/past_games.html.twig',
104
            [
105
                'pastGamesForm' => $pastGamesForm,
106
                'username' => false,
107
                'extraInfo' => $this->extraInfo($user)
108
            ]
109
        );
110
    }
111
112
    /**
113
     * toDo Refactor this
114
     * @Route("/past-game-detail/{gameId}", name="past_game_detail")
115
     * @return \Symfony\Component\HttpFoundation\Response
116
     */
117
    public function pastGameDetail(int $gameId)
118
    {
119
        $users = $this->getDoctrine()->getRepository(User::class)
120
            ->findBy([], ['username' => 'ASC']);
121
122
        $teamFirstName = '';
123
        $teamSecondName = '';
124
        $gameInfo2users = [];
125
        foreach ($users as $user) {
126
            $pastGamesForm = $this->userPastGames->getOnePastGame($user, $gameId);
127
            if (!empty($pastGamesForm)) {
128
                $teamFirstName = current($pastGamesForm)->getFirstTeamName();
129
                $teamSecondName = current($pastGamesForm)->getSecondTeamName();
130
131
                $gameInfo2users[$user->getUsername()] = current($pastGamesForm);
132
            }
133
134
        }
135
136
        return $this->render(
137
            'gamebetting/betting/past_game_detail.html.twig',
138
            [
139
                'gameInfo2users' => $gameInfo2users,
140
                'teamFirstName' => $teamFirstName,
141
                'teamSecondName' => $teamSecondName,
142
            ]
143
        );
144
    }
145
146
    /**
147
     * @Route("/all-past-games-by-user/{userId}", name="game_past_games_by_users")
148
     */
149
    public function allPastGameByUserId(int $userId)
150
    {
151
        $user = $this->getDoctrine()->getRepository(User::class)
152
            ->findOneBy(['id' => $userId]);
153
        $pastGamesForm = [];
154
        $username = '';
155
        $extraInfo = [];
156
        if ($user instanceof User) {
157
            $pastGamesForm = $this->userPastGames->get($user);
158
            $username = $user->getUsername();
159
            $extraInfo = $this->extraInfo($user);
160
        }
161
162
163
164
        return $this->render(
165
            'gamebetting/betting/past_games.html.twig',
166
            [
167
                'pastGamesForm' => $pastGamesForm,
168
                'username' => $username,
169
                'extraInfo' => $extraInfo,
170
            ]
171
        );
172
173
    }
174
175
    public function getNextGames(int $numberOfGames)
176
    {
177
        $gameBetResult = \array_slice($this->userFutureGames->get($this->getUser()), 0, $numberOfGames);
178
179
        return $this->render(
180
            'dashboard/next_games.html.twig',
181
            [
182
                'gameBetResult' => $gameBetResult
183
            ]
184
        );
185
    }
186
187
    /**
188
     * @Route("/savebet", name="save_bet")
189
     * @Method({"POST"})
190
     */
191
    public function saveBet(Request $request)
192
    {
193
        $params = (array)$request->get('user_betting');
194
        $userBetting = new UserBettingEntity();
195
        $form = $this->createForm(UserBettingType::class, $userBetting);
196
        $form->handleRequest($request);
197
198
        if ($this->isSaveFormInValid($form, $params) === true) {
199
            return $this->json(['status' => false]);
200
        }
201
202
        $entityManager = $this->getDoctrine()->getManager();
203
        $userBetting = $entityManager->getRepository(UserBettingEntity::class)
204
            ->findByGameIdAndUserId($params['gameId'], $this->getUser()->getId());
205
        if (!$userBetting instanceof UserBettingEntity) {
206
            $userBetting = new UserBettingEntity();
207
            $game = $entityManager->getRepository(Game::class)
208
                ->find($params['gameId']);
209
            $userBetting->setGame($game);
210
            $userBetting->setUser($this->getUser());
211
        }
212
213
214
        $userBetting->setFirstTeamResult((int)$params['firstTeamResult']);
215
        $userBetting->setSecondTeamResult((int)$params['secondTeamResult']);
216
217
        if ($userBetting->getGame()->getDate()->getTimestamp() < time()) {
218
            return $this->json(['status' => false]);
219
        }
220
221
        $entityManager->persist($userBetting);
222
        $entityManager->flush();
223
224
        return $this->json(['status' => true]);
225
    }
226
227
    /**
228
     * @param $form
229
     * @param array $params
230
     * @return bool
231
     */
232
    private function isSaveFormInValid($form, array $params): bool
233
    {
234
        $isSymfonyFormInValid = (!$form->isSubmitted() && !$form->isValid());
235
        $isFirstTeamResultNotValid = (!isset($params['firstTeamResult']) || $params['firstTeamResult'] < 0);
236
        $isSecondTeamResultNotValid = (!isset($params['secondTeamResult']) || $params['secondTeamResult'] < 0);
237
238
        return $isSymfonyFormInValid || $isFirstTeamResultNotValid || $isSecondTeamResultNotValid;
239
    }
240
241
    /**
242
     * @return array
243
     */
244
    private function getFutureGamesFormBuilder(): array
245
    {
246
        $userFurureGames = $this->userFutureGames->get(
247
            $this->getUser()
248
        );
249
        $gamesFormBuilder = [];
250
        foreach ($userFurureGames as $gameId => $futureGameInfo) {
251
            $gamesFormBuilder[$gameId] = $this->createForm(
252
                UserBettingType::class, null, $futureGameInfo
253
            )->createView();
254
        }
255
        return $gamesFormBuilder;
256
    }
257
258
    /**
259
     * @param $user
260
     * @return array
261
     */
262
    protected function extraInfo($user): array
263
    {
264
        $userExterBettings = $this->getDoctrine()->getRepository(UserExtraBetting::class)->getByUser($user);
265
        $extraInfo = [];
266
        foreach ($userExterBettings as $userExterBetting) {
267
            $team = $this->getDoctrine()->getRepository(Team::class)->find((int)$userExterBetting->getText());
268
            $extraInfo[] = new ExtraFrontendResult(
269
                $userExterBetting->getType(),
270
                $team->getName()
271
            );
272
        }
273
        return $extraInfo;
274
    }
275
}