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

getMostUnlockedBadgesForDate()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 3
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 getMostUnlockedBadgesForDate(\DateTime $date, TagInterface $tag, $limit = 3)
86
    {
87
        $month = $date->format('m');
88
        $year = $date->format('Y');
89
        $lastDay = date('t', mktime(0, 0, 0, $month, 1, $year));
90
91
        $qb = $this->getEntityManager()->createQueryBuilder();
92
        $qb->select('COUNT(DISTINCT(bc.id)) as nbCompletions, b as badge')
93
            ->from('GameBundle:Badge', 'b')
94
            ->leftJoin('b.completions', 'bc')
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('b.id')
104
            ->orderBy('nbCompletions', 'desc')
105
            ->setMaxResults($limit)
106
        ;
107
108
        return $qb->getQuery()->getResult();
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function getTopNumberOfUnlocksForDate(\DateTime $date, TagInterface $tag, $user = null)
115
    {
116
        $month = $date->format('m');
117
        $year = $date->format('Y');
118
        $lastDay = date('t', mktime(0, 0, 0, $month, 1, $year));
119
120
        $qb = $this->getEntityManager()->createQueryBuilder();
121
        $qb->select('DISTINCT(COUNT(bc)) as nbCompletions')
122
            ->from('GameBundle:BadgeCompletion', 'bc')
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', $year, $month)))
130
            ->andWhere('bc.completionDate <= :lastDayOfMonth')
131
                ->setParameter('lastDayOfMonth', date(sprintf('%s-%s-%s', $year, $month, $lastDay)))
132
            ->groupBy('bc.user')
133
            ->orderBy('nbCompletions', 'desc')
134
            ->setMaxResults(3)
135
        ;
136
137
        if (null !== $user) {
138
            $qb->andWhere('bc.user = :user')
139
                ->setParameter('user', $user);
140
        }
141
142
        return $qb->getQuery()->getResult();
143
    }
144
}
145