Passed
Push — master ( 99ed0e...bd8f44 )
by Nico
15:30 queued 06:48
created

AllianceBoard   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 68.75%

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 105
ccs 22
cts 32
cp 0.6875
rs 10
c 0
b 0
f 0
wmc 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 3 1
A __construct() 0 4 1
A getPostCount() 0 6 1
A getAlliance() 0 3 1
A setAlliance() 0 5 1
A getTopics() 0 3 1
A getTopicCount() 0 3 1
A getName() 0 3 1
A getLatestPost() 0 7 2
A getPosts() 0 3 1
A setName() 0 5 1
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\Orm\Repository\AllianceBoardRepository;
20
21
#[Table(name: 'stu_alliance_boards')]
22
#[Index(name: 'alliance_idx', columns: ['alliance_id'])]
23
#[Entity(repositoryClass: AllianceBoardRepository::class)]
24
class AllianceBoard
25
{
26
    #[Id]
27
    #[Column(type: 'integer')]
28
    #[GeneratedValue(strategy: 'IDENTITY')]
29
    private int $id;
30
31
    #[Column(type: 'integer')]
32
    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...
33
34
    #[Column(type: 'string')]
35
    private string $name = '';
36
37
    /**
38
     * @var ArrayCollection<int, AllianceBoardTopic>
39
     */
40
    #[OneToMany(targetEntity: AllianceBoardTopic::class, mappedBy: 'board')]
41
    private Collection $topics;
42
43
    /**
44
     * @var ArrayCollection<int, AllianceBoardPost>
45
     */
46
    #[OneToMany(targetEntity: AllianceBoardPost::class, mappedBy: 'board')]
47
    #[OrderBy(['date' => 'DESC'])]
48
    private Collection $posts;
49
50
    #[ManyToOne(targetEntity: Alliance::class)]
51
    #[JoinColumn(name: 'alliance_id', nullable: false, referencedColumnName: 'id', onDelete: 'CASCADE')]
52
    private Alliance $alliance;
53
54
    public function __construct()
55
    {
56
        $this->topics = new ArrayCollection();
57
        $this->posts = new ArrayCollection();
58
    }
59
60 4
    public function getId(): int
61
    {
62 4
        return $this->id;
63
    }
64
65 6
    public function getName(): string
66
    {
67 6
        return $this->name;
68
    }
69
70
    public function setName(string $name): AllianceBoard
71
    {
72
        $this->name = $name;
73
74
        return $this;
75
    }
76
77 1
    public function getTopicCount(): int
78
    {
79 1
        return count($this->topics);
80
    }
81
82 1
    public function getPostCount(): int
83
    {
84 1
        return array_reduce(
85 1
            $this->getTopics()->toArray(),
86 1
            fn(int $sum, AllianceBoardTopic $allianceBoardTopic): int => $sum + $allianceBoardTopic->getPostCount(),
87 1
            0
88 1
        );
89
    }
90
91 1
    public function getLatestPost(): ?AllianceBoardPost
92
    {
93 1
        $post = $this->getPosts()->first();
94
95 1
        return $post === false
96
            ? null
97 1
            : $post;
98
    }
99
100
    /**
101
     * @return Collection<int, AllianceBoardTopic>
102
     */
103 1
    public function getTopics(): Collection
104
    {
105 1
        return $this->topics;
106
    }
107
108 3
    public function getAlliance(): Alliance
109
    {
110 3
        return $this->alliance;
111
    }
112
113
    public function setAlliance(Alliance $alliance): AllianceBoard
114
    {
115
        $this->alliance = $alliance;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @return Collection<int, AllianceBoardPost>
122
     */
123 1
    public function getPosts(): Collection
124
    {
125 1
        return $this->posts;
126
    }
127
}
128