Passed
Push — master ( fcb0ff...ac0f12 )
by Nico
32:37 queued 12:14
created

ResearchedRepository::getResearchedPoints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 30
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 35
ccs 0
cts 19
cp 0
crap 2
rs 9.44
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\Module\Commodity\CommodityTypeEnum;
10
use Stu\Module\PlayerSetting\Lib\UserEnum;
11
use Stu\Orm\Entity\Researched;
12
use Stu\Orm\Entity\ResearchedInterface;
13
use Stu\Orm\Entity\UserInterface;
14
15
/**
16
 * @extends EntityRepository<Researched>
17
 */
18
final class ResearchedRepository extends EntityRepository implements ResearchedRepositoryInterface
19
{
20
    public function hasUserFinishedResearch(UserInterface $user, array $researchIds): bool
21
    {
22
        return $this->getEntityManager()
23
            ->createQuery(
24
                sprintf(
25
                    'SELECT COUNT(t.id) FROM %s t WHERE t.research_id IN (:researchIds) AND t.user_id = :userId AND t.finished > 0',
26
                    Researched::class,
27
                )
28
            )
29
            ->setParameters(['userId' => $user, 'researchIds' => $researchIds])
30
            ->getSingleScalarResult() > 0;
31
    }
32
33
    public function getListByUser(int $userId): array
34
    {
35
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM..., $userId)->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
36
            ->createQuery(
37
                sprintf(
38
                    'SELECT t FROM %s t WHERE t.user_id = :userId AND (t.finished > 0 OR t.aktiv > 0)',
39
                    Researched::class,
40
                )
41
            )
42
            ->setParameter('userId', $userId)
43
            ->getResult();
44
    }
45
46
    public function getFinishedListByUser(int $userId): array
47
    {
48
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM..., $userId)->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
49
            ->createQuery(
50
                sprintf(
51
                    'SELECT t FROM %s t WHERE t.user_id = :userId AND t.finished > 0',
52
                    Researched::class,
53
                )
54
            )
55
            ->setParameter('userId', $userId)
56
            ->getResult();
57
    }
58
59
    public function getCurrentResearch(UserInterface $user): ?ResearchedInterface
60
    {
61
        return $this->getEntityManager()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getEntityM...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return Stu\Orm\Entity\ResearchedInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
62
            ->createQuery(
63
                sprintf(
64
                    'SELECT t FROM %s t WHERE t.user = :user AND t.aktiv > 0',
65
                    Researched::class
66
                )
67
            )
68
            ->setParameter('user', $user)
69
            ->getOneOrNullResult();
70
    }
71
72
    public function getFor(int $researchId, int $userId): ?ResearchedInterface
73
    {
74
        return $this->findOneBy([
75
            'research_id' => $researchId,
76
            'user_id' => $userId,
77
        ]);
78
    }
79
80
    public function save(ResearchedInterface $researched): void
81
    {
82
        $em = $this->getEntityManager();
83
84
        $em->persist($researched);
85
        $em->flush();
86
    }
87
88
    public function delete(ResearchedInterface $researched): void
89
    {
90
        $em = $this->getEntityManager();
91
92
        $em->remove($researched);
93
        $em->flush();
94
    }
95
96
    public function prototype(): ResearchedInterface
97
    {
98
        return new Researched();
99
    }
100
101
    public function truncateForUser(int $userId): void
102
    {
103
        $this->getEntityManager()
104
            ->createQuery(
105
                sprintf(
106
                    'DELETE FROM %s t WHERE t.user_id = :userId',
107
                    Researched::class
108
                )
109
            )
110
            ->setParameter('userId', $userId)
111
            ->execute();
112
    }
113
114
    public function getResearchedPoints(): array
115
    {
116
        $rsm = new ResultSetMapping();
117
        $rsm->addScalarResult('user_id', 'user_id', 'integer');
118
        $rsm->addScalarResult('points', 'points', 'integer');
119
120
        return $this
121
            ->getEntityManager()
122
            ->createNativeQuery(
123
                'SELECT u.id as user_id,
124
                    (SELECT coalesce(sum(r.points), 0)
125
                        FROM stu_researched red JOIN stu_research r ON red.research_id = r.id
126
                        WHERE red.user_id = u.id AND r.commodity_id = :lvl1) 
127
                    + (SELECT coalesce(sum(r.points), 0)
128
                        FROM stu_researched red JOIN stu_research r ON red.research_id = r.id
129
                        WHERE red.user_id = u.id AND r.commodity_id = :lvl2) *2
130
                    + (SELECT coalesce(sum(r.points), 0)
131
                        FROM stu_researched red JOIN stu_research r ON red.research_id = r.id
132
                        WHERE red.user_id = u.id AND r.commodity_id = :lvl3) *3
133
                    + (SELECT coalesce(sum(r.points), 0)
134
                        FROM stu_researched red JOIN stu_research r ON red.research_id = r.id
135
                        WHERE red.user_id = u.id AND r.commodity_id IN (:lvl4)) *4 AS points
136
                FROM stu_user u
137
                WHERE u.id >= :firstUserId
138
                ORDER BY points DESC',
139
                $rsm
140
            )
141
            ->setParameters([
142
                'firstUserId' => UserEnum::USER_FIRST_ID,
143
                'lvl1' => CommodityTypeEnum::COMMODITY_RESEARCH_LVL1,
144
                'lvl2' => CommodityTypeEnum::COMMODITY_RESEARCH_LVL2,
145
                'lvl3' => CommodityTypeEnum::COMMODITY_RESEARCH_LVL3,
146
                'lvl4' => CommodityTypeEnum::COMMODITY_RESEARCH_LVL4
147
            ])
148
            ->getArrayResult();
149
    }
150
}
151