Completed
Push — master ( e7a6c5...67cf24 )
by Rafal
12s queued 10s
created

UserBetting::list()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 8.8981
c 0
b 0
f 0
cc 4
eloc 31
nc 6
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Persistence\DataProvider\GamePastBetting;
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
     * @param UserFutureGamesInterface $userFutureGames
25
     */
26
    public function __construct(UserFutureGamesInterface $userFutureGames)
27
    {
28
        $this->userFutureGames = $userFutureGames;
29
    }
30
31
32
    /**
33
     * @Route("/gamebet/", name="game_bet_list")
34
     */
35
    public function list()
36
    {
37
        $futureGamesFormBuilder = $this->getFutureGamesFormBuilder();
38
39
40
        $pastGames = $this->getDoctrine()
41
                          ->getRepository(Game::class)
42
                          ->findPastGames()
43
        ;
44
45
        $user = $this->getUser();
46
47
        $userBets = $this->getDoctrine()
48
                         ->getRepository(UserBettingEntity::class)
49
                         ->findUserBettingByUserId($user, $pastGames)
50
        ;
51
52
        $pastGamesForm = [];
53
        /** @var UserBettingEntity[] $gameId2UserBets */
54
        $gameId2UserBets = [];
55
        /** @var UserBettingEntity $userBet */
56
        foreach ($userBets as $userBet) {
57
            $gameId2UserBets[$userBet->getGame()->getId()] = $userBet;
58
        }
59
        /** @var Game[] $pastGames */
60
61
        foreach ($pastGames as $pastGame) {
62
63
            $firstTeamUserResult = null;
64
            $secondTeamUserResult = null;
65
            if (isset($gameId2UserBets[$pastGame->getId()])) {
66
                $firstTeamUserResult = $gameId2UserBets[$pastGame->getId()]->getFirstTeamResult();
67
                $secondTeamUserResult = $gameId2UserBets[$pastGame->getId()]->getSecondTeamResult();
68
            }
69
70
            $pastGamesForm[] = new GamePastBetting(
71
                $pastGame->getTeamFirst()->getName(),
72
                $pastGame->getTeamSecond()->getName(),
73
                $pastGame->getDate(),
74
                (int)$pastGame->getFirstTeamResult(),
75
                (int)$pastGame->getSecondTeamResult(),
76
                $firstTeamUserResult,
77
                $secondTeamUserResult
78
            );
79
        }
80
81
        return $this->render(
82
            'gamebetting/betting/betting.html.twig',
83
            [
84
                'futureGamesForm' => $futureGamesFormBuilder,
85
                'pastGamesForm'   => $pastGamesForm
86
            ]
87
        );
88
    }
89
90
91
    /**
92
     * @Route("/gamebet/{gameId}", name="game_bet")
93
     */
94
    public function index($gameId)
95
    {
96
        $game = $this->getDoctrine()
97
                     ->getRepository(Game::class)
98
                     ->find($gameId)
99
        ;
100
101
        if (!$game) {
102
            return $this->redirectToRoute('replace_with_some_route');
103
        }
104
105
        $form = $this->createForm(UserBettingType::class, null, ['game' => $game]);
106
107
        return $this->render(
108
            'gamebetting/betting/betting.html.twig',
109
            array('form' => $form->createView())
110
        );
111
    }
112
113
    /**
114
     * @Route("/savebet", name="save_bet")
115
     * @Method({"POST"})
116
     */
117
    public function saveBet(Request $request)
118
    {
119
120
        $params = $request->get('user_betting');
121
        $userBetting = new UserBettingEntity();
122
        $form = $this->createForm(UserBettingType::class, $userBetting);
123
        $form->handleRequest($request);
124
        if (!$form->isSubmitted() && !$form->isValid()) {
125
            return $this->json(['status' => false]);
126
        }
127
128
        $entityManager = $this->getDoctrine()->getManager();
129
130
        $userBetting = $entityManager->getRepository(UserBettingEntity::class)
131
                                     ->findByGameIdAndUserId($params['gameId'], $this->getUser()->getId())
132
        ;
133
        if (!$userBetting instanceof UserBettingEntity) {
134
            $userBetting = new UserBettingEntity();
135
            $game = $entityManager->getRepository(Game::class)
136
                                  ->find($params['gameId']);
137
            $userBetting->setGame($game);
138
            $userBetting->setUser($this->getUser());
139
        }
140
141
        $userBetting->setFirstTeamResult($params['firstTeamResult']);
142
        $userBetting->setSecondTeamResult($params['secondTeamResult']);
143
144
        if($userBetting->getGame()->getDate()->getTimestamp() < time() ) {
145
            return $this->json(['status' => false]);
146
        }
147
148
        $entityManager->persist($userBetting);
149
        $entityManager->flush();
150
151
        return $this->json(['status' => true]);
152
    }
153
154
    /**
155
     * @return array
156
     */
157
    private function getFutureGamesFormBuilder(): array
158
    {
159
        $userFurureGames = $this->userFutureGames->get(
160
            $this->getUser()
161
        );
162
        $gamesFormBuilder = [];
163
        foreach ($userFurureGames as $gameId => $futureGameInfo) {
164
            $gamesFormBuilder[$gameId] = $this->createForm(
165
                UserBettingType::class, null, $futureGameInfo
166
            )->createView();
167
        }
168
        return $gamesFormBuilder;
169
    }
170
}