Passed
Pull Request — master (#1821)
by Nico
52:02 queued 25:23
created

FriendDeterminator::isFriend()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 6
nop 2
dl 0
loc 36
ccs 24
cts 24
cp 1
crap 7
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Player\Relation;
6
7
use Stu\Component\Alliance\AllianceEnum;
8
use Stu\Orm\Entity\UserInterface;
9
use Stu\Orm\Repository\AllianceRelationRepositoryInterface;
10
use Stu\Orm\Repository\ContactRepositoryInterface;
11
12
class FriendDeterminator
13
{
14
    private AllianceRelationRepositoryInterface $allianceRelationRepository;
15
16
    private ContactRepositoryInterface $contactRepository;
17
18 4
    public function __construct(
19
        AllianceRelationRepositoryInterface $allianceRelationRepository,
20
        ContactRepositoryInterface $contactRepository
21
    ) {
22 4
        $this->allianceRelationRepository = $allianceRelationRepository;
23 4
        $this->contactRepository = $contactRepository;
24
    }
25
26 4
    public function isFriend(UserInterface $user, UserInterface $otherUser): PlayerRelationTypeEnum
27
    {
28 4
        $alliance = $user->getAlliance();
29
30 4
        $otherUserAlliance = $otherUser->getAlliance();
31
32 4
        if ($alliance !== null && $otherUserAlliance !== null) {
33 3
            if ($alliance === $otherUserAlliance) {
34 1
                return PlayerRelationTypeEnum::ALLY;
35
            }
36
37 2
            $result = $this->allianceRelationRepository->getActiveByTypeAndAlliancePair(
38 2
                [
39 2
                    AllianceEnum::ALLIANCE_RELATION_FRIENDS,
40 2
                    AllianceEnum::ALLIANCE_RELATION_ALLIED,
41 2
                    AllianceEnum::ALLIANCE_RELATION_VASSAL
42 2
                ],
43 2
                $otherUserAlliance->getId(),
44 2
                $alliance->getId()
45 2
            );
46
47 2
            if ($result !== null) {
48 1
                return PlayerRelationTypeEnum::ALLY;
49
            }
50
        }
51
52 2
        $contact = $this->contactRepository->getByUserAndOpponent(
53 2
            $user->getId(),
54 2
            $otherUser->getId()
55 2
        );
56
57 2
        if ($contact !== null && $contact->isFriendly()) {
58 1
            return PlayerRelationTypeEnum::USER;
59
        }
60
61 1
        return PlayerRelationTypeEnum::NONE;
62
    }
63
}
64