Passed
Push — master ( 4c20c0...1d94dd )
by Janko
29:51 queued 14:43
created

CommunicationProvider::setTemplateVariables()   C

Complexity

Conditions 13
Paths 128

Size

Total Lines 69
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 47
nc 128
nop 1
dl 0
loc 69
ccs 0
cts 52
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\Module\Control\GameControllerInterface;
13
use Stu\Orm\Entity\KnPostInterface;
14
use Stu\Orm\Entity\UserInterface;
15
use Stu\Orm\Repository\KnPostRepositoryInterface;
16
17
final class CommunicationProvider implements ViewComponentProviderInterface
18
{
19 1
    public function __construct(private KnPostRepositoryInterface $knPostRepository, private KnFactoryInterface $knFactory) {}
20
21
    #[Override]
22
    public function setTemplateVariables(GameControllerInterface $game): void
23
    {
24
        $user = $game->getUser();
25
        $userKnMark = $user->getKnMark();
26
27
        $newKnPostCount = $this->knPostRepository->getAmountSince($userKnMark);
28
        $knPostCount = $this->knPostRepository->getAmount();
29
30
        $mark = $knPostCount;
31
        $lim = floor($mark / GameEnum::KN_PER_SITE) * GameEnum::KN_PER_SITE;
32
        $knStart = $mark % GameEnum::KN_PER_SITE == 0 ? $lim - GameEnum::KN_PER_SITE : $lim;
33
34
        $mark = request::getInt('mark');
35
        if ($mark % GameEnum::KN_PER_SITE != 0 || $mark < 0) {
36
            $mark = 0;
37
        }
38
        if (request::getInt('user_mark') !== 0) {
39
            $mark = max(0, (int) floor(($newKnPostCount - 1) / GameEnum::KN_PER_SITE) * GameEnum::KN_PER_SITE);
40
        }
41
42
        $maxpage = ceil($knPostCount / GameEnum::KN_PER_SITE);
43
        $curpage = floor($mark / GameEnum::KN_PER_SITE);
44
        $knNavigation = [];
45
        if ($curpage != 0) {
46
            $knNavigation[] = ["page" => "<<", "mark" => 0, "cssclass" => "pages"];
47
            $knNavigation[] = ["page" => "<", "mark" => ($mark - GameEnum::KN_PER_SITE), "cssclass" => "pages"];
48
        }
49
        for ($i = $curpage - 1; $i <= $curpage + 3; $i++) {
50
            if ($i > $maxpage || $i < 1) {
51
                continue;
52
            }
53
            $knNavigation[] = [
54
                "page" => $i,
55
                "mark" => ($i * GameEnum::KN_PER_SITE - GameEnum::KN_PER_SITE),
56
                "cssclass" => ($curpage + 1 === $i ? "pages selected" : "pages")
57
            ];
58
        }
59
        if ($curpage + 1 !== $maxpage) {
60
            $knNavigation[] = ["page" => ">", "mark" => ($mark + GameEnum::KN_PER_SITE), "cssclass" => "pages"];
61
            $knNavigation[] = ["page" => ">>", "mark" => $maxpage * GameEnum::KN_PER_SITE - GameEnum::KN_PER_SITE, "cssclass" => "pages"];
62
        }
63
64
65
        $markedPostId = $this->getMarkedKnId($user);
66
67
        $game->setTemplateVar(
68
            'KN_POSTINGS',
69
            array_map(
70
                function (KnPostInterface $knPost) use ($user, $markedPostId): KnItemInterface {
71
                    $knItem = $this->knFactory->createKnItem(
72
                        $knPost,
73
                        $user
74
                    );
75
                    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...
76
                        $knItem->setIsHighlighted(true);
77
                    }
78
                    return $knItem;
79
                },
80
                $this->knPostRepository->getBy($mark, GameEnum::KN_PER_SITE)
81
            )
82
        );
83
        $game->setTemplateVar('HAS_NEW_KN_POSTINGS', $this->knPostRepository->getAmountSince($userKnMark));
84
        $game->setTemplateVar('KN_START', $knStart);
85
        $game->setTemplateVar('KN_OFFSET', $mark);
86
        $game->setTemplateVar('NEW_KN_POSTING_COUNT', $newKnPostCount);
87
        $game->setTemplateVar('KN_NAVIGATION', $knNavigation);
88
89
        $game->addExecuteJS("initTranslations();", GameEnum::JS_EXECUTION_AFTER_RENDER);
90
    }
91
92
    private function getMarkedKnId(UserInterface $user): ?int
93
    {
94
        $markedPostId = request::getInt('markedPost');
95
        if ($markedPostId !== 0) {
96
            return $markedPostId;
97
        }
98
99
        $newerKnPosts = $this->knPostRepository->getNewerThenMark($user->getKnMark());
100
        if ($newerKnPosts !== []) {
101
            return $newerKnPosts[0]->getId();
102
        }
103
104
        return null;
105
    }
106
}
107