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) |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.