Passed
Push — master ( 1370c6...b5ba92 )
by Nico
60:32 queued 29:07
created

PlayerRelationDeterminator::isEnemy()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 6
nop 2
dl 0
loc 30
ccs 20
cts 20
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Component\Player;
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
/**
13
 * Some locations require to determine if a certain user is friendly towards the player
14
 */
15
final class PlayerRelationDeterminator implements PlayerRelationDeterminatorInterface
16
{
17
    private AllianceRelationRepositoryInterface $allianceRelationRepository;
18
19
    private ContactRepositoryInterface $contactRepository;
20
21 8
    public function __construct(
22
        AllianceRelationRepositoryInterface $allianceRelationRepository,
23
        ContactRepositoryInterface $contactRepository
24
    ) {
25 8
        $this->allianceRelationRepository = $allianceRelationRepository;
26 8
        $this->contactRepository = $contactRepository;
27
    }
28
29 4
    public function isFriend(UserInterface $user, UserInterface $otherUser): bool
30
    {
31 4
        $alliance = $user->getAlliance();
32
33 4
        $otherUserAlliance = $otherUser->getAlliance();
34
35 4
        if ($alliance !== null && $otherUserAlliance !== null) {
36 3
            if ($alliance === $otherUserAlliance) {
37 1
                return true;
38
            }
39
40 2
            $result = $this->allianceRelationRepository->getActiveByTypeAndAlliancePair(
41 2
                [
42 2
                    AllianceEnum::ALLIANCE_RELATION_FRIENDS,
43 2
                    AllianceEnum::ALLIANCE_RELATION_ALLIED,
44 2
                    AllianceEnum::ALLIANCE_RELATION_VASSAL
45 2
                ],
46 2
                $otherUserAlliance->getId(),
47 2
                $alliance->getId()
48 2
            );
49
50 2
            if ($result !== null) {
51 1
                return true;
52
            }
53
        }
54
55 2
        $contact = $this->contactRepository->getByUserAndOpponent(
56 2
            $user->getId(),
57 2
            $otherUser->getId()
58 2
        );
59
60 2
        return $contact !== null && $contact->isFriendly();
61
    }
62
63 4
    public function isEnemy(UserInterface $user, UserInterface $otherUser): bool
64
    {
65 4
        $alliance = $user->getAlliance();
66
67 4
        $otherUserAlliance = $otherUser->getAlliance();
68
69 4
        if ($alliance !== null && $otherUserAlliance !== null) {
70 3
            if ($alliance === $otherUserAlliance) {
71 1
                return false;
72
            }
73
74 2
            $result = $this->allianceRelationRepository->getActiveByTypeAndAlliancePair(
75 2
                [
76 2
                    AllianceEnum::ALLIANCE_RELATION_WAR,
77 2
                ],
78 2
                $otherUserAlliance->getId(),
79 2
                $alliance->getId()
80 2
            );
81
82 2
            if ($result !== null) {
83 1
                return true;
84
            }
85
        }
86
87 2
        $contact = $this->contactRepository->getByUserAndOpponent(
88 2
            $user->getId(),
89 2
            $otherUser->getId()
90 2
        );
91
92 2
        return $contact !== null && $contact->isEnemy();
93
    }
94
}
95