Passed
Push — dev ( 18efcd...cfb449 )
by Nico
24:45 queued 11:36
created

KnItem   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Test Coverage

Coverage 80.3%

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 156
ccs 53
cts 66
cp 0.803
rs 9.92
c 0
b 0
f 0
wmc 31

24 Methods

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