Passed
Push — dev ( 79c88c...be1bbd )
by Nico
05:18
created

RefactorRunner::migrateAllianceJobSystem()   C

Complexity

Conditions 12
Paths 66

Size

Total Lines 119
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
cc 12
eloc 76
nc 66
nop 0
dl 0
loc 119
ccs 0
cts 79
cp 0
crap 156
rs 6.0969
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Refactor;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Stu\Component\Alliance\Enum\AllianceJobTypeEnum;
9
use Stu\Orm\Entity\AllianceJob;
10
use Stu\Orm\Entity\AllianceMemberJob;
11
use Stu\Orm\Repository\AllianceJobRepositoryInterface;
12
use Stu\Orm\Repository\AllianceMemberJobRepositoryInterface;
13
use Stu\Orm\Repository\AllianceRepositoryInterface;
14
use Stu\Orm\Repository\AllianceSettingsRepositoryInterface;
15
use Stu\Component\Alliance\AllianceSettingsEnum;
16
17
final class RefactorRunner
18
{
19 1
    public function __construct(
20
        private EntityManagerInterface $entityManager,
21
        private AllianceJobRepositoryInterface $allianceJobRepository,
22
        private AllianceRepositoryInterface $allianceRepository,
23
        private AllianceSettingsRepositoryInterface $allianceSettingsRepository
24 1
    ) {}
25
26
    public function refactor(): void
27
    {
28
        echo "\n=== Alliance Job System Migration Start ===\n\n";
29
        $this->migrateAllianceJobSystem();
30
        echo "\n=== Alliance Job System Migration Complete ===\n";
31
    }
32
33
    private function migrateAllianceJobSystem(): void
34
    {
35
        $connection = $this->entityManager->getConnection();
36
37
        $oldJobs = $connection->fetchAllAssociative(
38
            'SELECT id, alliance_id, user_id, type FROM stu_alliances_jobs WHERE type != :pending ORDER BY alliance_id, type',
39
            ['pending' => AllianceJobTypeEnum::PENDING->value]
40
        );
41
42
        echo "Found " . count($oldJobs) . " existing jobs (excluding pending)\n";
43
44
        $processedAlliances = [];
45
        $jobMapping = [];
46
47
        foreach ($oldJobs as $oldJob) {
48
            $allianceId = (int) $oldJob['alliance_id'];
49
            $userId = (int) $oldJob['user_id'];
50
            $type = (int) $oldJob['type'];
51
52
            if (!isset($processedAlliances[$allianceId])) {
53
                $processedAlliances[$allianceId] = [];
54
            }
55
56
            if (!isset($processedAlliances[$allianceId][$type])) {
57
                $title = $this->getTitleForType($allianceId, $type);
58
59
                $newJob = new AllianceJob();
60
                $alliance = $this->allianceRepository->find($allianceId);
61
                if ($alliance === null) {
62
                    continue;
63
                }
64
65
                $newJob->setAlliance($alliance);
66
                $newJob->setTitle($title);
67
                $newJob->setSort($this->getSortForType($type));
68
                $newJob->setIsFounderPermission($type === AllianceJobTypeEnum::FOUNDER->value);
69
                $newJob->setIsSuccessorPermission($type === AllianceJobTypeEnum::SUCCESSOR->value);
70
                $newJob->setIsDiplomaticPermission($type === AllianceJobTypeEnum::DIPLOMATIC->value);
71
72
                $this->entityManager->persist($newJob);
73
                $this->entityManager->flush();
74
75
                $processedAlliances[$allianceId][$type] = $newJob->getId();
76
                $jobMapping[$oldJob['id']] = $newJob->getId();
77
78
                echo "  Created new job '{$title}' (ID: {$newJob->getId()}) for Alliance {$allianceId}\n";
79
            }
80
81
            $jobId = $processedAlliances[$allianceId][$type];
82
            $newJob = $this->allianceJobRepository->find($jobId);
83
            $user = $this->entityManager->getReference('Stu\\Orm\\Entity\\User', $userId);
84
85
            if ($newJob !== null && $user !== null) {
86
                $memberJob = new AllianceMemberJob();
87
                $memberJob->setJob($newJob);
88
                $memberJob->setUser($user);
89
90
                $this->entityManager->persist($memberJob);
91
            }
92
        }
93
94
        $this->entityManager->flush();
95
96
        echo "\nProcessing pending applications...\n";
97
98
        $pendingApplications = $connection->fetchAllAssociative(
99
            'SELECT id, alliance_id, user_id FROM stu_alliances_jobs WHERE type = :pending',
100
            ['pending' => AllianceJobTypeEnum::PENDING->value]
101
        );
102
103
        echo "Found " . count($pendingApplications) . " pending applications\n";
104
105
        foreach ($pendingApplications as $pending) {
106
            $allianceId = (int) $pending['alliance_id'];
107
            $userId = (int) $pending['user_id'];
108
109
            $alliance = $this->allianceRepository->find($allianceId);
110
            $user = $this->entityManager->getReference('Stu\\Orm\\Entity\\User', $userId);
111
112
            if ($alliance === null || $user === null) {
113
                continue;
114
            }
115
116
            if (!isset($processedAlliances[$allianceId]['pending'])) {
117
                $pendingJob = new AllianceJob();
118
                $pendingJob->setAlliance($alliance);
119
                $pendingJob->setTitle('Bewerber');
120
                $pendingJob->setSort(999);
121
                $pendingJob->setIsFounderPermission(false);
122
                $pendingJob->setIsSuccessorPermission(false);
123
                $pendingJob->setIsDiplomaticPermission(false);
124
125
                $this->entityManager->persist($pendingJob);
126
                $this->entityManager->flush();
127
128
                $processedAlliances[$allianceId]['pending'] = $pendingJob->getId();
129
130
                echo "  Created 'Bewerber' job (ID: {$pendingJob->getId()}) for Alliance {$allianceId}\n";
131
            }
132
133
            $jobId = $processedAlliances[$allianceId]['pending'];
134
            $pendingJob = $this->allianceJobRepository->find($jobId);
135
136
            if ($pendingJob !== null) {
137
                $memberJob = new AllianceMemberJob();
138
                $memberJob->setJob($pendingJob);
139
                $memberJob->setUser($user);
140
141
                $this->entityManager->persist($memberJob);
142
            }
143
        }
144
145
        $this->entityManager->flush();
146
147
        $totalJobs = count(array_unique(array_merge(...array_values($processedAlliances))));
148
        echo "\nSummary:\n";
149
        echo "  - Alliances processed: " . count($processedAlliances) . "\n";
150
        echo "  - Jobs created: {$totalJobs}\n";
151
        echo "  - Member assignments created\n";
152
    }
153
154
    private function getSortForType(int $type): int
155
    {
156
        return match ($type) {
157
            AllianceJobTypeEnum::FOUNDER->value => 1,
158
            AllianceJobTypeEnum::SUCCESSOR->value => 2,
159
            AllianceJobTypeEnum::DIPLOMATIC->value => 3,
160
            default => 999,
161
        };
162
    }
163
164
    private function getTitleForType(int $allianceId, int $type): string
165
    {
166
        $settingEnum = match ($type) {
167
            AllianceJobTypeEnum::FOUNDER->value => AllianceSettingsEnum::ALLIANCE_FOUNDER_DESCRIPTION,
168
            AllianceJobTypeEnum::SUCCESSOR->value => AllianceSettingsEnum::ALLIANCE_SUCCESSOR_DESCRIPTION,
169
            AllianceJobTypeEnum::DIPLOMATIC->value => AllianceSettingsEnum::ALLIANCE_DIPLOMATIC_DESCRIPTION,
170
            default => null,
171
        };
172
173
        if ($settingEnum === null) {
174
            return match ($type) {
175
                AllianceJobTypeEnum::FOUNDER->value => 'Präsident',
176
                AllianceJobTypeEnum::SUCCESSOR->value => 'Vize-Präsident',
177
                AllianceJobTypeEnum::DIPLOMATIC->value => 'Außenminister',
178
                default => 'Mitglied',
179
            };
180
        }
181
182
        $settings = $this->allianceSettingsRepository->findBy([
183
            'alliance' => $allianceId,
184
            'setting' => $settingEnum,
185
        ]);
186
187
        if (count($settings) > 0) {
188
            return $settings[0]->getValue();
189
        }
190
191
        return match ($type) {
192
            AllianceJobTypeEnum::FOUNDER->value => 'Präsident',
193
            AllianceJobTypeEnum::SUCCESSOR->value => 'Vize-Präsident',
194
            AllianceJobTypeEnum::DIPLOMATIC->value => 'Außenminister',
195
            default => 'Mitglied',
196
        };
197
    }
198
}
199