Passed
Pull Request — master (#127)
by Wouter
02:39
created

Post::setTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Fixtures\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
8
/**
9
 * @ORM\Entity(repositoryClass="Zenstruck\Foundry\Tests\Fixtures\Repository\PostRepository")
10
 * @ORM\Table(name="posts")
11
 */
12
class Post
13
{
14
    /**
15
     * @ORM\Id
16
     * @ORM\GeneratedValue
17
     * @ORM\Column(type="integer")
18
     */
19
    private $id;
0 ignored issues
show
introduced by
The private property $id is not used, and could be removed.
Loading history...
20
21
    /**
22
     * @ORM\Column(type="string", length=255)
23
     */
24
    private $title;
25
26
    /**
27
     * @ORM\Column(type="text")
28
     */
29
    private $body;
30
31
    /**
32
     * @ORM\Column(type="string", length=255, nullable=true)
33
     */
34
    private $shortDescription;
35
36
    /**
37
     * @ORM\Column(type="integer")
38
     */
39
    private $viewCount = 0;
40
41
    /**
42
     * @ORM\Column(type="datetime")
43
     */
44
    private $createdAt;
45
46
    /**
47
     * @ORM\Column(type="datetime", nullable=true)
48
     */
49
    private $publishedAt;
50
51
    /**
52
     * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="posts")
53
     * @ORM\JoinColumn
54
     */
55
    private $category;
56
57
    /**
58
     * @ORM\ManyToMany(targetEntity=Tag::class, inversedBy="posts")
59
     */
60
    private $tags;
61
62
    /**
63
     * @ORM\OneToMany(targetEntity=Comment::class, mappedBy="post")
64
     */
65
    private $comments;
66
67
    public function __construct(string $title, string $body, ?string $shortDescription = null)
68
    {
69
        $this->title = $title;
70
        $this->body = $body;
71
        $this->shortDescription = $shortDescription;
72
        $this->createdAt = new \DateTime('now');
73
        $this->tags = new ArrayCollection();
74
        $this->comments = new ArrayCollection();
75
    }
76
77
    public function __toString(): string
78
    {
79
        return $this->title;
80
    }
81
82
    public function getTitle(): ?string
83
    {
84
        return $this->title;
85
    }
86
87
    public function setTitle(string $title): void
88
    {
89
        $this->title = $title;
90
    }
91
92
    public function getBody(): ?string
93
    {
94
        return $this->body;
95
    }
96
97
    public function setBody(string $body): void
98
    {
99
        $this->body = $body;
100
    }
101
102
    public function getShortDescription(): ?string
103
    {
104
        return $this->shortDescription;
105
    }
106
107
    public function getViewCount(): int
108
    {
109
        return $this->viewCount;
110
    }
111
112
    public function increaseViewCount(int $amount = 1): void
113
    {
114
        $this->viewCount += $amount;
115
    }
116
117
    public function getCreatedAt(): ?\DateTime
118
    {
119
        return $this->createdAt;
120
    }
121
122
    public function getCategory(): ?Category
123
    {
124
        return $this->category;
125
    }
126
127
    public function setCategory(?Category $category)
128
    {
129
        $this->category = $category;
130
    }
131
132
    public function isPublished(): bool
133
    {
134
        return null !== $this->publishedAt;
135
    }
136
137
    public function setPublishedAt(\DateTime $timestamp)
138
    {
139
        $this->publishedAt = $timestamp;
140
    }
141
142
    public function getTags()
143
    {
144
        return $this->tags;
145
    }
146
147
    public function addTag(Tag $tag)
148
    {
149
        if (!$this->tags->contains($tag)) {
150
            $this->tags[] = $tag;
151
        }
152
    }
153
154
    public function removeTag(Tag $tag)
155
    {
156
        if ($this->tags->contains($tag)) {
157
            $this->tags->removeElement($tag);
158
        }
159
    }
160
161
    public function getComments()
162
    {
163
        return $this->comments;
164
    }
165
166
    public function addComment(Comment $comment): self
167
    {
168
        if (!$this->comments->contains($comment)) {
169
            $this->comments[] = $comment;
170
            $comment->setPost($this);
171
        }
172
173
        return $this;
174
    }
175
176
    public function removeComment(Comment $comment): self
177
    {
178
        if ($this->comments->contains($comment)) {
179
            $this->comments->removeElement($comment);
180
            // set the owning side to null (unless already changed)
181
            if ($comment->getPost() === $this) {
182
                $comment->setPost(null);
183
            }
184
        }
185
186
        return $this;
187
    }
188
}
189