Passed
Pull Request — master (#1943)
by Nico
24:24 queued 10:41
created

AllianceDeletionHandler::getLastOnlineMember()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 13
ccs 5
cts 8
cp 0.625
crap 3.4746
rs 10
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\Component\Alliance\AllianceEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Alliance\AllianceEnum 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...
10
use Stu\Module\Alliance\Lib\AllianceActionManagerInterface;
11
use Stu\Orm\Entity\UserInterface;
12
use Stu\Orm\Repository\AllianceJobRepositoryInterface;
13
use Stu\Orm\Repository\UserRepositoryInterface;
14
15
final class AllianceDeletionHandler implements PlayerDeletionHandlerInterface
16
{
17 4
    public function __construct(private AllianceJobRepositoryInterface $allianceJobRepository, private AllianceActionManagerInterface $allianceActionManager, private UserRepositoryInterface $userRepository) {}
18
19 3
    #[Override]
20
    public function delete(UserInterface $user): void
21
    {
22 3
        foreach ($this->allianceJobRepository->getByUser($user->getId()) as $job) {
23 3
            if ($job->getType() === AllianceEnum::ALLIANCE_JOBS_FOUNDER) {
24 2
                $alliance = $job->getAlliance();
25
26 2
                foreach ($alliance->getMembers() as $member) {
27
                    $member->setAlliance(null);
28
                    $this->userRepository->save($member);
29
                }
30
31
32 2
                $successor = $alliance->getSuccessor();
33
34 2
                $diplomatic = $alliance->getDiplomatic();
35
36 2
                $members = $alliance->getMembers();
37 2
                $members->removeElement($user);
38
39 2
                $lastonlinemember = $this->getLastOnlineMember($members);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $lastonlinemember is correct as $this->getLastOnlineMember($members) targeting Stu\Component\Player\Del...::getLastOnlineMember() 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...
40
41 2
                if ($successor === null && $lastonlinemember === null) {
42 1
                    $this->allianceJobRepository->delete($job);
43 1
                    $this->allianceActionManager->delete($alliance);
44
                }
45 2
                if ($successor !== null) {
46 1
                    $successorUserId = $successor->getUserId();
47
48 1
                    $this->allianceActionManager->setJobForUser(
49 1
                        $alliance->getId(),
50 1
                        $successorUserId,
51 1
                        AllianceEnum::ALLIANCE_JOBS_FOUNDER
52 1
                    );
53 1
                    $this->allianceJobRepository->delete($successor);
54
                }
55 2
                if ($successor == null && $lastonlinemember != null) {
56
                    if ($diplomatic !== null && $lastonlinemember == $diplomatic->getUser()) {
57
                        $this->allianceJobRepository->delete($diplomatic);
58
                    }
59
                    $this->allianceActionManager->setJobForUser(
60
                        $alliance->getId(),
61
                        $lastonlinemember->getId(),
62
                        AllianceEnum::ALLIANCE_JOBS_FOUNDER
63
                    );
64
                }
65
            } else {
66 1
                $this->allianceJobRepository->delete($job);
67
            }
68
        }
69
    }
70
71
    /**
72
     * @param Collection<int, UserInterface> $members
73
     */
74 2
    private function getLastOnlineMember(Collection $members): ?UserInterface
75
    {
76 2
        $lastOnlineMember = null;
77 2
        $maxLastAction = 0;
78
79 2
        foreach ($members as $member) {
80
            if ($member->getLastAction() > $maxLastAction) {
81
                $maxLastAction = $member->getLastAction();
82
                $lastOnlineMember = $member;
83
            }
84
        }
85
86 2
        return $lastOnlineMember;
87
    }
88
}
89