Passed
Push — master ( 57050c...eb8a02 )
by
unknown
03:36
created

UserBetting::getNextGames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 8
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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
13
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
class UserBetting extends Controller
18
{
19
    /**
20
     * @var UserFutureGamesInterface
21
     */
22
    private $userFutureGames;
23
24
    /**
25
     * @var UserPastGamesInterface
26
     */
27
    private $userPastGames;
28
29
    /**
30
     * @param UserFutureGamesInterface $userFutureGames
31
     * @param UserPastGamesInterface $userPastGames
32
     */
33
    public function __construct(UserFutureGamesInterface $userFutureGames, UserPastGamesInterface $userPastGames)
34
    {
35
        $this->userFutureGames = $userFutureGames;
36
        $this->userPastGames = $userPastGames;
37
    }
38
39
    /**
40
     * @TODO template struktur überarbeiten und eigene actions machen
41
     * @Route("/gamebet/", name="game_bet_list")
42
     */
43
    public function list()
44
    {
45
        $futureGamesFormBuilder = $this->getFutureGamesFormBuilder();
46
        $pastGamesForm = $this->userPastGames->get($this->getUser());
47
48
        return $this->render(
49
            'gamebetting/betting/betting.html.twig',
50
            [
51
                'futureGamesForm' => $futureGamesFormBuilder,
52
                'pastGamesForm'   => $pastGamesForm
53
            ]
54
        );
55
    }
56
57
    /**
58
     * @Route("/all-upcomming-games", name="all_upcomming_games")
59
     * @return \Symfony\Component\HttpFoundation\Response
60
     */
61
    public function allUpcommingGames()
62
    {
63
        $futureGamesFormBuilder = $this->getFutureGamesFormBuilder();
64
65
        return $this->render(
66
            'gamebetting/betting/all_games.html.twig',
67
            [
68
                'futureGamesForm' => $futureGamesFormBuilder
69
            ]
70
        );
71
    }
72
73
74
    public function getNextGames(int $numberOfGames, GameRepository $gameRepository)
0 ignored issues
show
Unused Code introduced by
The parameter $gameRepository is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

74
    public function getNextGames(int $numberOfGames, /** @scrutinizer ignore-unused */ GameRepository $gameRepository)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        $gameBetResult = \array_slice($this->userFutureGames->get($this->getUser()),0,$numberOfGames);
77
78
        return $this->render(
79
            'dashboard/next_games.html.twig',
80
            [
81
                'gameBetResult' => $gameBetResult
82
            ]
83
        );
84
    }
85
86
    /**
87
     * @Route("/savebet", name="save_bet")
88
     * @Method({"POST"})
89
     */
90
    public function saveBet(Request $request)
91
    {
92
        $params = $request->get('user_betting');
93
        $userBetting = new UserBettingEntity();
94
        $form = $this->createForm(UserBettingType::class, $userBetting);
95
        $form->handleRequest($request);
96
        if (!$form->isSubmitted() && !$form->isValid()) {
97
            return $this->json(['status' => false]);
98
        }
99
        if(!isset($params['firstTeamResult'])  || $params['firstTeamResult'] < 0) {
100
            return $this->json(['status' => $params]);
101
        }
102
        if(!isset($params['secondTeamResult'])  || $params['secondTeamResult'] < 0) {
103
            return $this->json(['status' => false]);
104
        }
105
        
106
        $entityManager = $this->getDoctrine()->getManager();
107
108
        $userBetting = $entityManager->getRepository(UserBettingEntity::class)
109
                                     ->findByGameIdAndUserId($params['gameId'], $this->getUser()->getId())
110
        ;
111
        if (!$userBetting instanceof UserBettingEntity) {
112
            $userBetting = new UserBettingEntity();
113
            $game = $entityManager->getRepository(Game::class)
114
                                  ->find($params['gameId']);
115
            $userBetting->setGame($game);
116
            $userBetting->setUser($this->getUser());
117
        }
118
119
120
        $userBetting->setFirstTeamResult((int)$params['firstTeamResult']);
121
        $userBetting->setSecondTeamResult((int)$params['secondTeamResult']);
122
123
        if($userBetting->getGame()->getDate()->getTimestamp() < time() ) {
124
            return $this->json(['status' => false]);
125
        }
126
127
        $entityManager->persist($userBetting);
128
        $entityManager->flush();
129
130
        return $this->json(['status' => true]);
131
    }
132
133
    /**
134
     * @return array
135
     */
136
    private function getFutureGamesFormBuilder(): array
137
    {
138
        $userFurureGames = $this->userFutureGames->get(
139
            $this->getUser()
140
        );
141
        $gamesFormBuilder = [];
142
        foreach ($userFurureGames as $gameId => $futureGameInfo) {
143
            $gamesFormBuilder[$gameId] = $this->createForm(
144
                UserBettingType::class, null, $futureGameInfo
145
            )->createView();
146
        }
147
        return $gamesFormBuilder;
148
    }
149
}