Passed
Push — dev ( 7914d2...bbc331 )
by Janko
10:29
created

AllianceBoardTopic::getPostCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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\Orm\Entity;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ORM\Mapping\Column;
10
use Doctrine\ORM\Mapping\Entity;
11
use Doctrine\ORM\Mapping\GeneratedValue;
12
use Doctrine\ORM\Mapping\Id;
13
use Doctrine\ORM\Mapping\Index;
14
use Doctrine\ORM\Mapping\JoinColumn;
15
use Doctrine\ORM\Mapping\ManyToOne;
16
use Doctrine\ORM\Mapping\OneToMany;
17
use Doctrine\ORM\Mapping\OrderBy;
18
use Doctrine\ORM\Mapping\Table;
19
use Stu\Module\Alliance\View\Topic\Topic;
20
use Stu\Orm\Repository\AllianceBoardTopicRepository;
21
22
#[Table(name: 'stu_alliance_topics')]
23
#[Index(name: 'recent_topics_idx', columns: ['alliance_id', 'last_post_date'])]
24
#[Index(name: 'ordered_topics_idx', columns: ['board_id', 'last_post_date'])]
25
#[Entity(repositoryClass: AllianceBoardTopicRepository::class)]
26
class AllianceBoardTopic
27
{
28
    #[Id]
29
    #[Column(type: 'integer')]
30
    #[GeneratedValue(strategy: 'IDENTITY')]
31
    private int $id;
32
33
    #[Column(type: 'integer')]
34
    private int $board_id = 0;
35
36
    #[Column(type: 'integer')]
37
    private int $alliance_id = 0;
0 ignored issues
show
introduced by
The private property $alliance_id is not used, and could be removed.
Loading history...
38
39
    #[Column(type: 'string')]
40
    private string $name = '';
41
42
    #[Column(type: 'integer')]
43
    private int $last_post_date = 0;
44
45
    #[Column(type: 'integer')]
46
    private int $user_id = 0;
47
48
    #[Column(type: 'boolean')]
49
    private bool $sticky = false;
50
51
    #[ManyToOne(targetEntity: AllianceBoard::class, inversedBy: 'topics')]
52
    #[JoinColumn(name: 'board_id', nullable: false, referencedColumnName: 'id', onDelete: 'CASCADE')]
53
    private AllianceBoard $board;
54
55
    #[ManyToOne(targetEntity: Alliance::class)]
56
    #[JoinColumn(name: 'alliance_id', nullable: false, referencedColumnName: 'id', onDelete: 'CASCADE')]
57
    private Alliance $alliance;
58
59
    #[ManyToOne(targetEntity: User::class)]
60
    #[JoinColumn(name: 'user_id', nullable: false, referencedColumnName: 'id', onDelete: 'CASCADE')]
61
    private User $user;
62
63
    /**
64
     * @var ArrayCollection<int, AllianceBoardPost>
65
     */
66
    #[OneToMany(targetEntity: AllianceBoardPost::class, mappedBy: 'topic')]
67
    #[OrderBy(['date' => 'DESC'])]
68
    private Collection $posts;
69
70
    public function __construct()
71
    {
72
        $this->posts = new ArrayCollection();
73
    }
74
75 6
    public function getId(): int
76
    {
77 6
        return $this->id;
78
    }
79
80 5
    public function getBoardId(): int
81
    {
82 5
        return $this->board_id;
83
    }
84
85
    public function setBoardId(int $boardId): AllianceBoardTopic
86
    {
87
        $this->board_id = $boardId;
88
89
        return $this;
90
    }
91
92 7
    public function getName(): string
93
    {
94 7
        return $this->name;
95
    }
96
97
    public function setName(string $name): AllianceBoardTopic
98
    {
99
        $this->name = $name;
100
101
        return $this;
102
    }
103
104
    public function getLastPostDate(): int
105
    {
106
        return $this->last_post_date;
107
    }
108
109
    public function setLastPostDate(int $lastPostDate): AllianceBoardTopic
110
    {
111
        $this->last_post_date = $lastPostDate;
112
113
        return $this;
114
    }
115
116
    public function getUserId(): int
117
    {
118
        return $this->user_id;
119
    }
120
121 2
    public function getSticky(): bool
122
    {
123 2
        return $this->sticky;
124
    }
125
126
    public function setSticky(bool $sticky): AllianceBoardTopic
127
    {
128
        $this->sticky = $sticky;
129
130
        return $this;
131
    }
132
133
    public function getUser(): User
134
    {
135
        return $this->user;
136
    }
137
138
    public function setUser(User $user): AllianceBoardTopic
139
    {
140
        $this->user = $user;
141
        return $this;
142
    }
143
144
    /**
145
     * @return null|array<int>
146
     */
147 1
    public function getPages(): ?array
148
    {
149 1
        $postCount = count($this->getPosts());
150
151 1
        if ($postCount <= Topic::ALLIANCEBOARDLIMITER) {
152 1
            return null;
153
        }
154
155
        $pages = [];
156
        for ($i = 1; $i <= ceil($postCount / Topic::ALLIANCEBOARDLIMITER); $i++) {
157
            $pages[$i] = ($i - 1) * Topic::ALLIANCEBOARDLIMITER;
158
        }
159
        return $pages;
160
    }
161
162 3
    public function getPostCount(): int
163
    {
164 3
        return count($this->posts);
165
    }
166
167 3
    public function getLatestPost(): ?AllianceBoardPost
168
    {
169 3
        $post = $this->getPosts()->first();
170
171 3
        return $post === false
172
            ? null
173 3
            : $post;
174
    }
175
176 2
    public function getBoard(): AllianceBoard
177
    {
178 2
        return $this->board;
179
    }
180
181
    public function setBoard(AllianceBoard $board): AllianceBoardTopic
182
    {
183
        $this->board = $board;
184
185
        return $this;
186
    }
187
188
    /**
189
     * @return Collection<int, AllianceBoardPost>
190
     */
191 3
    public function getPosts(): Collection
192
    {
193 3
        return $this->posts;
194
    }
195
196 3
    public function getAlliance(): Alliance
197
    {
198 3
        return $this->alliance;
199
    }
200
201
    public function setAlliance(Alliance $alliance): AllianceBoardTopic
202
    {
203
        $this->alliance = $alliance;
204
205
        return $this;
206
    }
207
}
208