Completed
Pull Request — master (#158)
by Adrien
02:05
created

BadgeCompletionRepository::userHasBadge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 2
1
<?php
2
3
namespace Badger\Bundle\GameBundle\Doctrine\Repository;
4
5
use Badger\Component\Game\Model\BadgeInterface;
6
use Badger\Component\Game\Repository\BadgeCompletionRepositoryInterface;
7
use Badger\Component\Game\Repository\TagSearchableRepositoryInterface;
8
use Badger\Component\User\Model\UserInterface;
9
use Doctrine\DBAL\Connection;
10
use Doctrine\ORM\EntityRepository;
11
12
/**
13
 * @author  Adrien Pétremann <[email protected]>
14
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
15
 */
16
class BadgeCompletionRepository extends EntityRepository implements
17
    BadgeCompletionRepositoryInterface,
18
    TagSearchableRepositoryInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function getCompletionBadgesByUser(UserInterface $user, $pending = null)
24
    {
25
        $qb = $this->getEntityManager()->createQueryBuilder();
26
        $qb->select('badge.id')
27
            ->from('GameBundle:BadgeCompletion', 'completion')
28
            ->leftJoin('completion.badge', 'badge')
29
            ->where('completion.user = :user')
30
            ->setParameter('user', $user);
31
32
        if (is_bool($pending)) {
33
            $qb->andWhere('completion.pending = :pending')
34
                ->setParameter('pending', $pending ? '1' : '0');
35
        }
36
37
        $queryResult = $qb->getQuery()->getScalarResult();
38
39
        return array_column($queryResult, 'id');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function userHasBadge(UserInterface $user, BadgeInterface $badge)
46
    {
47
        $qb = $this->getEntityManager()->createQueryBuilder();
48
        $qb->select('COUNT(badge.id)')
49
            ->from('GameBundle:BadgeCompletion', 'completion')
50
            ->leftJoin('completion.badge', 'badge')
51
            ->where('completion.user = :user')
52
            ->andWhere('completion.badge = :badge')
53
            ->andWhere('completion.pending = 0')
54
            ->setParameter('user', $user)
55
            ->setParameter('badge', $badge);
56
57
        return (1 === intval($qb->getQuery()->getSingleScalarResult()));
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function findByTags(array $tags)
64
    {
65
        $tagIds = [];
66
        foreach ($tags as $tag) {
67
            $tagIds[] = $tag->getId();
68
        }
69
70
        $qb = $this->createQueryBuilder('bc');
71
        $qb->leftJoin('bc.badge', 'b')
72
            ->leftJoin('b.tags', 't')
73
            ->where('t.id IN (:ids)')->setParameter('ids', $tagIds, Connection::PARAM_STR_ARRAY)
74
            ->orderBy('bc.completionDate', 'desc')
75
            ->setMaxResults(15)
76
            ->groupBy('bc.id');
77
78
        return $qb->getQuery()->getResult();
79
    }
80
81
    public function getMostUnlockedBadgesForMonth($month, $year, $tag, $limit = 3)
82
    {
83
        $lastDay = date('t', mktime(0, 0, 0, $month, 1, $year));
84
85
        $qb = $this->getEntityManager()->createQueryBuilder();
86
        $qb->select('COUNT(DISTINCT(bc.id)) as nbCompletions, b as badge')
87
            ->from('GameBundle:Badge', 'b')
88
            ->leftJoin('b.completions', 'bc')
89
            ->leftJoin('b.tags', 't')
90
            ->where('t.id = :tagId')
91
                ->setParameter('tagId', $tag->getId())
92
            ->andWhere('bc.pending = 0')
93
            ->andWhere('bc.completionDate >= :firstDayOfMonth')
94
                ->setParameter('firstDayOfMonth', date(sprintf('%s-%s-01', $year, $month)))
95
            ->andWhere('bc.completionDate <= :lastDayOfMonth')
96
                ->setParameter('lastDayOfMonth', date(sprintf('%s-%s-%s', $year, $month, $lastDay)))
97
            ->groupBy('b.id')
98
            ->orderBy('nbCompletions', 'desc')
99
            ->setMaxResults($limit)
100
        ;
101
102
        return $qb->getQuery()->getResult();
103
    }
104
105
    public function getNumberOfUnlocksForMonth($month, $year, $tag, $user = null)
106
    {
107
        $lastDay = date('t', mktime(0, 0, 0, $month, 1, $year));
108
109
        $qb = $this->getEntityManager()->createQueryBuilder();
110
        $qb->select('DISTINCT(COUNT(bc)) as nbCompletions')
111
            ->from('GameBundle:BadgeCompletion', 'bc')
112
            ->leftJoin('bc.badge', 'b')
113
            ->leftJoin('b.tags', 't')
114
            ->where('t.id = :tagId')
115
                ->setParameter('tagId', $tag->getId())
116
            ->andWhere('bc.pending = 0')
117
            ->andWhere('bc.completionDate >= :firstDayOfMonth')
118
                ->setParameter('firstDayOfMonth', date(sprintf('%s-%s-01', $year, $month)))
119
            ->andWhere('bc.completionDate <= :lastDayOfMonth')
120
                ->setParameter('lastDayOfMonth', date(sprintf('%s-%s-%s', $year, $month, $lastDay)))
121
            ->groupBy('bc.user')
122
            ->orderBy('nbCompletions', 'desc')
123
            ->setMaxResults(3)
124
        ;
125
126
        if (null !== $user) {
127
            $qb->andWhere('bc.user = :user')
128
                ->setParameter('user', $user);
129
        }
130
131
        return $qb->getQuery()->getResult();
132
    }
133
}
134