Passed
Push — master ( ad0a87...920d42 )
by Nico
05:24
created

AllianceDeletionHandler::delete()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4.002

Importance

Changes 0
Metric Value
cc 4
eloc 21
nc 4
nop 1
dl 0
loc 31
ccs 19
cts 20
cp 0.95
crap 4.002
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Player\Deletion\Handler;
6
7
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...
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();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $successorJob is correct as $alliance->getSuccessor() targeting Stu\Orm\Entity\Alliance::getSuccessor() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
68 1
        if ($successorJob !== null) {
0 ignored issues
show
introduced by
The condition $successorJob !== null is always false.
Loading history...
69 1
            $successors = $successorJob->getUsers();
70 1
            if (!empty($successors)) {
71 1
                return $this->getLastOnlineUser($successors);
72
            }
73
        }
74
75
        $diplomaticJob = $alliance->getDiplomatic();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $diplomaticJob is correct as $alliance->getDiplomatic() targeting Stu\Orm\Entity\Alliance::getDiplomatic() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76
        if ($diplomaticJob !== null) {
0 ignored issues
show
introduced by
The condition $diplomaticJob !== null is always false.
Loading history...
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