Passed
Push — dev ( 03e2b8...98e531 )
by Janko
15:04
created

SpacecraftSystemRepository   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 3.8%

Importance

Changes 0
Metric Value
eloc 69
c 0
b 0
f 0
dl 0
loc 121
ccs 3
cts 79
cp 0.038
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A isSystemHealthy() 0 17 1
A delete() 0 6 1
A getByShip() 0 6 1
A getWebOwningShipSystem() 0 17 1
A prototype() 0 4 1
A getTrackingShipSystems() 0 17 1
A truncateByShip() 0 12 1
A save() 0 6 1
A getWebConstructingShipSystems() 0 17 1
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\Component\Spacecraft\System\SpacecraftSystemTypeEnum;
10
use Stu\Orm\Entity\SpacecraftInterface;
11
use Stu\Orm\Entity\SpacecraftSystem;
12
use Stu\Orm\Entity\SpacecraftSystemInterface;
13
14
/**
15
 * @extends EntityRepository<SpacecraftSystem>
16
 */
17
final class SpacecraftSystemRepository extends EntityRepository implements SpacecraftSystemRepositoryInterface
18
{
19
    #[Override]
20
    public function prototype(): SpacecraftSystemInterface
21
    {
22
        return new SpacecraftSystem();
23
    }
24
25 1
    #[Override]
26
    public function save(SpacecraftSystemInterface $post): void
27
    {
28 1
        $em = $this->getEntityManager();
29
30 1
        $em->persist($post);
31
    }
32
33
    #[Override]
34
    public function delete(SpacecraftSystemInterface $post): void
35
    {
36
        $em = $this->getEntityManager();
37
38
        $em->remove($post);
39
    }
40
41
    #[Override]
42
    public function getByShip(int $shipId): array
43
    {
44
        return $this->findBy(
45
            ['spacecraft_id' => $shipId],
46
            ['system_type' => 'asc']
47
        );
48
    }
49
50
    #[Override]
51
    public function getTrackingShipSystems(int $targetId): array
52
    {
53
        return $this->getEntityManager()
54
            ->createQuery(
55
                sprintf(
56
                    'SELECT ss FROM %s ss
57
                    WHERE ss.system_type = :systemType
58
                    AND ss.data LIKE :target',
59
                    SpacecraftSystem::class
60
                )
61
            )
62
            ->setParameters([
63
                'systemType' => SpacecraftSystemTypeEnum::TRACKER,
64
                'target' => sprintf('%%"targetId":%d%%', $targetId)
65
            ])
66
            ->getResult();
67
    }
68
69
    #[Override]
70
    public function getWebConstructingShipSystems(int $webId): array
71
    {
72
        return $this->getEntityManager()
73
            ->createQuery(
74
                sprintf(
75
                    'SELECT ss FROM %s ss
76
                    WHERE ss.system_type = :systemType
77
                    AND ss.data LIKE :target',
78
                    SpacecraftSystem::class
79
                )
80
            )
81
            ->setParameters([
82
                'systemType' => SpacecraftSystemTypeEnum::THOLIAN_WEB,
83
                'target' => sprintf('%%"webUnderConstructionId":%d%%', $webId)
84
            ])
85
            ->getResult();
86
    }
87
88
    #[Override]
89
    public function getWebOwningShipSystem(int $webId): ?SpacecraftSystemInterface
90
    {
91
        return $this->getEntityManager()
92
            ->createQuery(
93
                sprintf(
94
                    'SELECT ss FROM %s ss
95
                    WHERE ss.system_type = :systemType
96
                    AND ss.data LIKE :target',
97
                    SpacecraftSystem::class
98
                )
99
            )
100
            ->setParameters([
101
                'systemType' => SpacecraftSystemTypeEnum::THOLIAN_WEB,
102
                'target' => sprintf('%%"ownedWebId":%d%%', $webId)
103
            ])
104
            ->getOneOrNullResult();
105
    }
106
107
    #[Override]
108
    public function isSystemHealthy(SpacecraftInterface $spacecraft, SpacecraftSystemTypeEnum $type): bool
109
    {
110
        return (int)$this->getEntityManager()
111
            ->createQuery(
112
                sprintf(
113
                    'SELECT ss.status FROM %s ss
114
                    WHERE ss.system_type = :systemType
115
                    AND ss.spacecraft = :spacecraft',
116
                    SpacecraftSystem::class
117
                )
118
            )
119
            ->setParameters([
120
                'systemType' => $type->value,
121
                'spacecraft' => $spacecraft
122
            ])
123
            ->getSingleScalarResult() > 0;
124
    }
125
126
    #[Override]
127
    public function truncateByShip(int $shipId): void
128
    {
129
        $this->getEntityManager()
130
            ->createQuery(
131
                sprintf(
132
                    'DELETE FROM %s s WHERE s.spacecraft_id = :shipId',
133
                    SpacecraftSystem::class
134
                )
135
            )
136
            ->setParameter('shipId', $shipId)
137
            ->execute();
138
    }
139
}
140