Passed
Pull Request — master (#1904)
by Nico
39:29 queued 12:01
created

ColonyShipQueueRepository::getByColonyAndMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 4
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\Orm\Entity\ColonyInterface;
10
use Stu\Orm\Entity\ColonyShipQueue;
11
use Stu\Orm\Entity\ColonyShipQueueInterface;
12
13
/**
14
 * @extends EntityRepository<ColonyShipQueue>
15
 */
16
final class ColonyShipQueueRepository extends EntityRepository implements ColonyShipQueueRepositoryInterface
17
{
18
    #[Override]
19
    public function prototype(): ColonyShipQueueInterface
20
    {
21
        return new ColonyShipQueue();
22
    }
23
24
    #[Override]
25
    public function save(ColonyShipQueueInterface $post): void
26
    {
27
        $em = $this->getEntityManager();
28
29
        $em->persist($post);
30
    }
31
32
    #[Override]
33
    public function delete(ColonyShipQueueInterface $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();
44
    }
45
46
    #[Override]
47
    public function stopQueueByColonyAndBuildingFunction(int $colonyId, int $buildingFunctionId): 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' => $buildingFunctionId
60
            ])
61
            ->execute();
62
    }
63
64
    #[Override]
65
    public function restartQueueByColonyAndBuildingFunction(int $colonyId, int $buildingFunctionId): 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' => $buildingFunctionId
83
            ])
84
            ->execute();
85
    }
86
87
    #[Override]
88
    public function getAmountByColonyAndBuildingFunctionAndMode(int $colonyId, int $buildingFunctionId, int $mode): int
89
    {
90
        return $this->count([
91
            'colony_id' => $colonyId,
92
            'building_function_id' => $buildingFunctionId,
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
    #[Override]
106
    public function getByColonyAndMode(int $colonyId, int $mode): array
107
    {
108
        return $this->findBy([
109
            'colony_id' => $colonyId,
110
            'mode' => $mode
111
        ]);
112
    }
113
114
    #[Override]
115
    public function getByUser(int $userId): array
116
    {
117
        return $this->findBy([
118
            'user_id' => $userId
119
        ]);
120
    }
121
122
    #[Override]
123
    public function getByUserAndMode(int $userId, int $mode): array
124
    {
125
        return $this->findBy([
126
            'user_id' => $userId,
127
            'mode' => $mode
128
        ]);
129
    }
130
131
    #[Override]
132
    public function getCountByBuildplan(int $buildplanId): int
133
    {
134
        return $this->count([
135
            'buildplan_id' => $buildplanId
136
        ]);
137
    }
138
139
    #[Override]
140
    public function getFinishedJobs(): array
141
    {
142
        return $this->getEntityManager()
143
            ->createQuery(
144
                sprintf(
145
                    'SELECT sq FROM %s sq WHERE sq.stop_date = :stopDate AND sq.finish_date <= :time',
146
                    ColonyShipQueue::class
147
                )
148
            )
149
            ->setParameters([
150
                'stopDate' => 0,
151
                'time' => time()
152
            ])
153
            ->getResult();
154
    }
155
156
    #[Override]
157
    public function truncateByColony(ColonyInterface $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(ColonyInterface $colony, int $buildingFunctionId): 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' => $buildingFunctionId
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
}