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

CanAskProofProvider::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 16
rs 9.9
c 1
b 0
f 0
cc 2
nc 2
nop 1
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