Passed
Push — master ( 9c3618...35d629 )
by Janko
20:35 queued 11:32
created

AllianceDeletionHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 38
c 1
b 1
f 0
dl 0
loc 67
ccs 30
cts 40
cp 0.75
rs 10
wmc 14

3 Methods

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