Completed
Pull Request — master (#17)
by Rafal
03:41
created

UserPastGames::getGamePastBetting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
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
     * @return array
52
     * @throws \Exception
53
     */
54
    public function getActiveGames(UserInterface $user): array
55
    {
56
        /** @var Game[] $pastGames */
57
        $activeGamesGames = $this->entityManager
58
            ->getRepository(Game::class)
59
            ->findActiveGames();
60
61
        return $this->getGamePastBetting($user, $activeGamesGames);
62
    }
63
64
    /**
65
     * @param UserInterface $user
66
     * @param Game[] $games
67
     * @return array
68
     */
69
    private function getGamePastBetting(UserInterface $user, array $games): array
70
    {
71
        $pastGamesForm = [];
72
        $gameId2UserBets = $this->getGameIdForUserBets($user, $games);
73
74
        foreach ($games as $pastGame) {
75
76
            $pastGamesForm[$pastGame->getId()] = $this->getGamePastBettingInfo(
77
                $gameId2UserBets,
78
                $pastGame
79
            );
80
        }
81
82
        return $pastGamesForm;
83
    }
84
85
    /**
86
     * @param array $gameId2UserBets
87
     * @param Game $pastGame
88
     * @return GamePastBetting
89
     */
90
    private function getGamePastBettingInfo(array $gameId2UserBets, Game $pastGame): GamePastBetting
91
    {
92
        $firstTeamUserResult = null;
93
        $secondTeamUserResult = null;
94
        if (isset($gameId2UserBets[$pastGame->getId()])) {
95
            $firstTeamUserResult = $gameId2UserBets[$pastGame->getId()]->getFirstTeamResult();
96
            $secondTeamUserResult = $gameId2UserBets[$pastGame->getId()]->getSecondTeamResult();
97
        }
98
99
        $gameResult = new Result(
100
            (int)$pastGame->getFirstTeamResult(),
101
            (int)$pastGame->getSecondTeamResult(),
102
            $firstTeamUserResult,
103
            $secondTeamUserResult
104
        );
105
106
        return new GamePastBetting(
107
            $pastGame->getTeamFirst()->getName(),
108
            $pastGame->getTeamSecond()->getName(),
109
            $pastGame->getDate(),
110
            $gameResult->getFirstTeamResult(),
111
            $gameResult->getSecondTeamResult(),
112
            $gameResult->getFirstTeamUserResult(),
113
            $gameResult->getSecondTeamUserResult(),
114
            $this->points->get($gameResult)
115
        );
116
    }
117
118
    /**
119
     * @param UserInterface $user
120
     * @param Game[] $pastGames
121
     * @return UserBettingEntity[]
122
     */
123
    private function getGameIdForUserBets(UserInterface $user, array $pastGames): array
124
    {
125
        if (empty($pastGames)) {
126
            return [];
127
        }
128
129
        /** @var UserBettingEntity[] $userBets */
130
        $userBets = $this->entityManager
131
            ->getRepository(UserBettingEntity::class)
132
            ->findUserBettingByUserId($user, $pastGames);
133
134
        /** @var UserBettingEntity[] $gameId2UserBets */
135
        $gameId2UserBets = [];
136
        foreach ($userBets as $userBet) {
137
            $gameId2UserBets[$userBet->getGame()->getId()] = $userBet;
138
        }
139
        return $gameId2UserBets;
140
    }
141
}