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

SpacecraftSystemRepository::isSystemHealthy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 17
ccs 0
cts 14
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 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