Passed
Push — master ( 37217b...efdda9 )
by Nico
19:16 queued 09:28
created

StarSystemRepository::getRandomFreeSystemNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 18
ccs 0
cts 13
cp 0
crap 2
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Orm\Repository;
6
7
use Doctrine\ORM\EntityRepository;
8
use Stu\Component\Database\DatabaseCategoryTypeEnum;
9
use Stu\Orm\Entity\DatabaseEntry;
10
use Stu\Orm\Entity\LayerInterface;
11
use Stu\Orm\Entity\Map;
12
use Stu\Orm\Entity\StarSystem;
13
use Stu\Orm\Entity\StarSystemInterface;
14
use Stu\Orm\Entity\StarSystemName;
15
use Stu\Orm\Entity\StarSystemNameInterface;
16
17
/**
18
 * @extends EntityRepository<StarSystem>
19
 */
20
final class StarSystemRepository extends EntityRepository implements StarSystemRepositoryInterface
21
{
22
    public function prototype(): StarSystemInterface
23
    {
24
        return new StarSystem();
25
    }
26
27
    public function save(StarSystemInterface $storage): void
28
    {
29
        $em = $this->getEntityManager();
30
31
        $em->persist($storage);
32
    }
33
34
    public function getByLayer(int $layerId): array
35
    {
36
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...$layerId))->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...
37
            ->createQuery(
38
                sprintf(
39
                    'SELECT s FROM %s s
40
                    JOIN %s m
41
                    WITH m.systems_id = s.id
42
                    WHERE m.layer_id  = :layerId
43
                    ORDER BY s.name ASC',
44
                    StarSystem::class,
45
                    Map::class
46
                )
47
            )
48
            ->setParameters([
49
                'layerId' => $layerId
50
            ])
51
            ->getResult();
52
    }
53
54
    public function getWithoutDatabaseEntry(): array
55
    {
56
        return $this->getEntityManager()
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->getEntityM...ARSYSTEM))->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...
57
            ->createQuery(
58
                sprintf(
59
                    'SELECT t FROM %s t WHERE t.database_id NOT IN (SELECT d.id FROM %s d WHERE d.category_id = :categoryId)',
60
                    StarSystem::class,
61
                    DatabaseEntry::class
62
                )
63
            )
64
            ->setParameters([
65
                'categoryId' => DatabaseCategoryTypeEnum::DATABASE_CATEGORY_STARSYSTEM,
66
            ])
67
            ->getResult();
68
    }
69
70
    public function getNumberOfSystemsToGenerate(LayerInterface $layer): int
71
    {
72
        return (int) $this->getEntityManager()
73
            ->createQuery(
74
                sprintf(
75
                    'SELECT count(m.id) from %s m
76
                    WHERE m.system_type_id IS NOT NULL
77
                    AND m.systems_id IS NULL
78
                    AND m.layer = :layer',
79
                    Map::class
80
                )
81
            )
82
            ->setParameters([
83
                'layer' => $layer
84
            ])
85
            ->getSingleScalarResult();
86
    }
87
88
    public function getRandomFreeSystemNames(int $amount): array
89
    {
90
        $freeNames = $this->getEntityManager()
91
            ->createQuery(
92
                sprintf(
93
                    'SELECT ssm FROM %s ssm
94
                    WHERE NOT EXISTS (SELECT ss.id
95
                                        FROM %s ss
96
                                        WHERE ss.name = ssm.name)',
97
                    StarSystemName::class,
98
                    StarSystem::class
99
                )
100
            )
101
            ->getResult();
102
103
        shuffle($freeNames);
0 ignored issues
show
Bug introduced by
It seems like $freeNames can also be of type integer; however, parameter $array of shuffle() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        shuffle(/** @scrutinizer ignore-type */ $freeNames);
Loading history...
104
105
        return array_slice($freeNames, 0, $amount);
106
    }
107
108
    public function getPreviousStarSystem(StarSystemInterface $current): ?StarSystemInterface
109
    {
110
        return $this->getEntityManager()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getEntityM...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return Stu\Orm\Entity\StarSystemInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
111
            ->createQuery(
112
                sprintf(
113
                    'SELECT ss FROM %s ss
114
                    JOIN %s m
115
                    WITH m.systems_id = ss.id
116
                    WHERE ss.id < :currentId
117
                    AND m.layer = :layer
118
                    ORDER BY ss.id DESC',
119
                    StarSystem::class,
120
                    Map::class
121
                )
122
            )
123
            ->setParameters(['layer' => $current->getLayer(), 'currentId' => $current->getId()])
124
            ->setMaxResults(1)
125
            ->getOneOrNullResult();
126
    }
127
128
    public function getNextStarSystem(StarSystemInterface $current): ?StarSystemInterface
129
    {
130
        return $this->getEntityManager()
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getEntityM...)->getOneOrNullResult() could return the type integer which is incompatible with the type-hinted return Stu\Orm\Entity\StarSystemInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
131
            ->createQuery(
132
                sprintf(
133
                    'SELECT ss FROM %s ss
134
                    JOIN %s m
135
                    WITH m.systems_id = ss.id
136
                    WHERE ss.id > :currentId
137
                    AND m.layer = :layer
138
                    ORDER BY ss.id ASC',
139
                    StarSystem::class,
140
                    Map::class
141
                )
142
            )
143
            ->setParameters(['layer' => $current->getLayer(), 'currentId' => $current->getId()])
144
            ->setMaxResults(1)
145
            ->getOneOrNullResult();
146
    }
147
}
148