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

AllianceActionManager::setJobForUser()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 3
dl 0
loc 19
ccs 0
cts 13
cp 0
crap 12
rs 9.8666
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A AllianceActionManager::assignUserToJob() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Alliance\Lib;
6
7
use Noodlehaus\ConfigInterface;
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 RuntimeException;
10
use Stu\Component\Station\Dock\DockTypeEnum;
11
use Stu\Module\Message\Lib\PrivateMessageSenderInterface;
12
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...
13
use Stu\Orm\Entity\Alliance;
14
use Stu\Orm\Entity\AllianceJob;
15
use Stu\Orm\Entity\User;
16
use Stu\Orm\Repository\AllianceJobRepositoryInterface;
17
use Stu\Orm\Repository\AllianceRepositoryInterface;
18
use Stu\Orm\Repository\DockingPrivilegeRepositoryInterface;
19
use Stu\Orm\Repository\UserRepositoryInterface;
20
use Stu\Orm\Repository\AllianceMemberJobRepositoryInterface;
21
22
final class AllianceActionManager implements AllianceActionManagerInterface
23
{
24 1
    public function __construct(
25
        private AllianceJobRepositoryInterface $allianceJobRepository,
26
        private AllianceRepositoryInterface $allianceRepository,
27
        private DockingPrivilegeRepositoryInterface $dockingPrivilegeRepository,
28
        private PrivateMessageSenderInterface $privateMessageSender,
29
        private UserRepositoryInterface $userRepository,
30
        private ConfigInterface $config,
31
        private AllianceJobManagerInterface $allianceJobManager
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
        $text = sprintf(_('Die Allianz %s wurde aufgelöst'), $alliance->getName());
46
47
        foreach ($alliance->getMembers() as $user) {
48
            if ($sendMesage === true) {
49
                $this->privateMessageSender->send(UserConstants::USER_NOONE, $user->getId(), $text);
50
            }
51
52
            $user->setAlliance(null);
53
54
            $this->userRepository->save($user);
55
        }
56
57
        if ($alliance->hasAvatar()) {
58
            $result = @unlink(
59
                sprintf(
60
                    '%s/%s/%s.png',
61
                    $this->config->get('game.webroot'),
62
                    $this->config->get('game.alliance_avatar_path'),
63
                    $alliance->getAvatar()
64
                )
65
            );
66
67
68
            if ($result === false) {
69
                throw new RuntimeException('alliance avatar could not be deleted');
70
            }
71
        }
72
73
        foreach ($alliance->getJobs() as $job) {
74
            $this->allianceJobRepository->delete($job);
75
        }
76
77
        $this->allianceRepository->delete($alliance);
78
    }
79
80 9
    #[Override]
81
    public function mayEdit(Alliance $alliance, User $user): bool
82
    {
83 9
        return $this->allianceJobManager->hasUserFounderPermission($user, $alliance)
84 9
            || $this->allianceJobManager->hasUserSuccessorPermission($user, $alliance);
85
    }
86
87 2
    #[Override]
88
    public function mayManageForeignRelations(Alliance $alliance, User $user): bool
89
    {
90 2
        return $this->allianceJobManager->hasUserDiplomaticPermission($user, $alliance)
91 2
            || $this->mayEdit($alliance, $user);
92
    }
93
94
    #[Override]
95
    public function sendMessage(int $allianceId, string $text): void
96
    {
97
        $alliance = $this->allianceRepository->find($allianceId);
98
99
        if ($alliance === null) {
100
            return;
101
        }
102
103
        foreach ($alliance->getMembers() as $member) {
104
            $this->privateMessageSender->send(UserConstants::USER_NOONE, $member->getId(), $text);
105
        }
106
    }
107
108 1
    #[Override]
109
    public function mayEditFactionMode(Alliance $alliance, int $factionId): bool
110
    {
111 1
        if ($alliance->getFaction() === null) {
112 1
            return true;
113
        }
114
115
        foreach ($alliance->getMembers() as $obj) {
116
            if ($obj->getFactionId() !== $factionId) {
117
                return false;
118
            }
119
        }
120
121
        return true;
122
    }
123
}
124