Completed
Pull Request — master (#145)
by Adrien
02:57
created

BadgeCompletionRepository::userHasBadge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
1
<?php
2
3
namespace Badger\Bundle\GameBundle\Doctrine\Repository;
4
5
use Badger\Component\Game\Model\BadgeInterface;
6
use Badger\Component\Game\Repository\BadgeCompletionRepositoryInterface;
7
use Badger\Component\Game\Repository\TagSearchableRepositoryInterface;
8
use Badger\Component\User\Model\UserInterface;
9
use Doctrine\DBAL\Connection;
10
use Doctrine\ORM\EntityRepository;
11
12
/**
13
 * @author  Adrien Pétremann <[email protected]>
14
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
15
 */
16
class BadgeCompletionRepository extends EntityRepository implements
17
    BadgeCompletionRepositoryInterface,
18
    TagSearchableRepositoryInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function getCompletionBadgesByUser(UserInterface $user, $pending = null)
24
    {
25
        $qb = $this->getEntityManager()->createQueryBuilder();
26
        $qb->select('badge.id')
27
            ->from('GameBundle:BadgeCompletion', 'completion')
28
            ->leftJoin('completion.badge', 'badge')
29
            ->where('completion.user = :user')
30
            ->setParameter('user', $user);
31
32
        if (is_bool($pending)) {
33
            $qb->andWhere('completion.pending = :pending')
34
                ->setParameter('pending', $pending ? '1' : '0');
35
        }
36
37
        $queryResult = $qb->getQuery()->getScalarResult();
38
39
        return array_column($queryResult, 'id');
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 View Code Duplication
    public function userHasBadge(UserInterface $user, BadgeInterface $badge)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        $qb = $this->getEntityManager()->createQueryBuilder();
48
        $qb->select('badge.id')
49
            ->from('GameBundle:BadgeCompletion', 'completion')
50
            ->leftJoin('completion.badge', 'badge')
51
            ->where('completion.user = :user')
52
            ->andWhere('completion.pending = 0')
53
            ->setParameter('user', $user);
54
55
        $queryResult = $qb->getQuery()->getScalarResult();
56
57
        return array_column($queryResult, 'id');
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function findByTags(array $tags)
64
    {
65
        $tagIds = [];
66
        foreach ($tags as $tag) {
67
            $tagIds[] = $tag->getId();
68
        }
69
70
        $qb = $this->createQueryBuilder('bc');
71
        $qb->leftJoin('bc.badge', 'b')
72
            ->leftJoin('b.tags', 't')
73
            ->where('t.id IN (:ids)')->setParameter('ids', $tagIds, Connection::PARAM_STR_ARRAY)
74
            ->orderBy('bc.completionDate', 'desc')
75
            ->setMaxResults(15)
76
            ->groupBy('bc.id');
77
78
        return $qb->getQuery()->getResult();
79
    }
80
81
82
}
83