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

getMostUnlockedBadgesForMonth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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