UserBettingRepository::findByGameIdAndUserId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\GameBetting\Persistence\Repository;
4
5
use App\GameBetting\Persistence\Entity\UserBetting;
6
use App\User\Persistence\Entity\User;
7
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
8
use Symfony\Bridge\Doctrine\RegistryInterface;
9
10
/**
11
 * @method Game|null find($id, $lockMode = null, $lockVersion = null)
12
 * @method Game|null findOneBy(array $criteria, array $orderBy = null)
13
 * @method Game[]    findAll()
14
 * @method Game[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
15
 */
16
class UserBettingRepository extends ServiceEntityRepository
17
{
18
    public function __construct(RegistryInterface $registry)
19
    {
20
        parent::__construct($registry, UserBetting::class);
21
    }
22
23
    public function findUserBettingByUserId(User $user, array $games)
24
    {
25
        $gameIds = array_map(
26
            function ($game) {
27
                return $game->getId();
28
            },
29
            $games
30
        );
31
        
32
        return $this->createQueryBuilder('ub')
33
                    ->add('where', $this->createQueryBuilder('ub')->expr()->in('ub.game', $gameIds))
34
                    ->where('ub.user = :userId')
35
                    ->setParameter('userId', $user->getId())
36
                    ->getQuery()
37
                    ->getResult()
38
            ;
39
    }
40
41
    public function findByGameIdAndUserId(int $gameId, int $userId)
42
    {
43
        return $this->findOneBy([
44
            'game' => $gameId,
45
            'user' => $userId
46
        ]);
47
    }
48
}
49