getAvailableAdventuresForUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 15
nc 2
nop 1
1
<?php
2
3
namespace Badger\Bundle\GameBundle\Doctrine\Repository;
4
5
use Badger\Component\Game\Repository\AdventureRepositoryInterface;
6
use Badger\Component\Game\Taggable\TaggableInterface;
7
use Doctrine\DBAL\Connection;
8
use Doctrine\ORM\EntityRepository;
9
10
/**
11
 * Doctrine implementation of repository for Adventure entities.
12
 *
13
 * @author  Marie Bochu <[email protected]>
14
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
15
 */
16
class AdventureRepository extends EntityRepository implements AdventureRepositoryInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 View Code Duplication
    public function getAvailableAdventuresForUser(TaggableInterface $user)
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...
22
    {
23
        $tagIds = [];
24
        foreach ($user->getTags() as $tag) {
25
            $tagIds[] = $tag->getId();
26
        }
27
28
        $qb = $this->createQueryBuilder('adventure');
29
        $qb->leftJoin('adventure.tags', 'tags')
30
            ->leftJoin('adventure.steps', 's')
31
            ->leftJoin('s.completions', 'sc', 'WITH', 'sc.user = :user AND sc.pending = 0')
32
            ->setParameter('user', $user)
33
            ->having('COUNT(sc) < COUNT(s)')
34
            ->andWhere('tags.id IN (:tagIds)')
35
            ->setMaxResults(15)
36
            ->groupBy('adventure.id')
37
            ->setParameter('tagIds', $tagIds, Connection::PARAM_STR_ARRAY)
38
        ;
39
40
        return $qb->getQuery()->getResult();
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 View Code Duplication
    public function getCompletedAdventuresForUser(TaggableInterface $user)
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...
47
    {
48
        $tagIds = [];
49
        foreach ($user->getTags() as $tag) {
50
            $tagIds[] = $tag->getId();
51
        }
52
53
        $qb = $this->createQueryBuilder('adventure');
54
        $qb->leftJoin('adventure.tags', 'tags')
55
            ->leftJoin('adventure.steps', 's')
56
            ->leftJoin('s.completions', 'sc', 'WITH', 'sc.user = :user AND sc.pending = 0')
57
            ->setParameter('user', $user)
58
            ->having('COUNT(sc) = COUNT(s)')
59
            ->andWhere('tags.id IN (:tagIds)')
60
            ->setMaxResults(15)
61
            ->groupBy('adventure.id')
62
            ->setParameter('tagIds', $tagIds, Connection::PARAM_STR_ARRAY)
63
        ;
64
65
        return $qb->getQuery()->getResult();
66
    }
67
}
68