Passed
Pull Request — master (#1930)
by Janko
14:58 queued 05:13
created

AllianceDeletionHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 73.68%

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 66
ccs 28
cts 38
cp 0.7368
rs 10
c 0
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A getLastOnlineMember() 0 13 3
B delete() 0 42 10
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
14
final class AllianceDeletionHandler implements PlayerDeletionHandlerInterface
15
{
16 3
    public function __construct(private AllianceJobRepositoryInterface $allianceJobRepository, private AllianceActionManagerInterface $allianceActionManager) {}
17
18 3
    #[Override]
19
    public function delete(UserInterface $user): void
20
    {
21 3
        foreach ($this->allianceJobRepository->getByUser($user->getId()) as $job) {
22 3
            if ($job->getType() === AllianceEnum::ALLIANCE_JOBS_FOUNDER) {
23 2
                $alliance = $job->getAlliance();
24
25 2
                $successor = $alliance->getSuccessor();
26
27 2
                $diplomatic = $alliance->getDiplomatic();
28
29 2
                $members = $alliance->getMembers();
30 2
                $members->removeElement($user);
31
32 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...
33
34 2
                if ($successor === null && $lastonlinemember === null) {
35 1
                    $this->allianceJobRepository->delete($job);
36 1
                    $this->allianceActionManager->delete($alliance->getId(), true);
37
                }
38 2
                if ($successor !== null) {
39 1
                    $successorUserId = $successor->getUserId();
40
41 1
                    $this->allianceActionManager->setJobForUser(
42 1
                        $alliance->getId(),
43 1
                        $successorUserId,
44 1
                        AllianceEnum::ALLIANCE_JOBS_FOUNDER
45 1
                    );
46 1
                    $this->allianceJobRepository->delete($successor);
47
                }
48 2
                if ($successor == null && $lastonlinemember != null) {
49
                    if ($diplomatic !== null && $lastonlinemember == $diplomatic->getUser()) {
50
                        $this->allianceJobRepository->delete($diplomatic);
51
                    }
52
                    $this->allianceActionManager->setJobForUser(
53
                        $alliance->getId(),
54
                        $lastonlinemember->getId(),
55
                        AllianceEnum::ALLIANCE_JOBS_FOUNDER
56
                    );
57
                }
58
            } else {
59 1
                $this->allianceJobRepository->delete($job);
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param Collection<int, UserInterface> $members
66
     */
67 2
    private function getLastOnlineMember(Collection $members): ?UserInterface
68
    {
69 2
        $lastOnlineMember = null;
70 2
        $maxLastAction = 0;
71
72 2
        foreach ($members as $member) {
73
            if ($member->getLastAction() > $maxLastAction) {
74
                $maxLastAction = $member->getLastAction();
75
                $lastOnlineMember = $member;
76
            }
77
        }
78
79 2
        return $lastOnlineMember;
80
    }
81
}
82