Passed
Push — dev ( a37614...5d6f44 )
by Nico
05:36
created

AllianceActionManager::mayEditFactionMode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6.9849

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 2
dl 0
loc 14
ccs 3
cts 7
cp 0.4286
crap 6.9849
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Alliance\Lib;
6
7
use Noodlehaus\ConfigInterface;
8
use RuntimeException;
9
use Stu\Component\Station\Dock\DockTypeEnum;
10
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
11
use Stu\Module\PlayerSetting\Lib\UserConstants;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserConstants 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...
12
use Stu\Orm\Entity\Alliance;
13
use Stu\Orm\Entity\AllianceJob;
14
use Stu\Orm\Entity\User;
15
use Stu\Orm\Repository\AllianceJobRepositoryInterface;
16
use Stu\Orm\Repository\AllianceRepositoryInterface;
17
use Stu\Orm\Repository\DockingPrivilegeRepositoryInterface;
18
use Stu\Orm\Repository\StationRepositoryInterface;
19
use Stu\Orm\Repository\UserRepositoryInterface;
20
21
final class AllianceActionManager implements AllianceActionManagerInterface
22
{
23 1
    public function __construct(
24
        private AllianceJobRepositoryInterface $allianceJobRepository,
25
        private AllianceRepositoryInterface $allianceRepository,
26
        private DockingPrivilegeRepositoryInterface $dockingPrivilegeRepository,
27
        private PrivateMessageSenderInterface $privateMessageSender,
28
        private UserRepositoryInterface $userRepository,
29
        private ConfigInterface $config,
30
        private AllianceJobManagerInterface $allianceJobManager,
31
        private StationRepositoryInterface $stationRepository
32 1
    ) {}
33
34
    #[\Override]
35
    public function assignUserToJob(User $user, AllianceJob $job): void
36
    {
37
        $this->allianceJobManager->assignUserToJob($user, $job);
38
    }
39
40
    #[\Override]
41
    public function delete(Alliance $alliance, bool $sendMesage = true): void
42
    {
43
        $this->dockingPrivilegeRepository->truncateByTypeAndTarget(DockTypeEnum::ALLIANCE, $alliance->getId());
44
45
        foreach ($alliance->getStations() as $station) {
46
            $station->setAlliance(null);
47
            $this->stationRepository->save($station);
48
        }
49
50
        $text = sprintf(_('Die Allianz %s wurde aufgelöst'), $alliance->getName());
51
52
        foreach ($alliance->getMembers() as $user) {
53
            if ($sendMesage === true) {
54
                $this->privateMessageSender->send(UserConstants::USER_NOONE, $user->getId(), $text);
55
            }
56
57
            $user->setAlliance(null);
58
59
            $this->userRepository->save($user);
60
        }
61
62
        if ($alliance->hasAvatar()) {
63
            $result = @unlink(
64
                sprintf(
65
                    '%s/%s/%s.png',
66
                    $this->config->get('game.webroot'),
67
                    $this->config->get('game.alliance_avatar_path'),
68
                    $alliance->getAvatar()
69
                )
70
            );
71
72
            if ($result === false) {
73
                throw new RuntimeException('alliance avatar could not be deleted');
74
            }
75
        }
76
77
        foreach ($alliance->getJobs() as $job) {
78
            $this->allianceJobRepository->delete($job);
79
        }
80
81
        $this->allianceRepository->delete($alliance);
82
    }
83
84
    #[\Override]
85
    public function sendMessage(int $allianceId, string $text): void
86
    {
87
        $alliance = $this->allianceRepository->find($allianceId);
88
89
        if ($alliance === null) {
90
            return;
91
        }
92
93
        foreach ($alliance->getMembers() as $member) {
94
            $this->privateMessageSender->send(UserConstants::USER_NOONE, $member->getId(), $text);
95
        }
96
    }
97
98 1
    #[\Override]
99
    public function mayEditFactionMode(Alliance $alliance, int $factionId): bool
100
    {
101 1
        if ($alliance->getFaction() === null) {
102 1
            return true;
103
        }
104
105
        foreach ($alliance->getMembers() as $obj) {
106
            if ($obj->getFactionId() !== $factionId) {
107
                return false;
108
            }
109
        }
110
111
        return true;
112
    }
113
}
114