Passed
Pull Request — master (#1969)
by Janko
22:34 queued 10:03
created

UserProfileProvider::setTemplateVariables()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 3

Importance

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