Completed
Pull Request — master (#6)
by Rafal
07:50 queued 03:16
created

UserPastGames::get()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 20
nc 3
nop 1
1
<?php
2
3
namespace App\GameBetting\Business\Games;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Symfony\Component\Security\Core\User\UserInterface;
7
use App\GameBetting\Persistence\Entity\UserBetting as UserBettingEntity;
8
use App\GameCore\Persistence\Entity\Game;
9
use App\GameBetting\Persistence\DataProvider\GamePastBetting;
10
11
class UserPastGames implements UserPastGamesInterface
12
{
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    private $entityManager;
17
18
    /**
19
     * @param EntityManagerInterface $entityManager
20
     */
21
    public function __construct(EntityManagerInterface $entityManager)
22
    {
23
        $this->entityManager = $entityManager;
24
    }
25
26
    /**
27
     * @param UserInterface $user
28
     * @return GamePastBetting[]
29
     */
30
    public function get(UserInterface $user): array
31
    {
32
        $pastGamesForm = [];
33
34
        /** @var Game[] $pastGames */
35
        $pastGames = $this->entityManager
36
            ->getRepository(Game::class)
37
            ->findPastGames();
38
39
        $gameId2UserBets = $this->getGameIdForUserBets($user, $pastGames);
40
41
        foreach ($pastGames as $pastGame) {
42
43
            $firstTeamUserResult = null;
44
            $secondTeamUserResult = null;
45
            if (isset($gameId2UserBets[$pastGame->getId()])) {
46
                $firstTeamUserResult = $gameId2UserBets[$pastGame->getId()]->getFirstTeamResult();
47
                $secondTeamUserResult = $gameId2UserBets[$pastGame->getId()]->getSecondTeamResult();
48
            }
49
50
            $pastGamesForm[$pastGame->getId()] = new GamePastBetting(
51
                $pastGame->getTeamFirst()->getName(),
52
                $pastGame->getTeamSecond()->getName(),
53
                $pastGame->getDate(),
54
                (int)$pastGame->getFirstTeamResult(),
55
                (int)$pastGame->getSecondTeamResult(),
56
                $firstTeamUserResult,
57
                $secondTeamUserResult
58
            );
59
        }
60
61
        return $pastGamesForm;
62
    }
63
64
    /**
65
     * @param UserInterface $user
66
     * @param Game[] $pastGames
67
     * @return UserBettingEntity[]
68
     */
69
    private function getGameIdForUserBets(UserInterface $user, array $pastGames): array
70
    {
71
        /** @var UserBettingEntity[] $userBets */
72
        $userBets = $this->entityManager
73
            ->getRepository(UserBettingEntity::class)
74
            ->findUserBettingByUserId($user, $pastGames);
75
76
        /** @var UserBettingEntity[] $gameId2UserBets */
77
        $gameId2UserBets = [];
78
        foreach ($userBets as $userBet) {
79
            $gameId2UserBets[$userBet->getGame()->getId()] = $userBet;
80
        }
81
        return $gameId2UserBets;
82
    }
83
}