Passed
Pull Request — master (#216)
by Charly
03:45
created

Post::setTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
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;
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 getId()
83
    {
84
        return $this->id;
85
    }
86
87
    public function getTitle(): ?string
88
    {
89
        return $this->title;
90
    }
91
92
    public function setTitle(?string $title): self
93
    {
94
        $this->title = $title;
95
96
        return $this;
97
    }
98
99
    public function getBody(): ?string
100
    {
101
        return $this->body;
102
    }
103
104
    public function getShortDescription(): ?string
105
    {
106
        return $this->shortDescription;
107
    }
108
109
    public function getViewCount(): int
110
    {
111
        return $this->viewCount;
112
    }
113
114
    public function increaseViewCount(int $amount = 1): void
115
    {
116
        $this->viewCount += $amount;
117
    }
118
119
    public function getCreatedAt(): ?\DateTime
120
    {
121
        return $this->createdAt;
122
    }
123
124
    public function getCategory(): ?Category
125
    {
126
        return $this->category;
127
    }
128
129
    public function setCategory(?Category $category)
130
    {
131
        $this->category = $category;
132
    }
133
134
    public function isPublished(): bool
135
    {
136
        return null !== $this->publishedAt;
137
    }
138
139
    public function setPublishedAt(\DateTime $timestamp)
140
    {
141
        $this->publishedAt = $timestamp;
142
    }
143
144
    public function getTags()
145
    {
146
        return $this->tags;
147
    }
148
149
    public function addTag(Tag $tag)
150
    {
151
        if (!$this->tags->contains($tag)) {
152
            $this->tags[] = $tag;
153
        }
154
    }
155
156
    public function removeTag(Tag $tag)
157
    {
158
        if ($this->tags->contains($tag)) {
159
            $this->tags->removeElement($tag);
160
        }
161
    }
162
163
    public function getComments()
164
    {
165
        return $this->comments;
166
    }
167
168
    public function addComment(Comment $comment): self
169
    {
170
        if (!$this->comments->contains($comment)) {
171
            $this->comments[] = $comment;
172
            $comment->setPost($this);
173
        }
174
175
        return $this;
176
    }
177
178
    public function removeComment(Comment $comment): self
179
    {
180
        if ($this->comments->contains($comment)) {
181
            $this->comments->removeElement($comment);
182
            // set the owning side to null (unless already changed)
183
            if ($comment->getPost() === $this) {
184
                $comment->setPost(null);
185
            }
186
        }
187
188
        return $this;
189
    }
190
}
191