Passed
Push — dev ( cd32fa...d689b9 )
by Nico
05:08
created

TradePostRepository::getByUserLicense()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

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