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

UserRepository::getNewUsersForMonth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 2
1
<?php
2
3
namespace Badger\Bundle\UserBundle\Doctrine\Repository;
4
5
use Badger\Component\User\Repository\UserRepositoryInterface;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\ORM\EntityRepository;
8
9
/**
10
 * @author  Adrien Pétremann <[email protected]>
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
class UserRepository extends EntityRepository implements UserRepositoryInterface
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function countAll()
19
    {
20
        $qb = $this->getEntityManager()->createQueryBuilder();
21
        $qb->select($qb->expr()->count('u'))
22
            ->from('UserBundle:User', 'u');
23
24
        $query = $qb->getQuery();
25
26
        return $query->getSingleScalarResult();
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getSortedUserByUnlockedBadges($order = 'DESC', $limit = 10)
33
    {
34
        $qb = $this->getEntityManager()->createQueryBuilder();
35
        $qb->select('u AS user, COUNT(bc.id) AS nbUnlockedBadges')
36
            ->from('UserBundle:User', 'u')
37
            ->leftJoin('GameBundle:BadgeCompletion', 'bc')
38
            ->where('bc.user = u')
39
            ->setMaxResults($limit)
40
            ->orderBy('nbUnlockedBadges', $order)
41
            ->groupBy('u')
42
        ;
43
44
        $query = $qb->getQuery();
45
46
        return $query->getResult();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getAllUsernames()
53
    {
54
        $qb = $this->getEntityManager()->createQueryBuilder();
55
        $qb->select('u.username')
56
            ->from('UserBundle:User', 'u')
57
        ;
58
59
        $result = $qb->getQuery()->getResult();
60
61
        return array_column($result, 'username');
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getNewUsersForMonth($month, $year)
68
    {
69
        $lastDay = date('t', mktime(0, 0, 0, $month, 1, $year));
70
71
        $qb = $this->getEntityManager()->createQueryBuilder();
72
        $qb->select('u')
73
            ->from('UserBundle:User', 'u')
74
            ->where('u.date_registered >= :firstDayOfMonth')
75
            ->andWhere('u.date_registered <= :lastDayOfMonth')
76
            ->orderBy('u.date_registered', 'DESC')
77
            ->setParameter('firstDayOfMonth', date(sprintf('%s-%s-01', $year, $month)))
78
            ->setParameter('lastDayOfMonth', date(sprintf('%s-%s-%s', $year, $month, $lastDay)))
79
        ;
80
81
        $query = $qb->getQuery();
82
83
        return $query->getResult();
84
    }
85
86
    public function getMonthlyBadgeChampions($month, $year, $tag, $maxValues)
87
    {
88
        $lastDay = date('t', mktime(0, 0, 0, $month, 1, $year));
89
90
        $qb = $this->getEntityManager()->createQueryBuilder();
91
        $qb->select('u as user, COUNT(bc.id) as badgeCompletions')
92
            ->from('UserBundle:User', 'u')
93
            ->leftJoin('GameBundle:BadgeCompletion', 'bc', 'WITH', 'u.id = bc.user')
94
            ->leftJoin('bc.badge', 'b')
95
            ->leftJoin('b.tags', 't')
96
            ->where('t.id = :tagId')
97
                ->setParameter('tagId', $tag->getId())
98
            ->andWhere('bc.pending = 0')
99
            ->andWhere('bc.completionDate >= :firstDayOfMonth')
100
                ->setParameter('firstDayOfMonth', date(sprintf('%s-%s-01', $year, $month)))
101
            ->andWhere('bc.completionDate <= :lastDayOfMonth')
102
                ->setParameter('lastDayOfMonth', date(sprintf('%s-%s-%s', $year, $month, $lastDay)))
103
            ->groupBy('u')
104
            ->having('badgeCompletions IN (:maxValues)')
105
                ->setParameter('maxValues', $maxValues,  Connection::PARAM_STR_ARRAY)
106
            ->orderBy('badgeCompletions', 'DESC')
107
        ;
108
109
        $query = $qb->getQuery();
110
111
        return $query->getResult();
112
    }
113
}
114