Passed
Pull Request — master (#1710)
by Nico
24:28 queued 03:12
created

AnomalyRepository::getClosestAnomalyDistance()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 29
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 37
ccs 0
cts 26
cp 0
crap 12
rs 9.456
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 Stu\Orm\Entity\Anomaly;
10
use Stu\Orm\Entity\AnomalyInterface;
11
use Stu\Orm\Entity\Map;
12
use Stu\Orm\Entity\ShipInterface;
13
14
/**
15
 * @extends EntityRepository<Anomaly>
16
 */
17
final class AnomalyRepository extends EntityRepository implements AnomalyRepositoryInterface
18
{
19
    public function prototype(): AnomalyInterface
20
    {
21
        return new Anomaly();
22
    }
23
24
    public function save(AnomalyInterface $anomaly): void
25
    {
26
        $em = $this->getEntityManager();
27
28
        $em->persist($anomaly);
29
    }
30
31
    public function delete(AnomalyInterface $anomaly): void
32
    {
33
        $em = $this->getEntityManager();
34
35
        $em->remove($anomaly);
36
    }
37
38
    /**
39
     * @return array<AnomalyInterface>
40
     */
41
    public function findAllActive(): array
42
    {
43
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...y::class))->getResult() could return the type integer which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
44
            ->createQuery(
45
                sprintf(
46
                    'SELECT a
47
                        FROM %s a
48
                        WHERE a.remaining_ticks > 0',
49
                    Anomaly::class
50
                )
51
            )
52
            ->getResult();
53
    }
54
55
    public function getClosestAnomalyDistance(ShipInterface $ship): ?int
56
    {
57
        $map = $ship->getMap();
58
        if ($map === null) {
59
            return null;
60
        }
61
62
        $range = $ship->getSensorRange() * 2;
63
64
        try {
65
            return (int)$this->getEntityManager()->createQuery(
66
                sprintf(
67
                    'SELECT SQRT(ABS(m.cx - :x) * ABS(m.cx - :x) + ABS(m.cy - :y) * ABS(m.cy - :y)) as foo
68
                    FROM %s a
69
                    JOIN %s m
70
                    WITH a.map_id = m.id
71
                    WHERE (m.cx BETWEEN :startX AND :endX)
72
                    AND (m.cy BETWEEN :startY AND :endY)
73
                    AND m.layer = :layer
74
                    ORDER BY foo ASC',
75
                    Anomaly::class,
76
                    Map::class
77
                )
78
            )
79
                ->setMaxResults(1)
80
                ->setParameters([
81
                    'layer' => $map->getLayer(),
82
                    'x' => $map->getX(),
83
                    'y' => $map->getY(),
84
                    'startX' => $map->getX() - $range,
85
                    'endX' => $map->getX() +  $range,
86
                    'startY' => $map->getY() - $range,
87
                    'endY' => $map->getY() +  $range
88
                ])
89
                ->getSingleScalarResult();
90
        } catch (NoResultException) {
91
            return null;
92
        }
93
    }
94
}
95