|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stu\Component\Player\Deletion\Handler; |
|
6
|
|
|
|
|
7
|
|
|
use Override; |
|
|
|
|
|
|
8
|
|
|
use Doctrine\Common\Collections\Collection; |
|
9
|
|
|
use Stu\Module\Alliance\Lib\AllianceActionManagerInterface; |
|
10
|
|
|
use Stu\Module\Alliance\Lib\AllianceJobManagerInterface; |
|
11
|
|
|
use Stu\Orm\Entity\AllianceJob; |
|
12
|
|
|
use Stu\Orm\Entity\User; |
|
13
|
|
|
use Stu\Orm\Repository\AllianceMemberJobRepositoryInterface; |
|
14
|
|
|
use Stu\Orm\Repository\UserRepositoryInterface; |
|
15
|
|
|
|
|
16
|
|
|
final class AllianceDeletionHandler implements PlayerDeletionHandlerInterface |
|
17
|
|
|
{ |
|
18
|
4 |
|
public function __construct( |
|
19
|
|
|
private AllianceActionManagerInterface $allianceActionManager, |
|
20
|
|
|
private UserRepositoryInterface $userRepository, |
|
21
|
|
|
private AllianceJobManagerInterface $allianceJobManager |
|
22
|
4 |
|
) {} |
|
23
|
|
|
|
|
24
|
3 |
|
#[Override] |
|
25
|
|
|
public function delete(User $user): void |
|
26
|
|
|
{ |
|
27
|
3 |
|
$alliance = $user->getAlliance(); |
|
28
|
|
|
|
|
29
|
3 |
|
if ($alliance === null) { |
|
30
|
|
|
return; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
3 |
|
$isFounder = $this->allianceJobManager->hasUserFounderPermission($user, $alliance); |
|
34
|
|
|
|
|
35
|
3 |
|
if ($isFounder) { |
|
36
|
2 |
|
$members = $alliance->getMembers(); |
|
37
|
2 |
|
$members->removeElement($user); |
|
38
|
2 |
|
$user->setAlliance(null); |
|
39
|
2 |
|
$this->userRepository->save($user); |
|
40
|
|
|
|
|
41
|
2 |
|
$newFounder = $this->findNewFounder($alliance, $members); |
|
42
|
|
|
|
|
43
|
2 |
|
if ($newFounder === null) { |
|
44
|
1 |
|
$this->allianceJobManager->removeUserFromAllJobs($user, $alliance); |
|
45
|
1 |
|
$this->allianceActionManager->delete($alliance); |
|
46
|
1 |
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
$founderJob = $alliance->getFounder(); |
|
50
|
1 |
|
$this->allianceJobManager->removeUserFromJob($user, $founderJob); |
|
51
|
1 |
|
$this->allianceJobManager->removeUserFromAllJobs($newFounder, $alliance); |
|
52
|
1 |
|
$this->allianceJobManager->assignUserToJob($newFounder, $founderJob); |
|
53
|
|
|
} else { |
|
54
|
1 |
|
$this->allianceJobManager->removeUserFromAllJobs($user, $alliance); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Collection<int, User> $remainingMembers |
|
60
|
|
|
*/ |
|
61
|
2 |
|
private function findNewFounder(\Stu\Orm\Entity\Alliance $alliance, Collection $remainingMembers): ?User |
|
62
|
|
|
{ |
|
63
|
2 |
|
if ($remainingMembers->isEmpty()) { |
|
64
|
1 |
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
$successorJob = $alliance->getSuccessor(); |
|
|
|
|
|
|
68
|
1 |
|
if ($successorJob !== null) { |
|
|
|
|
|
|
69
|
1 |
|
$successors = $successorJob->getUsers(); |
|
70
|
1 |
|
if (!empty($successors)) { |
|
71
|
1 |
|
return $this->getLastOnlineUser($successors); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$diplomaticJob = $alliance->getDiplomatic(); |
|
|
|
|
|
|
76
|
|
|
if ($diplomaticJob !== null) { |
|
|
|
|
|
|
77
|
|
|
$diplomats = $diplomaticJob->getUsers(); |
|
78
|
|
|
if (!empty($diplomats)) { |
|
79
|
|
|
return $this->getLastOnlineUser($diplomats); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->getLastOnlineUser($remainingMembers->toArray()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param array<User> $users |
|
88
|
|
|
*/ |
|
89
|
1 |
|
private function getLastOnlineUser(array $users): ?User |
|
90
|
|
|
{ |
|
91
|
1 |
|
if (empty($users)) { |
|
92
|
|
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
$lastOnlineUser = null; |
|
96
|
1 |
|
$maxLastAction = 0; |
|
97
|
|
|
|
|
98
|
1 |
|
foreach ($users as $user) { |
|
99
|
1 |
|
if ($user->getLastaction() > $maxLastAction) { |
|
100
|
1 |
|
$maxLastAction = $user->getLastaction(); |
|
101
|
1 |
|
$lastOnlineUser = $user; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return $lastOnlineUser; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths