Passed
Pull Request — master (#38)
by Kevin
19:12
created

Post::removeTag()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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