Passed
Push — dev ( 45bf8d...5b8f46 )
by Janko
10:07
created

AnomalyRepository::findAllActive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
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 0
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 2
rs 9.9666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AnomalyRepository::getByLocationAndType() 0 6 1
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\Anomaly\Type\AnomalyTypeEnum;
11
use Stu\Orm\Entity\Anomaly;
12
use Stu\Orm\Entity\AnomalyInterface;
13
use Stu\Orm\Entity\Location;
14
use Stu\Orm\Entity\LocationInterface;
15
use Stu\Orm\Entity\Map;
16
use Stu\Orm\Entity\SpacecraftInterface;
17
18
/**
19
 * @extends EntityRepository<Anomaly>
20
 */
21
final class AnomalyRepository extends EntityRepository implements AnomalyRepositoryInterface
22
{
23
    #[Override]
24
    public function prototype(): AnomalyInterface
25
    {
26
        return new Anomaly();
27
    }
28
29
    #[Override]
30
    public function save(AnomalyInterface $anomaly): void
31
    {
32
        $em = $this->getEntityManager();
33
        $em->persist($anomaly);
34
    }
35
36
    #[Override]
37
    public function delete(AnomalyInterface $anomaly): void
38
    {
39
        $location = $anomaly->getLocation();
40
        if ($location !== null) {
41
            $location->getAnomalies()->removeElement($anomaly);
42
        }
43
44
        $parent = $anomaly->getParent();
45
        if ($parent !== null) {
46
            $parent->getChildren()->removeElement($anomaly);
47
        }
48
49
        $this->getEntityManager()->remove($anomaly);
50
    }
51
52
    #[Override]
53
    public function getByLocationAndType(LocationInterface $location, AnomalyTypeEnum $type): ?AnomalyInterface
54
    {
55
        return $this->findOneBy([
56
            'location_id' => $location->getId(),
57
            'anomaly_type_id' => $type->value
58
        ]);
59
    }
60
61
    /**
62
     * @return array<AnomalyInterface>
63
     */
64
    #[Override]
65
    public function findAllRoot(): array
66
    {
67
        return $this->getEntityManager()
68
            ->createQuery(
69
                sprintf(
70
                    'SELECT a FROM %s a
71
                    WHERE a.parent_id IS NULL',
72
                    Anomaly::class
73
                )
74
            )
75
            ->getResult();
76
    }
77
78
    #[Override]
79
    public function getActiveCountByTypeWithoutParent(AnomalyTypeEnum $type): int
80
    {
81
        return (int) $this->getEntityManager()
82
            ->createQuery(
83
                sprintf(
84
                    'SELECT count(a.id) FROM %s a
85
                    WHERE a.anomaly_type_id = :type
86
                    AND a.remaining_ticks > 0
87
                    AND a.parent_id IS NULL',
88
                    Anomaly::class
89
                )
90
            )
91
            ->setParameter('type', $type->value)
92
            ->getSingleScalarResult();
93
    }
94
95
    #[Override]
96
    public function getClosestAnomalyDistance(SpacecraftInterface $spacecraft): ?int
97
    {
98
        $map = $spacecraft->getMap();
99
        if ($map === null) {
100
            return null;
101
        }
102
103
        $range = $spacecraft->getSensorRange() * 2;
104
105
        try {
106
            $result = (int)$this->getEntityManager()->createQuery(
107
                sprintf(
108
                    'SELECT SQRT(ABS(l.cx - :x) * ABS(l.cx - :x) + ABS(l.cy - :y) * ABS(l.cy - :y)) as foo
109
                    FROM %s a
110
                    JOIN %s m
111
                    WITH a.location_id = m.id
112
                    JOIN %s l
113
                    WITH m.id = l.id
114
                    WHERE a.remaining_ticks > 0
115
                    AND (l.cx BETWEEN :startX AND :endX)
116
                    AND (l.cy BETWEEN :startY AND :endY)
117
                    AND l.layer = :layer
118
                    ORDER BY foo ASC',
119
                    Anomaly::class,
120
                    Map::class,
121
                    Location::class
122
                )
123
            )
124
                ->setMaxResults(1)
125
                ->setParameters([
126
                    'layer' => $map->getLayer(),
127
                    'x' => $map->getX(),
128
                    'y' => $map->getY(),
129
                    'startX' => $map->getX() - $range,
130
                    'endX' => $map->getX() +  $range,
131
                    'startY' => $map->getY() - $range,
132
                    'endY' => $map->getY() +  $range
133
                ])
134
                ->getSingleScalarResult();
135
136
            if ($result > $range) {
137
                return null;
138
            }
139
140
            return $result;
141
        } catch (NoResultException) {
142
            return null;
143
        }
144
    }
145
}
146