Passed
Push — dev ( 8387eb...f589dc )
by Janko
11:47
created

TradeOfferRepository::truncateAllTradeOffers()   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 Doctrine\ORM\Query\ResultSetMapping;
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\TradeLicense;
13
use Stu\Orm\Entity\TradeOffer;
14
15
/**
16
 * @extends EntityRepository<TradeOffer>
17
 */
18
final class TradeOfferRepository extends EntityRepository implements TradeOfferRepositoryInterface
19
{
20
    #[Override]
21
    public function prototype(): TradeOffer
22
    {
23
        return new TradeOffer();
24
    }
25
26
    #[Override]
27
    public function save(TradeOffer $post): void
28
    {
29
        $em = $this->getEntityManager();
30
31
        $em->persist($post);
32
    }
33
34
    #[Override]
35
    public function delete(TradeOffer $post): void
36
    {
37
        $em = $this->getEntityManager();
38
39
        $em->remove($post);
40
        $em->flush();
41
    }
42
43
    #[Override]
44
    public function truncateByUser(int $userId): void
45
    {
46
        /** @noinspection SyntaxError */
47
        $this->getEntityManager()
48
            ->createQuery(
49
                sprintf(
50
                    'DELETE FROM %s to WHERE to.user_id = :userId',
51
                    TradeOffer::class
52
                )
53
            )
54
            ->setParameter('userId', $userId)
55
            ->execute();
56
    }
57
58 1
    #[Override]
59
    public function getByTradePostAndUserAndOfferedCommodity(
60
        int $tradePostId,
61
        int $userId,
62
        int $offeredCommodityId
63
    ): array {
64 1
        return $this->findBy([
65 1
            'posts_id' => $tradePostId,
66 1
            'user_id' => $userId,
67 1
            'gg_id' => $offeredCommodityId
68 1
        ]);
69
    }
70
71
    #[Override]
72
    public function getByTradePostAndUserAndCommodities(
73
        int $tradePostId,
74
        int $userId,
75
        int $offeredCommodityId,
76
        int $wantedCommodityId
77
    ): array {
78
        return $this->findBy([
79
            'posts_id' => $tradePostId,
80
            'user_id' => $userId,
81
            'gg_id' => $offeredCommodityId,
82
            'wg_id' => $wantedCommodityId
83
        ]);
84
    }
85
86 1
    #[Override]
87
    public function getByUserLicenses(int $userId, ?int $commodityId, ?int $tradePostId, int $direction): array
88
    {
89 1
        if ($commodityId !== null && $commodityId !== 0) {
90
            if ($direction === TradeEnum::FILTER_COMMODITY_IN_BOTH) {
91
                $commoditySql = sprintf(' AND (to.gg_id = %1$d OR to.wg_id = %1$d) ', $commodityId);
92
            } elseif ($direction === TradeEnum::FILTER_COMMODITY_IN_OFFER) {
93
                $commoditySql = sprintf(' AND to.gg_id = %d ', $commodityId);
94
            } else {
95
                $commoditySql = sprintf(' AND to.wg_id = %d ', $commodityId);
96
            }
97
        } else {
98 1
            $commoditySql = '';
99
        }
100
101 1
        $time = time();
102 1
        return $this->getEntityManager()
103 1
            ->createQuery(
104 1
                sprintf(
105 1
                    'SELECT to FROM %s to WHERE to.posts_id IN (
106
                        SELECT tl.posts_id FROM %s tl WHERE tl.user_id = :userId AND tl.expired > :actime
107
                    ) %s %s
108 1
                    ORDER BY to.date DESC',
109 1
                    TradeOffer::class,
110 1
                    TradeLicense::class,
111 1
                    $commoditySql,
112 1
                    $tradePostId != null ? sprintf(' AND to.posts_id = %d ', $tradePostId) : ''
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $tradePostId of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
113 1
                )
114 1
            )
115 1
            ->setParameters([
116 1
                'userId' => $userId,
117 1
                'actime' => $time
118 1
            ])
119 1
            ->getResult();
120
    }
121
122
    #[Override]
123
    public function getSumByTradePostAndUser(int $tradePostId, int $userId): int
124
    {
125
        return (int) $this->getEntityManager()
126
            ->createQuery(
127
                sprintf(
128
                    'SELECT SUM(to.gg_count * to.amount) FROM %s to WHERE to.posts_id = :tradePostId AND to.user_id = :userId',
129
                    TradeOffer::class
130
                )
131
            )
132
            ->setParameters([
133
                'tradePostId' => $tradePostId,
134
                'userId' => $userId
135
            ])
136
            ->getSingleScalarResult();
137
    }
138
139 1
    #[Override]
140
    public function getGroupedSumByTradePostAndUser(int $tradePostId, int $userId): array
141
    {
142 1
        $rsm = new ResultSetMapping();
143 1
        $rsm->addScalarResult('commodity_id', 'commodity_id');
144 1
        $rsm->addScalarResult('amount', 'amount');
145 1
        $rsm->addScalarResult('commodity_name', 'commodity_name');
146
147 1
        return $this->getEntityManager()
148 1
            ->createNativeQuery(
149 1
                'SELECT tro.gg_id as commodity_id, SUM(tro.gg_count * tro.amount) as amount, c.name as commodity_name
150
                    FROM stu_trade_offers tro LEFT JOIN stu_commodity c ON c.id = tro.gg_id WHERE
151 1
                    tro.posts_id = :tradePostId AND tro.user_id = :userId GROUP BY tro.gg_id,c.name,c.sort ORDER BY c.sort',
152 1
                $rsm
153 1
            )
154 1
            ->setParameters([
155 1
                'tradePostId' => $tradePostId,
156 1
                'userId' => $userId
157 1
            ])
158 1
            ->getResult();
159
    }
160
161
    #[Override]
162
    public function getOldOffers(int $threshold): array
163
    {
164
        return $this->getEntityManager()
165
            ->createQuery(
166
                sprintf(
167
                    'SELECT to FROM %s to
168
                     WHERE to.date < :maxAge
169
                     AND to.user_id >= :firstUserId
170
                     ORDER BY to.user_id ASC, to.posts_id ASC
171
                    ',
172
                    TradeOffer::class
173
                )
174
            )
175
            ->setParameters([
176
                'maxAge' => time() - $threshold,
177
                'firstUserId' => UserEnum::USER_FIRST_ID
178
            ])
179
            ->getResult();
180
    }
181
}
182