Passed
Push — master ( 8387eb...5a11dc )
by Janko
07:59
created

HistoryRepository::truncateAllEntities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 2
rs 10
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 Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Stu\Component\History\HistoryTypeEnum;
10
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Stu\Orm\Entity\History;
12
13
/**
14
 * @extends EntityRepository<History>
15
 */
16
final class HistoryRepository extends EntityRepository implements HistoryRepositoryInterface
17
{
18
    #[Override]
19
    public function getRecent(): array
20
    {
21
        return $this->findBy(
22
            [],
23
            ['id' => 'desc'],
24
            10
25
        );
26
    }
27
28 2
    #[Override]
29
    public function getRecentWithoutPirate(): array
30
    {
31 2
        return $this->getEntityManager()
32 2
            ->createQuery(
33 2
                sprintf(
34 2
                    'SELECT h FROM %s h
35
                    WHERE COALESCE(h.source_user_id, 0) != :pirateId
36
                    AND COALESCE(h.target_user_id, 0) != :pirateId
37 2
                    ORDER BY h.id DESC',
38 2
                    History::class
39 2
                )
40 2
            )
41 2
            ->setParameter('pirateId', UserEnum::USER_NPC_KAZON)
42 2
            ->setMaxResults(10)
43 2
            ->getResult();
44
    }
45
46
47
    #[Override]
48
    public function getByTypeAndSearch(HistoryTypeEnum $type, int $limit): array
49
    {
50
        return $this->getEntityManager()
51
            ->createQuery(
52
                sprintf(
53
                    'SELECT h FROM %s h
54
                    WHERE h.type = :typeId
55
                    ORDER BY h.id desc',
56
                    History::class
57
                )
58
            )->setParameters(
59
                [
60
                    'typeId' => $type->value
61
                ]
62
            )
63
            ->setMaxResults($limit)
64
            ->getResult();
65
    }
66
67 1
    #[Override]
68
    public function getByTypeAndSearchWithoutPirate(HistoryTypeEnum $type, int $limit): array
69
    {
70
71 1
        return $this->getEntityManager()
72 1
            ->createQuery(
73 1
                sprintf(
74 1
                    'SELECT h FROM %s h
75
                    WHERE h.type = :typeId
76
                    AND COALESCE(h.source_user_id, 0) != :pirateId 
77
                    AND COALESCE(h.target_user_id, 0) != :pirateId
78 1
                    ORDER BY h.id desc',
79 1
                    History::class
80 1
                )
81 1
            )->setParameters(
82 1
                [
83 1
                    'typeId' => $type->value,
84 1
                    'pirateId' => UserEnum::USER_NPC_KAZON
85 1
                ]
86 1
            )
87 1
            ->setMaxResults($limit)
88 1
            ->getResult();
89
    }
90
91 1
    #[Override]
92
    public function getSumDestroyedByUser(int $source_user, int $target_user): int
93
    {
94 1
        return (int) $this->getEntityManager()
95 1
            ->createQuery(
96 1
                sprintf(
97 1
                    'SELECT COUNT(h.id) FROM %s h
98
                    WHERE h.type = 1
99
                    AND h.source_user_id = :source_user
100 1
                    AND h.target_user_id = :target_user',
101 1
                    History::class
102 1
                )
103 1
            )
104 1
            ->setParameters([
105 1
                'source_user' => $source_user,
106 1
                'target_user' => $target_user
107 1
            ])
108 1
            ->getSingleScalarResult();
109
    }
110
111
112 1
    #[Override]
113
    public function getAmountByType(int $typeId): int
114
    {
115 1
        return $this->count([
116 1
            'type' => $typeId
117 1
        ]);
118
    }
119
120
    #[Override]
121
    public function prototype(): History
122
    {
123
        return new History();
124
    }
125
126
    #[Override]
127
    public function save(History $history): void
128
    {
129
        $em = $this->getEntityManager();
130
131
        $em->persist($history);
132
    }
133
134
    #[Override]
135
    public function delete(History $history): void
136
    {
137
        $em = $this->getEntityManager();
138
139
        $em->remove($history);
140
    }
141
}
142