Passed
Pull Request — master (#2123)
by Nico
26:37 queued 15:41
created

DatabaseEntryRepository::getRegionEntriesByLayer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 2
dl 0
loc 28
ccs 0
cts 17
cp 0
crap 6
rs 9.584
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\DatabaseEntry;
10
use Stu\Orm\Entity\DatabaseEntryInterface;
11
use Stu\Orm\Entity\StarSystem;
12
use Stu\Orm\Entity\Map;
13
use Stu\Orm\Entity\MapRegion;
14
use Stu\Orm\Entity\Location;
15
use Stu\Orm\Entity\Spacecraft;
16
17
/**
18
 * @extends EntityRepository<DatabaseEntry>
19
 */
20
final class DatabaseEntryRepository extends EntityRepository implements DatabaseEntryRepositoryInterface
21
{
22
    #[Override]
23
    public function getByCategoryId(int $categoryId): array
24
    {
25
        return $this->findBy([
26
            'category_id' => $categoryId
27
        ]);
28
    }
29
30
    #[Override]
31
    public function getByCategoryIdAndObjectId(int $categoryId, int $objectId): ?DatabaseEntryInterface
32
    {
33
        return $this->findOneBy([
34
            'category_id' => $categoryId,
35
            'object_id' => $objectId
36
        ]);
37
    }
38
39
    #[Override]
40
    public function getStarSystemEntriesByLayer(int $categoryId, ?int $layer = null): array
41
    {
42
        $query = $this->getEntityManager()
43
            ->createQuery(
44
                sprintf(
45
                    'SELECT de FROM %s de WHERE de.category_id = :categoryId AND de.id IN (
46
                        SELECT ss.database_id FROM %s ss WHERE ss.id IN (
47
                            SELECT m.systems_id FROM %s m WHERE m.id IN (
48
                                SELECT l.id FROM %s l WHERE l.layer_id = :layerId
49
                            )
50
                        )
51
                    )',
52
                    DatabaseEntry::class,
53
                    StarSystem::class,
54
                    Map::class,
55
                    Location::class
56
                )
57
            )
58
            ->setParameter('categoryId', $categoryId);
59
60
        if ($layer !== null) {
61
            $query->setParameter('layerId', $layer);
62
        } else {
63
            return $this->findBy(['category_id' => $categoryId]);
64
        }
65
66
        return $query->getResult();
67
    }
68
69
    #[Override]
70
    public function getRegionEntriesByLayer(int $categoryId, ?int $layer = null): array
71
    {
72
        $query = $this->getEntityManager()
73
            ->createQuery(
74
                sprintf(
75
                    'SELECT de FROM %s de WHERE de.category_id = :categoryId AND de.id IN (
76
                        SELECT mr.database_id FROM %s mr WHERE mr.id IN (
77
                            SELECT m.region_id FROM %s m WHERE m.id IN (
78
                                SELECT l.id FROM %s l WHERE l.layer_id = :layerId
79
                            )
80
                        )
81
                    )',
82
                    DatabaseEntry::class,
83
                    MapRegion::class,
84
                    Map::class,
85
                    Location::class
86
                )
87
            )
88
            ->setParameter('categoryId', $categoryId);
89
90
        if ($layer !== null) {
91
            $query->setParameter('layerId', $layer);
92
        } else {
93
            return $this->findBy(['category_id' => $categoryId]);
94
        }
95
96
        return $query->getResult();
97
    }
98
99
    #[Override]
100
    public function getTradePostEntriesByLayer(int $categoryId, ?int $layer = null): array
101
    {
102
        $query = $this->getEntityManager()
103
            ->createQuery(
104
                sprintf(
105
                    'SELECT de FROM %s de WHERE de.category_id = :categoryId AND de.object_id IN (
106
                        SELECT s.id FROM %s s WHERE s.location_id IN (
107
                                SELECT l.id FROM %s l WHERE l.layer_id = :layerId
108
                            
109
                        )
110
                    )',
111
                    DatabaseEntry::class,
112
                    Spacecraft::class,
113
                    Location::class
114
                )
115
            )
116
            ->setParameter('categoryId', $categoryId);
117
118
        if ($layer !== null) {
119
            $query->setParameter('layerId', $layer);
120
        } else {
121
            return $this->findBy(['category_id' => $categoryId]);
122
        }
123
124
        return $query->getResult();
125
    }
126
127
    #[Override]
128
    public function prototype(): DatabaseEntryInterface
129
    {
130
        return new DatabaseEntry();
131
    }
132
133
    #[Override]
134
    public function save(DatabaseEntryInterface $entry): void
135
    {
136
        $em = $this->getEntityManager();
137
138
        $em->persist($entry);
139
    }
140
}
141