Test Failed
Push — master ( e8cf0f...b39c66 )
by Nils
03:16
created

GameRepository::findGameByMatchdayAndTeams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 3
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Torakel\DatabaseBundle\Repository;
3
4
use Doctrine\Common\Collections\ArrayCollection;
5
use Torakel\DatabaseBundle\Entity\Event;
6
use Torakel\DatabaseBundle\Entity\Game;
7
use Torakel\DatabaseBundle\Entity\Matchday;
8
use Torakel\DatabaseBundle\Entity\Team;
9
use Doctrine\ORM\EntityRepository;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
/**
12
 * Class GameRepository
13
 *
14
 * @package Torakel\DatabaseBundle\Repository
15
 */
16
class GameRepository extends EntityRepository
17
{
18
19
    /**
20
     * createOrLoadGame
21
     *
22
     * Returns the Game entity for the given Matchday and Team entities.
23
     *
24
     * @param Matchday $matchday
25
     * @param Team $teamHome
26
     * @param Team $teamAway
27
     * @return Game
28
     */
29
    public function createOrLoadGame(Matchday $matchday, Team $teamHome, Team $teamAway): Game
30
    {
31
        $game = null;
32
        // check if all entities were persisted before
33
        if ($matchday->getId() > 0 && $teamHome->getId() > 0 && $teamAway->getId() > 0) {
34
            $game = $this->findGameByMatchdayAndTeams($matchday, $teamHome, $teamAway);
35
        }
36
        if (!$game) {
37
            $game = new Game();
38
            $game->setMatchday($matchday);
39
            $game->setTeamHome($teamHome);
40
            $game->setTeamAway($teamAway);
41
            $slug = $game->getMatchday()->getSlug(). '-' . $teamHome->getSlug() . '-' . $teamAway->getSlug();
42
            $game->setSlug($slug);
43
        } else {
44
            $game = array_shift($game);
0 ignored issues
show
Bug introduced by
$game of type Doctrine\Common\Collections\ArrayCollection is incompatible with the type array expected by parameter $array of array_shift(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            $game = array_shift(/** @scrutinizer ignore-type */ $game);
Loading history...
45
        }
46
47
        return $game;
48
    }
49
50
    /**
51
     * findPreviousGamesByTeamsAndEvent
52
     *
53
     * Returns all the previous games between the two given Team entities in the given Event.
54
     *
55
     * @param Team $team
56
     * @param Team $opponentTeam
57
     * @param Event $event
58
     * @return ArrayCollection
59
     */
60
    public function findPreviousGamesByTeamsAndEvent(Team $team, Team $opponentTeam, Event $event): ArrayCollection
61
    {
62
        $previousGames = $this->getEntityManager()
63
            ->createQuery(
64
                'SELECT g FROM AbenilFootballDbBundle:Game g
65
                  JOIN AbenilFootballDbBundle:Matchday m WITH g.matchday = m
66
                  JOIN AbenilFootballDbBundle:Round r WITH m.round = r
67
                  WHERE
68
                    (g.teamHome = :team AND g.teamAway = :opponentTeam)
69
                    OR (g.teamAway = :team AND g.teamHome = :opponentTeam)
70
                  AND r.event = :event'
71
            )
72
            ->setParameter('team', $team)
73
            ->setParameter('opponentTeam', $opponentTeam)
74
            ->setParameter('event', $event)
75
            ->getResult();
76
77
        return $previousGames;
78
    }
79
80
    /**
81
     * findGameByMatchdayAndTeams
82
     *
83
     * Returns the Game entity for the given Matchday and Team entities if it exists.
84
     *
85
     * @param Matchday $matchday
86
     * @param Team $team
87
     * @param Team $opponentTeam
88
     * @return ArrayCollection
89
     */
90
    public function findGameByMatchdayAndTeams(Matchday $matchday, Team $team, Team $opponentTeam): ArrayCollection
91
    {
92
        $game = $this->getEntityManager()
93
            ->createQuery(
94
                'SELECT g
95
              FROM TorakelDatabaseBundle:Game g
96
              WHERE g.matchday = :matchday AND g.teamHome = :teamHome AND g.teamAway = :teamAway'
97
            )
98
            ->setParameter('matchday', $matchday)
99
            ->setParameter('teamHome', $team)
100
            ->setParameter('teamAway', $opponentTeam)
101
            ->getResult();
102
103
        return $game;
104
105
    }
106
}