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

GameRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A findActiveGames() 0 13 1
A findPastGames() 0 8 1
A __construct() 0 3 1
A findFutureGames() 0 8 1
1
<?php
2
3
namespace App\GameCore\Persistence\Repository;
4
5
use App\GameCore\Persistence\Entity\Game;
6
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7
use Symfony\Bridge\Doctrine\RegistryInterface;
8
9
/**
10
 * @method Game|null find($id, $lockMode = null, $lockVersion = null)
11
 * @method Game|null findOneBy(array $criteria, array $orderBy = null)
12
 * @method Game[]    findAll()
13
 * @method Game[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
14
 */
15
class GameRepository extends ServiceEntityRepository
16
{
17
    const GAME_TIME_RANGE = 'PT115M';
18
19
    /**
20
     * @param RegistryInterface $registry
21
     */
22
    public function __construct(RegistryInterface $registry)
23
    {
24
        parent::__construct($registry, Game::class);
25
    }
26
27
    /**
28
     * @return Game[]
29
     */
30
    public function findFutureGames()
31
    {
32
        $qb = $this->createQueryBuilder('game');
33
        $qb->where('game.date > :date')
34
            ->setParameter('date', new \DateTime())
35
            ->orderBy('game.date', 'ASC');
36
37
        return $qb->getQuery()->getResult();
38
    }
39
40
    /**
41
     * @return Game[]
42
     */
43
    public function findPastGames()
44
    {
45
        $qb = $this->createQueryBuilder('game');
46
        $qb->where('game.date <= :date')
47
            ->setParameter('date', new \DateTime())
48
            ->orderBy('game.date', 'ASC');
49
50
        return $qb->getQuery()->getResult();
51
    }
52
53
    /**
54
     * @return Game[]
55
     * @throws \Exception
56
     */
57
    public function findActiveGames()
58
    {
59
        $dateGameStartRange = new \DateTime();
60
        $dateGameStartRange->sub(new \DateInterval(self::GAME_TIME_RANGE));
61
62
        $qb = $this->createQueryBuilder('game');
63
        $qb->where('game.date <= :dateNow');
64
        $qb->andWhere('game.date >= :dateGameStartRange');
65
        $qb->setParameter('dateNow', new \DateTime());
66
        $qb->setParameter('dateGameStartRange', $dateGameStartRange);
67
        $qb->orderBy('game.date', 'ASC');
68
        
69
        return $qb->getQuery()->getResult();
70
    }
71
}
72