Passed
Pull Request — master (#1849)
by Nico
55:37 queued 21:01
created

RefactorRunner::obtainMeasurementFieldsForSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Refactor;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Stu\Lib\ModuleRumpWrapper\ModuleRumpWrapperProjectileWeapon;
9
use Stu\Orm\Entity\AstronomicalEntryInterface;
10
use Stu\Orm\Entity\LocationInterface;
11
use Stu\Orm\Entity\MapInterface;
12
use Stu\Orm\Entity\MapRegionInterface;
13
use Stu\Orm\Entity\StarSystemInterface;
14
use Stu\Orm\Entity\StarSystemMapInterface;
15
use Stu\Orm\Repository\AstroEntryRepositoryInterface;
16
use Stu\Orm\Repository\LocationRepositoryInterface;
17
use Stu\Orm\Repository\MapRepositoryInterface;
18
use Stu\Orm\Repository\StarSystemMapRepositoryInterface;
19
20
final class RefactorRunner
21
{
22
    public function __construct(
23
        private AstroEntryRepositoryInterface $astroEntryRepository,
24
        private LocationRepositoryInterface $locationRepository,
25
        private MapRepositoryInterface $mapRepository,
26
        private StarSystemMapRepositoryInterface $starSystemMapRepository,
27
    ) {
28
    }
29
30
    public function refactor(): void
31
    {
32
        foreach ($this->astroEntryRepository->findAll() as $astroEntry) {
33
34
            if ($astroEntry->getFieldIds() === '') {
35
                continue;
36
            }
37
38
            $fieldIds = new ArrayCollection(unserialize($astroEntry->getFieldIds()));
39
            if ($fieldIds->isEmpty()) {
40
                continue;
41
            }
42
43
            $locations = $fieldIds->map(fn (int $id) => $this->locationRepository->find($id));
44
45
            $lost = false;
46
            $isMap = false;
47
            $isSystemMap = false;
48
49
            /** @var LocationInterface|null $location */
50
            foreach ($locations as $location) {
51
                if ($location === null) {
52
                    $lost = true;
53
                    break;
54
                }
55
56
                if (
57
                    $location instanceof MapInterface
58
                    && $location->getMapRegion() !== $astroEntry->getRegion()
59
                ) {
60
                    $lost = true;
61
                    break;
62
                }
63
64
                if (
65
                    $location instanceof StarSystemMapInterface
66
                    && $location->getSystem() !== $astroEntry->getSystem()
67
                ) {
68
                    $lost = true;
69
                    break;
70
                }
71
72
                $isMap = $isMap || $location->isMap();
73
                $isSystemMap = $isSystemMap || !$location->isMap();
74
            }
75
76
            if ($lost || ($isMap && $isSystemMap)) {
77
                $this->obtainMeasurementFields(
78
                    $astroEntry->getSystem(),
79
                    $astroEntry->getRegion(),
80
                    $astroEntry
81
                );
82
            }
83
        }
84
    }
85
86
    private function obtainMeasurementFields(
87
        ?StarSystemInterface $system,
88
        ?MapRegionInterface $mapRegion,
89
        AstronomicalEntryInterface $entry
90
    ): void {
91
        if ($system !== null) {
92
            $this->obtainMeasurementFieldsForSystem($system, $entry);
93
        }
94
        if ($mapRegion !== null) {
95
            $this->obtainMeasurementFieldsForRegion($mapRegion, $entry);
96
        }
97
98
        $this->astroEntryRepository->save($entry);
99
    }
100
101
    private function obtainMeasurementFieldsForSystem(StarSystemInterface $system, AstronomicalEntryInterface $entry): void
102
    {
103
        $idArray = $this->starSystemMapRepository->getRandomSystemMapIdsForAstroMeasurement($system->getId());
104
105
        $entry->setFieldIds(serialize($idArray));
106
    }
107
108
    private function obtainMeasurementFieldsForRegion(MapRegionInterface $mapRegion, AstronomicalEntryInterface $entry): void
109
    {
110
        $mapIds = $this->mapRepository->getRandomMapIdsForAstroMeasurement($mapRegion->getId(), 25);
111
112
        $entry->setFieldIds(serialize($mapIds));
113
    }
114
}
115