Completed
Push — master ( 557b3f...cc4751 )
by Adrien
12s
created

UserRepository::getNewUsersForMonth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 1
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 getAllUsernames()
54
    {
55
        $qb = $this->getEntityManager()->createQueryBuilder();
56
        $qb->select('u.username')
57
            ->from('UserBundle:User', 'u')
58
        ;
59
60
        $result = $qb->getQuery()->getResult();
61
62
        return array_column($result, 'username');
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getNewUsersForMonth(\DateTime $date)
69
    {
70
        $month = $date->format('m');
71
        $year = $date->format('Y');
72
        $lastDay = date('t', mktime(0, 0, 0, $month, 1, $year));
73
74
        $qb = $this->getEntityManager()->createQueryBuilder();
75
        $qb->select('u')
76
            ->from('UserBundle:User', 'u')
77
            ->where('u.date_registered >= :firstDayOfMonth')
78
            ->andWhere('u.date_registered <= :lastDayOfMonth')
79
            ->orderBy('u.date_registered', 'DESC')
80
            ->setParameter('firstDayOfMonth', date(sprintf('%s-%s-01', $year, $month)))
81
            ->setParameter('lastDayOfMonth', date(sprintf('%s-%s-%s', $year, $month, $lastDay)))
82
        ;
83
84
        $query = $qb->getQuery();
85
86
        return $query->getResult();
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getMonthlyBadgeChampions(\DateTime $date, TagInterface $tag, array $nbOfBadges)
93
    {
94
        $month = $date->format('m');
95
        $year = $date->format('Y');
96
        $lastDay = date('t', mktime(0, 0, 0, $month, 1, $year));
97
98
        $qb = $this->getEntityManager()->createQueryBuilder();
99
        $qb->select('u as user, COUNT(bc.id) as badgeCompletions')
100
            ->from('UserBundle:User', 'u')
101
            ->leftJoin('GameBundle:BadgeCompletion', 'bc', 'WITH', 'u.id = bc.user')
102
            ->leftJoin('bc.badge', 'b')
103
            ->leftJoin('b.tags', 't')
104
            ->where('t.id = :tagId')
105
                ->setParameter('tagId', $tag->getId())
106
            ->andWhere('bc.pending = 0')
107
            ->andWhere('bc.completionDate >= :firstDayOfMonth')
108
                ->setParameter('firstDayOfMonth', date(sprintf('%s-%s-01', $year, $month)))
109
            ->andWhere('bc.completionDate <= :lastDayOfMonth')
110
                ->setParameter('lastDayOfMonth', date(sprintf('%s-%s-%s', $year, $month, $lastDay)))
111
            ->groupBy('u')
112
            ->having('badgeCompletions IN (:maxValues)')
113
                ->setParameter('maxValues', $nbOfBadges,  Connection::PARAM_STR_ARRAY)
114
            ->orderBy('badgeCompletions', 'DESC')
115
        ;
116
117
        $query = $qb->getQuery();
118
119
        return $query->getResult();
120
    }
121
}
122