Passed
Push — dev ( 837c0e...fdbfdd )
by Nico
26:17
created

ColonyShipQueueRepository::truncateByShip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 14
ccs 0
cts 12
cp 0
crap 2
rs 9.9666
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 getAmountByColonyAndBuildingFunction(int $colonyId, int $buildingFunctionId): int
89
    {
90
        return $this->count([
91
            'colony_id' => $colonyId,
92
            'building_function_id' => $buildingFunctionId
93
        ]);
94
    }
95
96
    #[Override]
97
    public function getByColony(int $colonyId): array
98
    {
99
        return $this->findBy([
100
            'colony_id' => $colonyId
101
        ]);
102
    }
103
104
    #[Override]
105
    public function getByUser(int $userId): array
106
    {
107
        return $this->findBy([
108
            'user_id' => $userId
109
        ]);
110
    }
111
112
    #[Override]
113
    public function getCountByBuildplan(int $buildplanId): int
114
    {
115
        return $this->count([
116
            'buildplan_id' => $buildplanId
117
        ]);
118
    }
119
120
    #[Override]
121
    public function getFinishedJobs(): array
122
    {
123
        return $this->getEntityManager()
124
            ->createQuery(
125
                sprintf(
126
                    'SELECT sq FROM %s sq WHERE sq.stop_date = :stopDate AND sq.finish_date <= :time',
127
                    ColonyShipQueue::class
128
                )
129
            )
130
            ->setParameters([
131
                'stopDate' => 0,
132
                'time' => time()
133
            ])
134
            ->getResult();
135
    }
136
137
    #[Override]
138
    public function truncateByColony(ColonyInterface $colony): void
139
    {
140
        $this->getEntityManager()
141
            ->createQuery(
142
                sprintf(
143
                    'DELETE FROM %s sq WHERE sq.colony_id = :colony',
144
                    ColonyShipQueue::class
145
                )
146
            )
147
            ->setParameters([
148
                'colony' => $colony
149
            ])
150
            ->execute();
151
    }
152
153
    #[Override]
154
    public function truncateByColonyAndBuildingFunction(ColonyInterface $colony, int $buildingFunctionId): void
155
    {
156
        $this->getEntityManager()
157
            ->createQuery(
158
                sprintf(
159
                    'DELETE FROM %s sq WHERE sq.colony = :colony AND sq.building_function_id = :buildingFunctionId',
160
                    ColonyShipQueue::class
161
                )
162
            )
163
            ->setParameters([
164
                'colony' => $colony,
165
                'buildingFunctionId' => $buildingFunctionId
166
            ])
167
            ->execute();
168
    }
169
170
    #[Override]
171
    public function truncateByShip(int $shipId): void
172
    {
173
        $this->getEntityManager()
174
            ->createQuery(
175
                sprintf(
176
                    'DELETE FROM %s sq WHERE sq.ship_id = :shipId',
177
                    ColonyShipQueue::class
178
                )
179
            )
180
            ->setParameters([
181
                'shipId' => $shipId
182
            ])
183
            ->execute();
184
    }
185
}