Passed
Push — master ( 48bfaa...170129 )
by Nico
25:44
created

UserProfileProvider::setTemplateVariables()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 3.0001

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 32
nc 3
nop 1
dl 0
loc 47
ccs 38
cts 39
cp 0.9744
crap 3.0001
rs 9.408
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Game\Lib\View\Provider;
6
7
use request;
8
use Stu\Lib\ParserWithImageInterface;
9
use Stu\Module\Control\Exception\ItemNotFoundException;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Game\Lib\View\Provider\ViewComponentProviderInterface;
12
use Stu\Module\Message\Lib\ContactListModeEnum;
13
use Stu\Module\PlayerProfile\Lib\ProfileVisitorRegistrationInterface;
14
use Stu\Orm\Repository\ContactRepositoryInterface;
15
use Stu\Orm\Repository\RpgPlotMemberRepositoryInterface;
16
use Stu\Orm\Repository\UserRepositoryInterface;
17
use Stu\Component\Game\GameEnum;
18
use Stu\Orm\Entity\UserInterface;
19
use Stu\Orm\Entity\ColonyScanInterface;
20
21
final class UserProfileProvider implements ViewComponentProviderInterface
22
{
23 2
    public function __construct(
24
        private RpgPlotMemberRepositoryInterface $rpgPlotMemberRepository,
25
        private ContactRepositoryInterface $contactRepository,
26
        private UserRepositoryInterface $userRepository,
27
        private ParserWithImageInterface $parserWithImage,
28
        private ProfileVisitorRegistrationInterface $profileVisitorRegistration
29
    ) {
30 2
    }
31
32 2
    public function setTemplateVariables(GameControllerInterface $game): void
33
    {
34 2
        if (!request::has('uid')) {
35
            $user = $game->getUser();
36
        } else {
37 2
            $userId = request::getIntFatal('uid');
38
39 2
            $user = $this->userRepository->find($userId);
40 2
            if ($user === null) {
41 1
                throw new ItemNotFoundException();
42
            }
43
        }
44
45 1
        $visitor = $game->getUser();
46
47 1
        $this->profileVisitorRegistration->register($user, $visitor);
48
49 1
        $game->setTemplateVar('PROFILE', $user);
50 1
        $game->setTemplateVar('COLONYSCANLIST', $this->getColonyScanList($user, $visitor));
51 1
        $game->setTemplateVar(
52 1
            'DESCRIPTION',
53 1
            $this->parserWithImage->parse($user->getDescription())->getAsHTML()
54 1
        );
55 1
        $game->setTemplateVar(
56 1
            'IS_PROFILE_CURRENT_USER',
57 1
            $user === $visitor
58 1
        );
59 1
        $game->setTemplateVar(
60 1
            'RPG_PLOTS',
61 1
            $this->rpgPlotMemberRepository->getByUser($user)
62 1
        );
63 1
        $game->setTemplateVar(
64 1
            'CONTACT',
65 1
            $this->contactRepository->getByUserAndOpponent(
66 1
                $visitor->getId(),
67 1
                $user->getId()
68 1
            )
69 1
        );
70 1
        $game->setTemplateVar(
71 1
            'FRIENDS',
72 1
            $this->userRepository->getFriendsByUserAndAlliance(
73 1
                $user,
74 1
                $user->getAlliance()
75 1
            )
76 1
        );
77 1
        $game->setTemplateVar('CONTACT_LIST_MODES', ContactListModeEnum::cases());
78 1
        $game->addExecuteJS("initTranslations();", GameEnum::JS_EXECUTION_AFTER_RENDER);
79
    }
80
81
    /**
82
     * @return array<ColonyScanInterface>
83
     */
84 1
    public function getColonyScanList(UserInterface $user, UserInterface $visitor): iterable
85
    {
86 1
        $alliance = $visitor->getAlliance();
87
88 1
        if ($alliance !== null) {
89
            $unfilteredScans = array_merge(...$alliance->getMembers()->map(fn (UserInterface $user) => $user->getColonyScans()->toArray()));
90
        } else {
91 1
            $unfilteredScans = $visitor->getColonyScans()->toArray();
92
        }
93
94
95 1
        return $this->filterByUser($unfilteredScans, $user);
96
    }
97
98
    /**
99
     * @param array<int, ColonyScanInterface> $colonyScans
100
     * 
101
     * @return array<int, ColonyScanInterface>
102
     */
103 1
    private function filterByUser(array $colonyScans, UserInterface $user): array
104
    {
105 1
        return array_filter(
106 1
            $colonyScans,
107 1
            fn (ColonyScanInterface $scan) => $scan->getColonyUserId() === $user->getId()
108 1
        );
109
    }
110
}
111