Completed
Pull Request — master (#6)
by Rafal
04:47
created

UserBetting::list()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
class UserBetting extends Controller
17
{
18
    /**
19
     * @var UserFutureGamesInterface
20
     */
21
    private $userFutureGames;
22
23
    /**
24
     * @var UserPastGamesInterface
25
     */
26
    private $userPastGames;
27
28
    /**
29
     * @param UserFutureGamesInterface $userFutureGames
30
     * @param UserPastGamesInterface $userPastGames
31
     */
32
    public function __construct(UserFutureGamesInterface $userFutureGames, UserPastGamesInterface $userPastGames)
33
    {
34
        $this->userFutureGames = $userFutureGames;
35
        $this->userPastGames = $userPastGames;
36
    }
37
38
    /**
39
     * @Route("/gamebet/", name="game_bet_list")
40
     */
41
    public function list()
42
    {
43
        $futureGamesFormBuilder = $this->getFutureGamesFormBuilder();
44
        $pastGamesForm = $this->userPastGames->get($this->getUser());
45
46
        return $this->render(
47
            'gamebetting/betting/betting.html.twig',
48
            [
49
                'futureGamesForm' => $futureGamesFormBuilder,
50
                'pastGamesForm'   => $pastGamesForm
51
            ]
52
        );
53
    }
54
55
56
    /**
57
     * @Route("/gamebet/{gameId}", name="game_bet")
58
     */
59
    public function index($gameId)
60
    {
61
        $game = $this->getDoctrine()
62
                     ->getRepository(Game::class)
63
                     ->find($gameId)
64
        ;
65
66
        if (!$game) {
67
            return $this->redirectToRoute('replace_with_some_route');
68
        }
69
70
        $form = $this->createForm(UserBettingType::class, null, ['game' => $game]);
71
72
        return $this->render(
73
            'gamebetting/betting/betting.html.twig',
74
            array('form' => $form->createView())
75
        );
76
    }
77
78
    /**
79
     * @Route("/savebet", name="save_bet")
80
     * @Method({"POST"})
81
     */
82
    public function saveBet(Request $request)
83
    {
84
85
        $params = $request->get('user_betting');
86
        $userBetting = new UserBettingEntity();
87
        $form = $this->createForm(UserBettingType::class, $userBetting);
88
        $form->handleRequest($request);
89
        if (!$form->isSubmitted() && !$form->isValid()) {
90
            return $this->json(['status' => false]);
91
        }
92
        if(!isset($params['firstTeamResult'])  || $params['firstTeamResult'] < 0) {
93
            return $this->json(['status' => false]);
94
        }
95
        if(!isset($params['secondTeamResult'])  || $params['secondTeamResult'] < 0) {
96
            return $this->json(['status' => false]);
97
        }
98
99
100
        $entityManager = $this->getDoctrine()->getManager();
101
102
        $userBetting = $entityManager->getRepository(UserBettingEntity::class)
103
                                     ->findByGameIdAndUserId($params['gameId'], $this->getUser()->getId())
104
        ;
105
        if (!$userBetting instanceof UserBettingEntity) {
106
            $userBetting = new UserBettingEntity();
107
            $game = $entityManager->getRepository(Game::class)
108
                                  ->find($params['gameId']);
109
            $userBetting->setGame($game);
110
            $userBetting->setUser($this->getUser());
111
        }
112
113
114
        $userBetting->setFirstTeamResult($params['firstTeamResult']);
115
        $userBetting->setSecondTeamResult($params['secondTeamResult']);
116
117
        if($userBetting->getGame()->getDate()->getTimestamp() < time() ) {
118
            return $this->json(['status' => false]);
119
        }
120
121
        $entityManager->persist($userBetting);
122
        $entityManager->flush();
123
124
        return $this->json(['status' => true]);
125
    }
126
127
    /**
128
     * @return array
129
     */
130
    private function getFutureGamesFormBuilder(): array
131
    {
132
        $userFurureGames = $this->userFutureGames->get(
133
            $this->getUser()
134
        );
135
        $gamesFormBuilder = [];
136
        foreach ($userFurureGames as $gameId => $futureGameInfo) {
137
            $gamesFormBuilder[$gameId] = $this->createForm(
138
                UserBettingType::class, null, $futureGameInfo
139
            )->createView();
140
        }
141
        return $gamesFormBuilder;
142
    }
143
}