Passed
Push — dev ( 2d398e...250fad )
by Janko
12:46
created

CommunicationProvider::setTemplateVariables()   C

Complexity

Conditions 13
Paths 128

Size

Total Lines 69
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 13.2597

Importance

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