Passed
Pull Request — master (#1884)
by Janko
50:38 queued 25:12
created

KnItem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
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 5
dl 0
loc 7
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\Component\Communication\Kn;
6
7
use Doctrine\Common\Collections\Collection;
8
use JBBCode\Parser;
9
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...
10
use Stu\Module\PlayerSetting\Lib\UserEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\PlayerSetting\Lib\UserEnum 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...
11
use Stu\Module\Template\StatusBarColorEnum;
0 ignored issues
show
Bug introduced by
The type Stu\Module\Template\StatusBarColorEnum 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\Template\StatusBarFactoryInterface;
13
use Stu\Orm\Entity\KnPostInterface;
14
use Stu\Orm\Entity\RpgPlotInterface;
15
use Stu\Orm\Entity\UserInterface;
16
use Stu\Orm\Repository\KnCommentRepositoryInterface;
17
18
final class KnItem implements KnItemInterface
19
{
20
    private ?int $mark = null;
21
22
    private bool $isHighlighted = false;
23
24 24
    public function __construct(
25
        private Parser $bbcodeParser,
26
        private KnCommentRepositoryInterface $knCommentRepository,
27
        private StatusBarFactoryInterface $statusBarFactory,
28
        private KnPostInterface $post,
29
        private UserInterface $currentUser
30 24
    ) {}
31
32 1
    #[Override]
33
    public function getId(): int
34
    {
35 1
        return $this->post->getId();
36
    }
37
38 1
    #[Override]
39
    public function getUser(): UserInterface
40
    {
41 1
        return $this->post->getUser();
42
    }
43
44 1
    #[Override]
45
    public function getUserId(): int
46
    {
47 1
        return $this->post->getUserId();
48
    }
49
50 1
    #[Override]
51
    public function getTitle(): ?string
52
    {
53 1
        return $this->post->getTitle();
54
    }
55
56 1
    #[Override]
57
    public function getText(): string
58
    {
59 1
        return $this->bbcodeParser->parse($this->post->getText())->getAsHTML();
60
    }
61
62 3
    #[Override]
63
    public function getDate(): int
64
    {
65 3
        return $this->post->getDate();
66
    }
67
68 1
    #[Override]
69
    public function getEditDate(): int
70
    {
71 1
        return $this->post->getEditDate();
72
    }
73
74 2
    #[Override]
75
    public function isEditAble(): bool
76
    {
77 2
        return $this->getDate() > time() - 600 && $this->post->getUser() === $this->currentUser;
78
    }
79
80 1
    #[Override]
81
    public function getPlot(): ?RpgPlotInterface
82
    {
83 1
        return $this->post->getRpgPlot();
84
    }
85
86
    #[Override]
87
    public function getCharacters(): Collection
88
    {
89
        return $this->post->getKnCharacters();
90
    }
91
92 1
    #[Override]
93
    public function getCommentCount(): int
94
    {
95 1
        return $this->knCommentRepository->getAmountByPost($this->post);
96
    }
97
98 2
    #[Override]
99
    public function displayContactLinks(): bool
100
    {
101 2
        $user = $this->post->getUser();
102 2
        return $user !== $this->currentUser && $user->getId() !== UserEnum::USER_NOONE;
103
    }
104
105 1
    #[Override]
106
    public function getUserName(): string
107
    {
108 1
        return $this->post->getUsername();
109
    }
110
111
    #[Override]
112
    public function isUserDeleted(): bool
113
    {
114
        return $this->post->getUserId() !== 1;
115
    }
116
117 2
    #[Override]
118
    public function isNewerThanMark(): bool
119
    {
120 2
        return $this->post->getId() > $this->currentUser->getKnMark();
121
    }
122
123 2
    #[Override]
124
    public function userCanRate(): bool
125
    {
126 2
        return !$this->userHasRated() && $this->currentUser !== $this->post->getUser() && $this->currentUser->getId() > 100;
127
    }
128
129 4
    #[Override]
130
    public function userHasRated(): bool
131
    {
132 4
        return array_key_exists($this->currentUser->getId(), $this->post->getRatings());
133
    }
134
135
    #[Override]
136
    public function getMark(): ?int
137
    {
138
        return $this->mark;
139
    }
140
141
    #[Override]
142
    public function setMark(int $mark): void
143
    {
144
        $this->mark = $mark;
145
    }
146
147
    #[Override]
148
    public function getDivClass(): string
149
    {
150
        return $this->isHighlighted ? 'red_box' : 'box';
151
    }
152
153
    #[Override]
154
    public function setIsHighlighted(bool $isHighlighted): void
155
    {
156
        $this->isHighlighted = $isHighlighted;
157
    }
158
159 2
    #[Override]
160
    public function getRating(): int
161
    {
162 2
        return (int) array_sum(
163 2
            array_filter(
164 2
                $this->post->getRatings(),
165 2
                static fn(int $value): bool => $value > 0
166 2
            )
167 2
        );
168
    }
169
170 2
    #[Override]
171
    public function getRatingBar(): string
172
    {
173 2
        $ratingAmount = count($this->post->getRatings());
174
175 2
        if ($ratingAmount === 0) {
176 1
            return '';
177
        }
178
179 1
        return $this->statusBarFactory
180 1
            ->createStatusBar()
181 1
            ->setColor(StatusBarColorEnum::STATUSBAR_YELLOW)
182 1
            ->setLabel('Bewertung')
183 1
            ->setMaxValue($ratingAmount)
184 1
            ->setValue($this->getRating())
185 1
            ->render();
186
    }
187
    #[Override]
188
    public function hasTranslation(): bool
189
    {
190
        $text = $this->getText();
191
        return strpos($text, '[translate]') !== false && strpos($text, '[/translate]') !== false;
192
    }
193
}
194