Completed
Pull Request — master (#178)
by Adrien
04:44
created

UserRepository::getBadgeChampions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 16
nc 1
nop 2
1
<?php
2
3
namespace Badger\Bundle\UserBundle\Doctrine\Repository;
4
5
use Badger\Component\Game\Model\TagInterface;
6
use Badger\Component\User\Repository\UserRepositoryInterface;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\ORM\EntityRepository;
9
10
/**
11
 * @author  Adrien Pétremann <[email protected]>
12
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
13
 */
14
class UserRepository extends EntityRepository implements UserRepositoryInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function countAll()
20
    {
21
        $qb = $this->getEntityManager()->createQueryBuilder();
22
        $qb->select($qb->expr()->count('u'))
23
            ->from('UserBundle:User', 'u');
24
25
        $query = $qb->getQuery();
26
27
        return $query->getSingleScalarResult();
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getSortedUserByUnlockedBadges($order = 'DESC', $limit = 10)
34
    {
35
        $qb = $this->getEntityManager()->createQueryBuilder();
36
        $qb->select('u AS user, COUNT(bc.id) AS nbUnlockedBadges')
37
            ->from('UserBundle:User', 'u')
38
            ->leftJoin('GameBundle:BadgeCompletion', 'bc')
39
            ->where('bc.user = u')
40
            ->setMaxResults($limit)
41
            ->orderBy('nbUnlockedBadges', $order)
42
            ->groupBy('u')
43
        ;
44
45
        $query = $qb->getQuery();
46
47
        return $query->getResult();
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getSortedUserUnlockedBadgesByTag($tag, $order = 'DESC', $limit = 15)
54
    {
55
        $qb = $this->createQueryBuilder('u');
56
        $qb->select('u AS user, COUNT(bc.id) AS nbUnlockedBadges')
57
            ->leftJoin('GameBundle:BadgeCompletion', 'bc', 'WITH', 'bc.user = u')
58
            ->innerJoin('bc.badge', 'badge')
59
            ->leftJoin('badge.tags', 't')
60
            ->where('t.id = :id')->setParameter('id', $tag->getId())
61
            ->setMaxResults($limit)
62
            ->orderBy('nbUnlockedBadges', $order)
63
            ->groupBy('u')
64
        ;
65
66
        $query = $qb->getQuery();
67
68
        return $query->getResult();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getAllUsernames()
75
    {
76
        $qb = $this->getEntityManager()->createQueryBuilder();
77
        $qb->select('u.username')
78
            ->from('UserBundle:User', 'u')
79
        ;
80
81
        $result = $qb->getQuery()->getResult();
82
83
        return array_column($result, 'username');
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getNewUsersForMonth(\DateTime $date)
90
    {
91
        $month = $date->format('m');
92
        $year = $date->format('Y');
93
        $lastDay = date('t', mktime(0, 0, 0, $month, 1, $year));
94
95
        $qb = $this->getEntityManager()->createQueryBuilder();
96
        $qb->select('u')
97
            ->from('UserBundle:User', 'u')
98
            ->where('u.date_registered >= :firstDayOfMonth')
99
            ->andWhere('u.date_registered <= :lastDayOfMonth')
100
            ->orderBy('u.date_registered', 'DESC')
101
            ->setParameter('firstDayOfMonth', date(sprintf('%s-%s-01 00:00:01', $year, $month)))
102
            ->setParameter('lastDayOfMonth', date(sprintf('%s-%s-%s 23:59:59', $year, $month, $lastDay)))
103
        ;
104
105
        $query = $qb->getQuery();
106
107
        return $query->getResult();
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getMonthlyBadgeChampions(\DateTime $date, TagInterface $tag, array $nbOfBadges)
114
    {
115
        $month = $date->format('m');
116
        $year = $date->format('Y');
117
        $lastDay = date('t', mktime(0, 0, 0, $month, 1, $year));
118
119
        $qb = $this->getEntityManager()->createQueryBuilder();
120
        $qb->select('u as user, COUNT(bc.id) as badgeCompletions')
121
            ->from('UserBundle:User', 'u')
122
            ->leftJoin('GameBundle:BadgeCompletion', 'bc', 'WITH', 'u.id = bc.user')
123
            ->leftJoin('bc.badge', 'b')
124
            ->leftJoin('b.tags', 't')
125
            ->where('t.id = :tagId')
126
                ->setParameter('tagId', $tag->getId())
127
            ->andWhere('bc.pending = 0')
128
            ->andWhere('bc.completionDate >= :firstDayOfMonth')
129
                ->setParameter('firstDayOfMonth', date(sprintf('%s-%s-01 00:00:01', $year, $month)))
130
            ->andWhere('bc.completionDate <= :lastDayOfMonth')
131
                ->setParameter('lastDayOfMonth', date(sprintf('%s-%s-%s 23:59:59', $year, $month, $lastDay)))
132
            ->groupBy('u')
133
            ->having('badgeCompletions IN (:maxValues)')
134
                ->setParameter('maxValues', $nbOfBadges,  Connection::PARAM_STR_ARRAY)
135
            ->orderBy('badgeCompletions', 'DESC')
136
        ;
137
138
        $query = $qb->getQuery();
139
140
        return $query->getResult();
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function getBadgeChampions(TagInterface $tag, array $nbOfBadges)
147
    {
148
        $qb = $this->getEntityManager()->createQueryBuilder();
149
        $qb->select('u as user, COUNT(bc.id) as badgeCompletions')
150
            ->from('UserBundle:User', 'u')
151
            ->leftJoin('GameBundle:BadgeCompletion', 'bc', 'WITH', 'u.id = bc.user')
152
            ->leftJoin('bc.badge', 'b')
153
            ->leftJoin('b.tags', 't')
154
            ->where('t.id = :tagId')
155
                ->setParameter('tagId', $tag->getId())
156
            ->andWhere('bc.pending = 0')
157
            ->groupBy('u')
158
            ->having('badgeCompletions IN (:maxValues)')
159
                ->setParameter('maxValues', $nbOfBadges,  Connection::PARAM_STR_ARRAY)
160
            ->orderBy('badgeCompletions', 'DESC')
161
        ;
162
163
        $query = $qb->getQuery();
164
165
        return $query->getResult();
166
    }
167
}
168