Passed
Push — master ( 0cf75a...c878e4 )
by Nico
53:25 queued 24:26
created

HistoryRepository::getSumDestroyedByUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 17
ccs 0
cts 14
cp 0
crap 2
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Stu\Component\History\HistoryTypeEnum;
9
use Stu\Orm\Entity\History;
10
use Stu\Orm\Entity\HistoryInterface;
11
use Stu\Module\PlayerSetting\Lib\UserEnum;
12
13
/**
14
 * @extends EntityRepository<History>
15
 */
16
final class HistoryRepository extends EntityRepository implements HistoryRepositoryInterface
17
{
18
    public function getRecent(): array
19
    {
20
        return $this->findBy(
21
            [],
22
            ['id' => 'desc'],
23
            10
24
        );
25
    }
26
27
    public function getRecentWithoutPirate(): array
28
    {
29
        return $this->getEntityManager()
30
            ->createQuery(
31
                sprintf(
32
                    'SELECT h FROM %s h
33
                    WHERE COALESCE(h.source_user_id, 0) != :pirateId
34
                    AND COALESCE(h.target_user_id, 0) != :pirateId
35
                    ORDER BY h.id DESC',
36
                    History::class
37
                )
38
            )
39
            ->setParameter('pirateId', UserEnum::USER_NPC_KAZON)
40
            ->setMaxResults(10)
41
            ->getResult();
42
    }
43
44
45
    public function getByTypeAndSearch(HistoryTypeEnum $type, int $limit, $search): array
46
    {
47
        $searchCriteria = $search ? 'AND UPPER(h.text) like UPPER(:search)' : '';
48
49
        return $this->getEntityManager()
50
            ->createQuery(
51
                sprintf(
52
                    'SELECT h FROM %s h
53
                    WHERE h.type = :typeId
54
                    %s
55
                    ORDER BY h.id desc',
56
                    History::class,
57
                    $searchCriteria
58
                )
59
            )->setParameters(
60
                $search ? [
61
                    'typeId' => $type->value,
62
                    'search' => sprintf('%%%s%%', $search)
63
                ] : ['typeId' => $type->value]
64
            )
65
            ->setMaxResults($limit)
66
            ->getResult();
67
    }
68
69
    public function getByTypeAndSearchWithoutPirate(HistoryTypeEnum $type, int $limit, $search): array
70
    {
71
        $searchCriteria = $search ? 'AND UPPER(h.text) like UPPER(:search)' : '';
72
73
        return $this->getEntityManager()
74
            ->createQuery(
75
                sprintf(
76
                    'SELECT h FROM %s h
77
                    WHERE h.type = :typeId
78
                    %s
79
                    AND COALESCE(h.source_user_id, 0) != :pirateId 
80
                    AND COALESCE(h.target_user_id, 0) != :pirateId
81
                    ORDER BY h.id desc',
82
                    History::class,
83
                    $searchCriteria
84
                )
85
            )->setParameters(
86
                $search ? [
87
                    'typeId' => $type->value,
88
                    'pirateId' => UserEnum::USER_NPC_KAZON,
89
                    'search' => sprintf('%%%s%%', $search)
90
                ] : ['typeId' => $type->value, 'pirateId' => UserEnum::USER_NPC_KAZON]
91
            )
92
            ->setMaxResults($limit)
93
            ->getResult();
94
    }
95
96
    public function getSumDestroyedByUser(int $source_user, int $target_user): int
97
    {
98
        return (int) $this->getEntityManager()
99
            ->createQuery(
100
                sprintf(
101
                    'SELECT COUNT(h.id) FROM %s h
102
                    WHERE h.type = 1
103
                    AND h.source_user_id = :source_user
104
                    AND h.target_user_id = :target_user',
105
                    History::class
106
                )
107
            )
108
            ->setParameters([
109
                'source_user' => $source_user,
110
                'target_user' => $target_user
111
            ])
112
            ->getSingleScalarResult();
113
    }
114
115
116
    public function getAmountByType(int $typeId): int
117
    {
118
        return $this->count([
119
            'type' => $typeId
120
        ]);
121
    }
122
123
    public function prototype(): HistoryInterface
124
    {
125
        return new History();
126
    }
127
128
    public function save(HistoryInterface $history): void
129
    {
130
        $em = $this->getEntityManager();
131
132
        $em->persist($history);
133
    }
134
135
    public function delete(HistoryInterface $history): void
136
    {
137
        $em = $this->getEntityManager();
138
139
        $em->remove($history);
140
    }
141
142
    public function truncateAllEntities(): void
143
    {
144
        $this->getEntityManager()->createQuery(
145
            sprintf(
146
                'DELETE FROM %s h',
147
                History::class
148
            )
149
        )->execute();
150
    }
151
}
152