|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\GameBetting\Business\Games; |
|
4
|
|
|
|
|
5
|
|
|
use App\GameBetting\Business\GamePoints\PointsInterface; |
|
6
|
|
|
use App\GameBetting\Persistence\DataProvider\Result; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
9
|
|
|
use App\GameBetting\Persistence\Entity\UserBetting as UserBettingEntity; |
|
10
|
|
|
use App\GameCore\Persistence\Entity\Game; |
|
11
|
|
|
use App\GameBetting\Persistence\DataProvider\GamePastBetting; |
|
12
|
|
|
|
|
13
|
|
|
class UserPastGames implements UserPastGamesInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var EntityManagerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $entityManager; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var PointsInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $points; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param EntityManagerInterface $entityManager |
|
27
|
|
|
* @param PointsInterface $points |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct(EntityManagerInterface $entityManager, PointsInterface $points) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->entityManager = $entityManager; |
|
32
|
|
|
$this->points = $points; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param UserInterface $user |
|
37
|
|
|
* @return GamePastBetting[] |
|
38
|
|
|
*/ |
|
39
|
|
|
public function get(UserInterface $user): array |
|
40
|
|
|
{ |
|
41
|
|
|
/** @var Game[] $pastGames */ |
|
42
|
|
|
$pastGames = $this->entityManager |
|
43
|
|
|
->getRepository(Game::class) |
|
44
|
|
|
->findPastGames(); |
|
45
|
|
|
|
|
46
|
|
|
return $this->getGamePastBetting($user, $pastGames); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param UserInterface $user |
|
51
|
|
|
* @param int $id |
|
52
|
|
|
* @return GamePastBetting[] |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getOnePastGame(UserInterface $user, int $id): array |
|
55
|
|
|
{ |
|
56
|
|
|
/** @var Game[] $pastGames */ |
|
57
|
|
|
$pastGames = $this->entityManager |
|
58
|
|
|
->getRepository(Game::class) |
|
59
|
|
|
->findPastGamesById($id); |
|
60
|
|
|
|
|
61
|
|
|
return $this->getGamePastBetting($user, $pastGames); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param UserInterface $user |
|
67
|
|
|
* @return array |
|
68
|
|
|
* @throws \Exception |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getActiveGames(UserInterface $user): array |
|
71
|
|
|
{ |
|
72
|
|
|
/** @var Game[] $pastGames */ |
|
73
|
|
|
$activeGamesGames = $this->entityManager |
|
74
|
|
|
->getRepository(Game::class) |
|
75
|
|
|
->findActiveGames(); |
|
76
|
|
|
|
|
77
|
|
|
return $this->getGamePastBetting($user, $activeGamesGames); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param UserInterface $user |
|
82
|
|
|
* @param Game[] $games |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
private function getGamePastBetting(UserInterface $user, array $games): array |
|
86
|
|
|
{ |
|
87
|
|
|
$pastGamesForm = []; |
|
88
|
|
|
$gameId2UserBets = $this->getGameIdForUserBets($user, $games); |
|
89
|
|
|
|
|
90
|
|
|
foreach ($games as $pastGame) { |
|
91
|
|
|
|
|
92
|
|
|
$pastGamesForm[$pastGame->getId()] = $this->getGamePastBettingInfo( |
|
93
|
|
|
$gameId2UserBets, |
|
94
|
|
|
$pastGame |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $pastGamesForm; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param array $gameId2UserBets |
|
103
|
|
|
* @param Game $pastGame |
|
104
|
|
|
* @return GamePastBetting |
|
105
|
|
|
*/ |
|
106
|
|
|
private function getGamePastBettingInfo(array $gameId2UserBets, Game $pastGame): GamePastBetting |
|
107
|
|
|
{ |
|
108
|
|
|
$firstTeamUserResult = null; |
|
109
|
|
|
$secondTeamUserResult = null; |
|
110
|
|
|
if (isset($gameId2UserBets[$pastGame->getId()])) { |
|
111
|
|
|
$firstTeamUserResult = $gameId2UserBets[$pastGame->getId()]->getFirstTeamResult(); |
|
112
|
|
|
$secondTeamUserResult = $gameId2UserBets[$pastGame->getId()]->getSecondTeamResult(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$gameResult = new Result( |
|
116
|
|
|
(int)$pastGame->getFirstTeamResult(), |
|
117
|
|
|
(int)$pastGame->getSecondTeamResult(), |
|
118
|
|
|
$firstTeamUserResult, |
|
119
|
|
|
$secondTeamUserResult |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
return new GamePastBetting( |
|
123
|
|
|
$pastGame->getId(), |
|
124
|
|
|
$pastGame->getTeamFirst()->getName(), |
|
125
|
|
|
$pastGame->getTeamSecond()->getName(), |
|
126
|
|
|
$pastGame->getDate(), |
|
127
|
|
|
$gameResult->getFirstTeamResult(), |
|
128
|
|
|
$gameResult->getSecondTeamResult(), |
|
129
|
|
|
$gameResult->getFirstTeamUserResult(), |
|
130
|
|
|
$gameResult->getSecondTeamUserResult(), |
|
131
|
|
|
$this->points->get($gameResult) |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param UserInterface $user |
|
137
|
|
|
* @param Game[] $pastGames |
|
138
|
|
|
* @return UserBettingEntity[] |
|
139
|
|
|
*/ |
|
140
|
|
|
private function getGameIdForUserBets(UserInterface $user, array $pastGames): array |
|
141
|
|
|
{ |
|
142
|
|
|
if (empty($pastGames)) { |
|
143
|
|
|
return []; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** @var UserBettingEntity[] $userBets */ |
|
147
|
|
|
$userBets = $this->entityManager |
|
148
|
|
|
->getRepository(UserBettingEntity::class) |
|
149
|
|
|
->findUserBettingByUserId($user, $pastGames); |
|
150
|
|
|
|
|
151
|
|
|
/** @var UserBettingEntity[] $gameId2UserBets */ |
|
152
|
|
|
$gameId2UserBets = []; |
|
153
|
|
|
foreach ($userBets as $userBet) { |
|
154
|
|
|
$gameId2UserBets[$userBet->getGame()->getId()] = $userBet; |
|
155
|
|
|
} |
|
156
|
|
|
return $gameId2UserBets; |
|
157
|
|
|
} |
|
158
|
|
|
} |