Passed
Pull Request — master (#2181)
by Nico
06:54
created

getUsersWithStorageOnTradepost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 1
dl 0
loc 22
ccs 0
cts 15
cp 0
crap 2
rs 9.7
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\NoResultException;
9
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...
10
use Stu\Component\Trade\TradeEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Trade\TradeEnum 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\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...
12
use Stu\Orm\Entity\Location;
13
use Stu\Orm\Entity\Station;
14
use Stu\Orm\Entity\Storage;
15
use Stu\Orm\Entity\TradeLicense;
16
use Stu\Orm\Entity\TradeOffer;
17
use Stu\Orm\Entity\TradePost;
18
use Stu\Orm\Entity\User;
19
20
/**
21
 * @extends EntityRepository<TradePost>
22
 */
23
final class TradePostRepository extends EntityRepository implements TradePostRepositoryInterface
24
{
25
    #[Override]
26
    public function prototype(): TradePost
27
    {
28
        return new TradePost();
29
    }
30
31
    #[Override]
32
    public function save(TradePost $tradePost): void
33
    {
34
        $em = $this->getEntityManager();
35
36
        $em->persist($tradePost);
37
    }
38
39
    #[Override]
40
    public function delete(TradePost $tradePost): void
41
    {
42
        $em = $this->getEntityManager();
43
44
        $em->remove($tradePost);
45
    }
46
47
    #[Override]
48
    public function getByUser(int $userId): array
49
    {
50
        return $this->findBy(
51
            ['user_id' => $userId]
52
        );
53
    }
54
55 1
    #[Override]
56
    public function getByUserLicense(int $userId): array
57
    {
58 1
        $time = time();
59 1
        return $this->getEntityManager()
60 1
            ->createQuery(
61 1
                sprintf(
62 1
                    'SELECT tp FROM %s tp WHERE tp.id IN (
63
                        SELECT tl.posts_id FROM %s tl WHERE tl.user_id = :userId AND tl.expired > :actime
64 1
                    ) ORDER BY tp.id ASC',
65 1
                    TradePost::class,
66 1
                    TradeLicense::class
67 1
                )
68 1
            )
69 1
            ->setParameters([
70 1
                'userId' => $userId,
71 1
                'actime' => $time
72 1
            ])
73 1
            ->getResult();
74
    }
75
76 1
    #[Override]
77
    public function getByUserLicenseOnlyNPC(int $userId): array
78
    {
79 1
        return $this->getEntityManager()
80 1
            ->createQuery(
81 1
                sprintf(
82 1
                    'SELECT tp FROM %s tp WHERE tp.user_id < :firstUserId AND tp.id IN (
83
                        SELECT tl.posts_id FROM %s tl WHERE tl.user_id = :userId AND tl.expired > :actime
84 1
                    )',
85 1
                    TradePost::class,
86 1
                    TradeLicense::class
87 1
                )
88 1
            )
89 1
            ->setParameters([
90 1
                'userId' => $userId,
91 1
                'actime' => time(),
92 1
                'firstUserId' => UserEnum::USER_FIRST_ID
93 1
            ])
94 1
            ->getResult();
95
    }
96
97
    #[Override]
98
    public function getByUserLicenseOnlyFerg(int $userId): array
99
    {
100
        return $this->getEntityManager()
101
            ->createQuery(
102
                sprintf(
103
                    'SELECT tp FROM %s tp WHERE tp.user_id = 14 AND tp.id IN (
104
                        SELECT tl.posts_id FROM %s tl WHERE tl.user_id = :userId AND tl.expired > :actime AND tl.posts_id = :tradepostId
105
                    )',
106
                    TradePost::class,
107
                    TradeLicense::class
108
                )
109
            )
110
            ->setParameters([
111
                'userId' => $userId,
112
                'actime' => time(),
113
                'tradepostId' => TradeEnum::DEALS_FERG_TRADEPOST_ID
114
            ])
115
            ->getResult();
116
    }
117
118
    #[Override]
119
    public function getClosestNpcTradePost(Location $location): ?TradePost
120
    {
121
        $layer = $location->getLayer();
122
        if ($layer === null) {
123
            return null;
124
        }
125
126
        try {
127
128
            return $this->getEntityManager()
129
                ->createQuery(
130
                    sprintf(
131
                        'SELECT tp
132
                            FROM %s tp
133
                            JOIN %s s
134
                            WITH tp.station = s
135
                            JOIN %s l
136
                            WITH s.location = l
137
                            WHERE tp.user_id < :firstUserId
138
                            AND l.layer = :layer
139
                            ORDER BY abs(l.cx - :cx) + abs(l.cy - :cy) ASC',
140
                        TradePost::class,
141
                        Station::class,
142
                        Location::class
143
                    )
144
                )
145
                ->setMaxResults(1)
146
                ->setParameters([
147
                    'layer' => $layer,
148
                    'cx' => $location->getCx(),
149
                    'cy' => $location->getCy(),
150
                    'firstUserId' => UserEnum::USER_FIRST_ID
151
                ])
152
                ->getSingleResult();
153
        } catch (NoResultException) {
154
            return null;
155
        }
156
    }
157
158
    #[Override]
159
    public function getUsersWithStorageOnTradepost(int $tradePostId): array
160
    {
161
        return $this->getEntityManager()
162
            ->createQuery(
163
                sprintf(
164
                    'SELECT u FROM %s u
165
                    WHERE EXISTS(
166
                            SELECT s.id FROM %s s
167
                            WHERE s.user_id = u.id
168
                            AND (s.tradepost_id = :tradePostId
169
                                OR s.tradeoffer_id IN (SELECT o.id FROM %s o WHERE o.posts_id = :tradePostId))
170
                            )',
171
                    User::class,
172
                    Storage::class,
173
                    TradeOffer::class
174
                )
175
            )
176
            ->setParameters([
177
                'tradePostId' => $tradePostId
178
            ])
179
            ->getResult();
180
    }
181
182
    #[Override]
183
    public function truncateAllTradeposts(): void
184
    {
185
        $this->getEntityManager()->createQuery(
186
            sprintf(
187
                'DELETE FROM %s tp',
188
                TradePost::class
189
            )
190
        )->execute();
191
    }
192
}
193