Passed
Pull Request — master (#2161)
by Janko
25:16 queued 15:11
created

UserProfileProvider::hasTranslation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
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 3
    ) {}
30
31 3
    #[Override]
32
    public function setTemplateVariables(GameControllerInterface $game): void
33
    {
34 3
        if (!request::has('uid')) {
35 1
            $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 2
        $visitor = $game->getUser();
46
47 2
        $this->profileVisitorRegistration->register($user, $visitor);
48
49 2
        $game->setTemplateVar('PROFILE', $user);
50 2
        $game->setTemplateVar('HAS_TRANSLATION', $this->hasTranslation($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 2
    private function hasTranslation(UserInterface $user): bool
83
    {
84 2
        $text = $user->getDescription();
85 2
        return strpos($text, '[translate]') !== false && strpos($text, '[/translate]') !== false;
86
    }
87
88
    /**
89
     * @return array<int, ColonyScanInterface>
90
     */
91 2
    private function getColonyScanList(UserInterface $user, UserInterface $visitor): array
92
    {
93 2
        $alliance = $visitor->getAlliance();
94
95 2
        if ($alliance !== null) {
96 1
            $unfilteredScans = array_merge(...$alliance->getMembers()->map(fn(UserInterface $user) => $user->getColonyScans()->toArray()));
97
        } else {
98 1
            $unfilteredScans = $visitor->getColonyScans()->toArray();
99
        }
100
101 2
        $filteredScans = array_filter(
102 2
            $unfilteredScans,
103 2
            fn(ColonyScanInterface $scan): bool => $scan->getColonyUserId() === $user->getId()
104 2
        );
105
106 2
        $scansByColony = [];
107 2
        foreach ($filteredScans as $scan) {
108 1
            $colonyId = $scan->getColony()->getId();
109 1
            if (!isset($scansByColony[$colonyId])) {
110 1
                $scansByColony[$colonyId] = [];
111
            }
112 1
            $scansByColony[$colonyId][] = $scan;
113
        }
114
115 2
        $latestScans = [];
116 2
        foreach ($scansByColony as $scans) {
117 1
            usort($scans, fn($a, $b): int => $b->getDate() <=> $a->getDate());
118 1
            $latestScans[] = $scans[0];
119
        }
120
121 2
        return $latestScans;
122
    }
123
}
124