Passed
Push — develop ( f2b0b4...a1dd18 )
by BENARD
04:21
created

CanAskProofProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 14
dl 0
loc 31
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A load() 0 16 2
1
<?php
2
3
namespace VideoGamesRecords\CoreBundle\DataProvider;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\NonUniqueResultException;
7
use Doctrine\ORM\NoResultException;
8
use VideoGamesRecords\CoreBundle\Contracts\VgrCoreInterface;
9
use VideoGamesRecords\CoreBundle\Entity\Player;
10
11
class CanAskProofProvider implements VgrCoreInterface
12
{
13
    protected EntityManagerInterface $em;
14
15
    public function __construct(EntityManagerInterface $em)
16
    {
17
        $this->em = $em;
18
    }
19
20
    /**
21
     * @param Player $player
22
     * @return bool
23
     * @throws NoResultException
24
     * @throws NonUniqueResultException
25
     */
26
    public function load(Player $player): bool
27
    {
28
        $qb = $this->em->createQueryBuilder()
29
            ->from('VideoGamesRecords\CoreBundle\Entity\ProofRequest', 'request')
30
            ->select('COUNT(request)')
31
            ->where('request.playerRequesting = :player')
32
            ->setParameter('player', $player)
33
            ->andWhere('request.createdAt LIKE :now')
34
            ->setParameter('now', date('Y-m-d') . '%');
35
36
        $nb = $qb->getQuery()->getSingleScalarResult();
37
38
        if ($nb >= self::MAX_PROOF_REQUEST_DAY) {
39
            return false;
40
        }
41
        return true;
42
    }
43
}
44