Passed
Push — dev ( 3da2bc...c2ee06 )
by Nico
05:18
created

ColonyShipQueueRepository::getByUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
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\Building\BuildingFunctionEnum;
10
use Stu\Orm\Entity\Colony;
11
use Stu\Orm\Entity\ColonyShipQueue;
12
13
/**
14
 * @extends EntityRepository<ColonyShipQueue>
15
 */
16
final class ColonyShipQueueRepository extends EntityRepository implements ColonyShipQueueRepositoryInterface
17
{
18
    #[Override]
19
    public function prototype(): ColonyShipQueue
20
    {
21
        return new ColonyShipQueue();
22
    }
23
24
    #[Override]
25
    public function save(ColonyShipQueue $post): void
26
    {
27
        $em = $this->getEntityManager();
28
29
        $em->persist($post);
30
    }
31
32
    #[Override]
33
    public function delete(ColonyShipQueue $queue): void
34
    {
35
        $em = $this->getEntityManager();
36
37
        $ship = $queue->getShip();
38
        if ($ship !== null) {
39
            $ship->setColonyShipQueue(null);
40
        }
41
42
        $em->remove($queue);
43
        $em->flush(); //TODO really neccessary?
44
    }
45
46
    #[Override]
47
    public function stopQueueByColonyAndBuildingFunction(int $colonyId, BuildingFunctionEnum $buildingFunction): void
48
    {
49
        $this->getEntityManager()
50
            ->createQuery(
51
                sprintf(
52
                    'UPDATE %s sq SET sq.stop_date = :time WHERE sq.colony_id = :colonyId AND sq.building_function_id = :buildingFunctionId',
53
                    ColonyShipQueue::class
54
                )
55
            )
56
            ->setParameters([
57
                'time' => time(),
58
                'colonyId' => $colonyId,
59
                'buildingFunctionId' => $buildingFunction->value
60
            ])
61
            ->execute();
62
    }
63
64
    #[Override]
65
    public function restartQueueByColonyAndBuildingFunction(int $colonyId, BuildingFunctionEnum $buildingFunction): void
66
    {
67
        $this->getEntityManager()
68
            ->createQuery(
69
                sprintf(
70
                    'UPDATE %s sq
71
                    SET sq.finish_date = (:time - sq.stop_date + sq.finish_date), sq.stop_date = :stopDate
72
                    WHERE sq.stop_date != 0
73
                    AND sq.colony_id = :colonyId
74
                    AND sq.building_function_id = :buildingFunctionId',
75
                    ColonyShipQueue::class
76
                )
77
            )
78
            ->setParameters([
79
                'stopDate' => 0,
80
                'time' => time(),
81
                'colonyId' => $colonyId,
82
                'buildingFunctionId' => $buildingFunction->value
83
            ])
84
            ->execute();
85
    }
86
87
    #[Override]
88
    public function getAmountByColonyAndBuildingFunctionAndMode(int $colonyId, BuildingFunctionEnum $buildingFunction, int $mode): int
89
    {
90
        return $this->count([
91
            'colony_id' => $colonyId,
92
            'building_function_id' => $buildingFunction->value,
93
            'mode' => $mode
94
        ]);
95
    }
96
97
    #[Override]
98
    public function getByColony(int $colonyId): array
99
    {
100
        return $this->findBy([
101
            'colony_id' => $colonyId
102
        ]);
103
    }
104
105 1
    #[Override]
106
    public function getByColonyAndMode(int $colonyId, int $mode): array
107
    {
108 1
        return $this->findBy([
109 1
            'colony_id' => $colonyId,
110 1
            'mode' => $mode
111 1
        ]);
112
    }
113
114
    #[Override]
115
    public function getByUser(int $userId): array
116
    {
117
        return $this->findBy([
118
            'user_id' => $userId
119
        ]);
120
    }
121
122 2
    #[Override]
123
    public function getByUserAndMode(int $userId, int $mode): array
124
    {
125 2
        return $this->findBy([
126 2
            'user_id' => $userId,
127 2
            'mode' => $mode
128 2
        ]);
129
    }
130
131 1
    #[Override]
132
    public function getCountByBuildplan(int $buildplanId): int
133
    {
134 1
        return $this->count([
135 1
            'buildplan_id' => $buildplanId
136 1
        ]);
137
    }
138
139 1
    #[Override]
140
    public function getFinishedJobs(): array
141
    {
142 1
        return $this->getEntityManager()
143 1
            ->createQuery(
144 1
                sprintf(
145 1
                    'SELECT sq FROM %s sq WHERE sq.stop_date = :stopDate AND sq.finish_date <= :time',
146 1
                    ColonyShipQueue::class
147 1
                )
148 1
            )
149 1
            ->setParameters([
150 1
                'stopDate' => 0,
151 1
                'time' => time()
152 1
            ])
153 1
            ->getResult();
154
    }
155
156
    #[Override]
157
    public function truncateByColony(Colony $colony): void
158
    {
159
        $this->getEntityManager()
160
            ->createQuery(
161
                sprintf(
162
                    'DELETE FROM %s sq WHERE sq.colony_id = :colony',
163
                    ColonyShipQueue::class
164
                )
165
            )
166
            ->setParameters([
167
                'colony' => $colony
168
            ])
169
            ->execute();
170
    }
171
172
    #[Override]
173
    public function truncateByColonyAndBuildingFunction(Colony $colony, BuildingFunctionEnum $buildingFunction): void
174
    {
175
        $this->getEntityManager()
176
            ->createQuery(
177
                sprintf(
178
                    'DELETE FROM %s sq WHERE sq.colony = :colony AND sq.building_function_id = :buildingFunctionId',
179
                    ColonyShipQueue::class
180
                )
181
            )
182
            ->setParameters([
183
                'colony' => $colony,
184
                'buildingFunctionId' => $buildingFunction->value
185
            ])
186
            ->execute();
187
    }
188
189
    #[Override]
190
    public function truncateByShip(int $shipId): void
191
    {
192
        $this->getEntityManager()
193
            ->createQuery(
194
                sprintf(
195
                    'DELETE FROM %s sq WHERE sq.ship_id = :shipId',
196
                    ColonyShipQueue::class
197
                )
198
            )
199
            ->setParameters([
200
                'shipId' => $shipId
201
            ])
202
            ->execute();
203
    }
204
205
    #[Override]
206
    public function getByShip(int $shipId): ?ColonyShipQueue
207
    {
208
        return $this->findOneBy([
209
            'ship_id' => $shipId
210
        ]);
211
    }
212
}