CommunicationProvider::setTemplateVariables()   C
last analyzed

Complexity

Conditions 13
Paths 128

Size

Total Lines 79
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 55
nc 128
nop 1
dl 0
loc 79
ccs 0
cts 61
cp 0
crap 182
rs 6.3833
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Communication\Kn\KnFactoryInterface;
10
use Stu\Component\Communication\Kn\KnItemInterface;
11
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...
12
use Stu\Component\Game\JavascriptExecutionTypeEnum;
13
use Stu\Module\Communication\View\ShowKnArchive\ShowKnArchive;
14
use Stu\Module\Control\GameControllerInterface;
15
use Stu\Orm\Entity\KnPost;
16
use Stu\Orm\Entity\User;
17
use Stu\Orm\Repository\KnPostArchivRepositoryInterface;
18
use Stu\Orm\Repository\KnPostRepositoryInterface;
19
20
final class CommunicationProvider implements ViewComponentProviderInterface
21
{
22 1
    public function __construct(
23
        private KnPostRepositoryInterface $knPostRepository,
24
        private KnFactoryInterface $knFactory,
25
        private KnPostArchivRepositoryInterface $knPostArchivRepository
26 1
    ) {}
27
28
    #[Override]
29
    public function setTemplateVariables(GameControllerInterface $game): void
30
    {
31
        $user = $game->getUser();
32
        $userKnMark = $user->getKnMark();
33
34
        $newKnPostCount = $this->knPostRepository->getAmountSince($userKnMark);
35
        $knPostCount = $this->knPostRepository->getAmount();
36
37
        $mark = $knPostCount;
38
        $lim = floor($mark / GameEnum::KN_PER_SITE) * GameEnum::KN_PER_SITE;
39
        $knStart = $mark % GameEnum::KN_PER_SITE == 0 ? $lim - GameEnum::KN_PER_SITE : $lim;
40
41
        $mark = request::getInt('mark');
42
        if ($mark % GameEnum::KN_PER_SITE != 0 || $mark < 0) {
43
            $mark = 0;
44
        }
45
        if (request::getInt('user_mark') !== 0) {
46
            $mark = max(0, (int) floor(($newKnPostCount - 1) / GameEnum::KN_PER_SITE) * GameEnum::KN_PER_SITE);
47
        }
48
49
        $maxpage = ceil($knPostCount / GameEnum::KN_PER_SITE);
50
        $curpage = floor($mark / GameEnum::KN_PER_SITE);
51
        $knNavigation = [];
52
        if ($curpage != 0) {
53
            $knNavigation[] = ["page" => "<<", "mark" => 0, "cssclass" => "pages"];
54
            $knNavigation[] = ["page" => "<", "mark" => ($mark - GameEnum::KN_PER_SITE), "cssclass" => "pages"];
55
        }
56
        for ($i = $curpage - 1; $i <= $curpage + 3; $i++) {
57
            if ($i > $maxpage || $i < 1) {
58
                continue;
59
            }
60
            $knNavigation[] = [
61
                "page" => $i,
62
                "mark" => ($i * GameEnum::KN_PER_SITE - GameEnum::KN_PER_SITE),
63
                "cssclass" => ($curpage + 1 === $i ? "pages selected" : "pages")
64
            ];
65
        }
66
        if ($curpage + 1 !== $maxpage) {
67
            $knNavigation[] = ["page" => ">", "mark" => ($mark + GameEnum::KN_PER_SITE), "cssclass" => "pages"];
68
            $knNavigation[] = ["page" => ">>", "mark" => $maxpage * GameEnum::KN_PER_SITE - GameEnum::KN_PER_SITE, "cssclass" => "pages"];
69
        }
70
71
        $markedPostId = $this->getMarkedKnId($user);
72
73
        $game->setTemplateVar(
74
            'KN_POSTINGS',
75
            array_map(
76
                function (KnPost $knPost) use ($user, $markedPostId): KnItemInterface {
77
                    $knItem = $this->knFactory->createKnItem(
78
                        $knPost,
79
                        $user
80
                    );
81
                    if ($markedPostId && $knItem->getId() == $markedPostId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $markedPostId of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
82
                        $knItem->setIsHighlighted(true);
83
                    }
84
                    return $knItem;
85
                },
86
                $this->knPostRepository->getBy($mark, GameEnum::KN_PER_SITE)
87
            )
88
        );
89
        $game->setTemplateVar('HAS_NEW_KN_POSTINGS', $this->knPostRepository->getAmountSince($userKnMark));
90
        $game->setTemplateVar('KN_START', $knStart);
91
        $game->setTemplateVar('KN_OFFSET', $mark);
92
        $game->setTemplateVar('NEW_KN_POSTING_COUNT', $newKnPostCount);
93
        $game->setTemplateVar('KN_NAVIGATION', $knNavigation);
94
95
        $availableVersions = $this->knPostArchivRepository->getAvailableVersions();
96
        $formattedVersions = array_map(function ($version) {
97
            return [
98
                'version' => $version,
99
                'display' => $this->formatVersion($version)
100
            ];
101
        }, $availableVersions);
102
103
        $game->setTemplateVar('AVAILABLE_ARCHIVE_VERSIONS', $formattedVersions);
104
        $game->setTemplateVar('SHOW_ARCHIVE_VIEW', ShowKnArchive::VIEW_IDENTIFIER);
105
106
        $game->addExecuteJS("initTranslations();", JavascriptExecutionTypeEnum::AFTER_RENDER);
107
    }
108
109
    private function getMarkedKnId(User $user): ?int
110
    {
111
        $markedPostId = request::getInt('markedPost');
112
        if ($markedPostId !== 0) {
113
            return $markedPostId;
114
        }
115
116
        $newerKnPosts = $this->knPostRepository->getNewerThenMark($user->getKnMark());
117
        if ($newerKnPosts !== []) {
118
            return $newerKnPosts[0]->getId();
119
        }
120
121
        return null;
122
    }
123
124
    private function formatVersion(string $version): string
125
    {
126
        $cleanVersion = ltrim($version, 'v');
127
128
        if (str_contains($cleanVersion, 'alpha')) {
129
            return 'v' . str_replace('alpha', 'α', $cleanVersion);
130
        }
131
132
        if (preg_match('/^(\d)(\d)$/', $cleanVersion, $matches)) {
133
            return 'v' . $matches[1] . '.' . $matches[2];
134
        }
135
136
        return $version;
137
    }
138
}
139