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

MessageFolderComponent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Module\Game\Component;
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 Stu\Component\Player\UserAwardEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Component\Player\UserAwardEnum 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...
9
use Stu\Lib\Component\ComponentInterface;
10
use Stu\Module\Control\GameControllerInterface;
11
use Stu\Module\Message\Lib\PrivateMessageFolderTypeEnum;
12
use Stu\Module\Message\Lib\PrivateMessageUiFactoryInterface;
13
use Stu\Orm\Entity\PrivateMessageFolderInterface;
14
use Stu\Orm\Entity\UserInterface;
15
use Stu\Orm\Repository\PrivateMessageFolderRepositoryInterface;
16
17
/**
18
 * Renders the pm folders in the header
19
 */
20
final class MessageFolderComponent implements ComponentInterface
21
{
22 3
    public function __construct(
23
        private PrivateMessageFolderRepositoryInterface $privateMessageFolderRepository,
24
        private PrivateMessageUiFactoryInterface $commUiFactory
25 3
    ) {}
26
27 6
    #[Override]
28
    public function setTemplateVariables(GameControllerInterface $game): void
29
    {
30 6
        $user = $game->getUser();
31
32 6
        $pmFolder = [
33 6
            PrivateMessageFolderTypeEnum::SPECIAL_MAIN,
34 6
            PrivateMessageFolderTypeEnum::SPECIAL_SHIP,
35 6
            PrivateMessageFolderTypeEnum::SPECIAL_STATION,
36 6
            PrivateMessageFolderTypeEnum::SPECIAL_COLONY,
37 6
            PrivateMessageFolderTypeEnum::SPECIAL_TRADE,
38 6
            PrivateMessageFolderTypeEnum::SPECIAL_SYSTEM
39 6
        ];
40 6
        $folder = [];
41
42 6
        foreach ($pmFolder as $folderType) {
43
            if (
44 6
                $folderType === PrivateMessageFolderTypeEnum::SPECIAL_STATION
45 6
                && !$this->hasStationsPmCategory($user)
46
            ) {
47 5
                continue;
48
            }
49
50
            /** @var PrivateMessageFolderInterface $specialFolder */
51 6
            $specialFolder = $this->privateMessageFolderRepository->getByUserAndSpecial($user->getId(), $folderType);
52
53 6
            $folder[$folderType->value] = $this->commUiFactory->createPrivateMessageFolderItem($specialFolder);
54
        }
55
56 6
        $game->setTemplateVar('PM', $folder);
57
    }
58
59 6
    private function hasStationsPmCategory(UserInterface $user): bool
60
    {
61 6
        if ($user->isNpc()) {
62 1
            return true;
63
        }
64
65 5
        return $user->hasAward(UserAwardEnum::RESEARCHED_STATIONS);
66
    }
67
}
68