Passed
Push — master ( 4c294d...73245e )
by Nico
31:23
created

ResearchRepository::getForFaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 16
ccs 0
cts 13
cp 0
crap 2
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\ORM\Query\ResultSetMapping;
9
use Stu\Component\Research\ResearchModeEnum;
10
use Stu\Orm\Entity\Commodity;
11
use Stu\Orm\Entity\Research;
12
use Stu\Orm\Entity\ResearchDependency;
13
use Stu\Orm\Entity\Researched;
14
use Stu\Orm\Entity\ResearchInterface;
15
use Stu\Orm\Entity\UserInterface;
16
17
/**
18
 * @extends EntityRepository<Research>
19
 */
20
final class ResearchRepository extends EntityRepository implements ResearchRepositoryInterface
21
{
22
    public function getAvailableResearch(int $userId): array
23
    {
24
        return $this->getEntityManager()
25
            ->createQuery(
26
                sprintf(
27
                    'SELECT r FROM %s r
28
                    JOIN %s c
29
                    WITH r.commodity_id = c.id
30
                    WHERE r.id NOT IN (
31
                        SELECT red.research_id from %s red WHERE red.user_id = :userId
32
                    )
33
                    ORDER BY c.sort ASC, r.id ASC',
34
                    Research::class,
35
                    Commodity::class,
36
                    Researched::class
37
                )
38
            )
39
            ->setParameter('userId', $userId)
40
            ->getResult();
41
    }
42
43
    public function getColonyTypeLimitByUser(UserInterface $user, int $colonyType): int
44
    {
45
        return (int)$this->getEntityManager()->createQuery(
46
            sprintf(
47
                'SELECT SUM(r.upper_limit_colony_amount) FROM %s r
48
                WHERE r.upper_limit_colony_type = :colonyType
49
                AND r.id IN (
50
                    SELECT ru.research_id FROM %s ru WHERE ru.user_id = :userId AND ru.aktiv = :activeState
51
                )',
52
                Research::class,
53
                Researched::class
54
            )
55
        )->setParameters([
56
            'userId' => $user,
57
            'activeState' => 0,
58
            'colonyType' => $colonyType
59
        ])->getSingleScalarResult();
60
    }
61
62
    public function getPossibleResearchByParent(int $researchId): array
63
    {
64
        return $this->getEntityManager()
65
            ->createQuery(
66
                sprintf(
67
                    'SELECT r
68
                    FROM %s r
69
                    WHERE r.id IN (
70
                        SELECT rd.research_id from %s rd
71
                        WHERE rd.depends_on = :researchId
72
                        AND rd.mode != :modeExclude
73
                    )',
74
                    Research::class,
75
                    ResearchDependency::class,
76
                )
77
            )
78
            ->setParameters([
79
                'researchId' => $researchId,
80
                'modeExclude' => ResearchModeEnum::EXCLUDE->value
81
            ])
82
            ->getResult();
83
    }
84
85
    public function save(ResearchInterface $research): void
86
    {
87
        $em = $this->getEntityManager();
88
89
        $em->persist($research);
90
    }
91
}
92